杭电1213
来源:互联网 发布:中国风qq头像源码 编辑:程序博客网 时间:2024/06/10 04:36
题目描述:
Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers.
One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.
For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.
One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.
For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.
这题就是典型的并查集(刚刚学会并查集这个概念),即求有几个几个互不相交的集合,AC代码:
using System;namespace a1{class Program{public static int[] a;//并查集初始化public static void Init(int n){for (int i = 0; i < n; i++)a[i] = i;}//合并两个元素所在的集合public static void Union(int x, int y){x = getfather(x);y = getfather(y);if (x != y)a[x] = y;}//判断两个元素是否属于同一个集合public static bool same(int x, int y){return getfather(x) == getfather(y);}//获取根结点public static int getfather(int x){while (x != a[x])x = a[x];return x;}public static void Main(string[] args){string str = string.Empty;str = Console.ReadLine();int t;t = Convert.ToInt32(str);while (t-- > 0) {str = Console.ReadLine();string[] s = str.Split(' ');int n = Convert.ToInt32(s[0]), m = Convert.ToInt32(s[1]);a = new int[n];Init(n);while (m-- > 0) {str = Console.ReadLine();s = str.Split(' ');int p = Convert.ToInt32(s[0]) - 1, q = Convert.ToInt32(s[1]) - 1;if(!same(p,q)) Union(p,q);}Console.ReadLine();int flag=0;for(int i=0;i<n;i++)if(a[i]==i) flag++;Console.WriteLine("{0}",flag);}}}}千万不要漏了最后那边的Console.Readline(); 否则读取格式就不对了
0 0
- 杭电1213
- 杭电1213
- 杭电 1213 通畅工程
- 杭电
- 杭电
- 杭电
- 杭电acm 1213 (How many tables)
- 杭电1213-How Many Tables
- 杭电1213(并查集)
- 杭电 1213 How Many Tables
- 【杭电1213】How Many Tables
- 杭电1213How Many Tables
- 【杭电】[1213]How Many Tables
- 杭电1213 How many tables
- 杭电oj--1213 How Many Tables
- 杭电1213-并查集
- 杭电1213How Many Tables与杭电1232大同小异
- 杭电 1234 和 杭电 2115
- js小技巧(持续更新)
- CSDN-markdown编辑器使用指南
- 如何成为一名优秀的 C 语言程序员
- 【NDN基础】Networking Named Content 全文翻译
- 云计算学习
- 杭电1213
- 浅谈Android中的Handler
- 海波账
- 去趋势分析
- Unity3D 参考网址
- ****ife 递归实现深度克隆(内含JS数据类型判断,对象遍历)****
- LeetCode : Merge k Sorted Lists [java]
- 第一个 C 语言编译器是怎样编写的?
- bat 批处理脚本 修改指定路径czz文件内容