G - Brain Network (easy)(并查集水题)

G - Brain Network (easy)

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

CodeForces 690C1

Description

One particularly well-known fact about zombies is that they move and think terribly slowly. While we still don‘t know why their movements are so sluggish, the problem of laggy thinking has been recently resolved. It turns out that the reason is not (as previously suspected) any kind of brain defect – it‘s the opposite! Independent researchers confirmed that the nervous system of a zombie is highly complicated – it consists of n brains (much like a cow has several stomachs). They are interconnected by brain connectors, which are veins capable of transmitting thoughts between brains. There are two important properties such a brain network should have to function properly:

  1. It should be possible to exchange thoughts between any two pairs of brains (perhaps indirectly, through other brains).
  2. There should be no redundant brain connectors, that is, removing any brain connector would make property 1 false.

If both properties are satisfied, we say that the nervous system is valid. Unfortunately (?), if the system is not valid, the zombie stops thinking and becomes (even more) dead. Your task is to analyze a given nervous system of a zombie and find out whether it is valid.

Input

The first line of the input contains two space-separated integers n and m (1 ≤ n, m ≤ 1000) denoting the number of brains (which are conveniently numbered from 1 to n) and the number of brain connectors in the nervous system, respectively. In the next m lines, descriptions of brain connectors follow. Every connector is given as a pair of brains ab it connects (1 ≤ a, b ≤ na ≠ b).

Output

The output consists of one line, containing either yes or no depending on whether the nervous system is valid.

Sample Input

Input

4 41 22 33 14 1

Output

no

Input

6 51 22 33 44 53 6

Output

yes

//并查集水题,第一行 n , m 是点,和边个数,然后 m 边的描述,问是否都连通了且没有多余的边

 1 #include<stdio.h>
 2 int f[1001];
 3 int find(int x)
 4 {
 5     if (f[x]!=x)
 6         f[x]=find(f[x]);
 7     return f[x];
 8 }
 9 int main()
10 {
11     int n,m;
12     while (scanf("%d%d",&n,&m)!=EOF)
13     {
14         int i,j;
15         for (i=1;i<=n;i++)
16         f[i]=i;
17
18         int a,b,temp;
19         scanf("%d%d",&a,&b);
20         temp=a;
21         f[a]=f[b];
22         int ok=1;
23         for (i=2;i<=m;i++)
24         {
25             scanf("%d%d",&a,&b);
26             int head_a=find(a);
27             int head_b=find(b);
28             if (head_a!=head_b)
29             f[head_a]=f[head_b];
30             else
31                 ok=0;
32         }
33         for (i=1;i<=n;i++)
34         {
35             if (ok==0) break;
36             if (find(i)!=find(temp))
37             {
38             ok=0;
39             break;
40             }
41         }
42         if (ok)
43         printf("yes\n");
44         else
45         printf("no\n");
46     }
47     return 0;
48 }

时间: 2024-08-02 02:39:05

G - Brain Network (easy)(并查集水题)的相关文章

POJ2236 wireless network 【并查集水题】

前端开发whqet,csdn,王海庆,whqet,前端开发专家 今天是个好日子,2014年5月20日,表白的最佳时机,虽说孩子已经四岁.结婚已经五年,但是也不可以偷懒,于是有了这个效果. 在线研究点这里,下载收藏点这里.程序猿and程序媛,大胆秀出你的爱吧. 利用html5 canvas实现动态的文字粒子效果,效果如下. OK,简单看看原理,首先我们需要在canvas里面实现描边文字,然后利用getImageData获得描边文字的像素矩阵,将粒子效果绑定在描边文章上. 整个效果如下. html文

并查集水题 POJ 1611

题意:有n(n<=30000)个学生,每个学生属于一个团体(可以属于多个团体),共有m(<=500)个团体,如果一个学生是嫌疑人,则他所在的团体的所有人都是嫌疑人,初始时0号学生是嫌疑人.问总共有多少个嫌疑人. 很明显同一个团体的学生可以连一条边,即求0号点所在的连通块有多少个点,用并查集可以很方便的办到,如果两个点属于同一个连通块则把他们的代表元连接起来即可,始终把较小的那个节点作为父节点,所以最后p[0]的节点数就是答案. 代码:

并查集水题 POJ2524

题意:一所学校有有n个学生,询问m对学生的宗教是否相同,求出这所学校最多有多少种宗教. 把宗教相同的学生连一条边,则未询问的学生默认他们没有边,最后连通块的个数就是宗教最多有多少个,并查集实现,把每个节点的最终父节点存到数组里,数组里不同元素的个数即为连通块的个数. 代码:

poj2524(并查集水题)

题目链接:http://poj.org/problem?id=2524 题目大意:学校共有n个同学,告诉你m对同学信仰同一宗教,问这个学校学生信仰宗教的数目最多为多少. 例: Sample Input 10 91 21 31 41 51 61 71 81 91 1010 42 34 54 85 80 0 Sample Output Case 1: 1Case 2: 7 解题思路:直接套并查集的板子就可以了,初始化n个集合默认他们都信任不一样的宗教,初始就用n个宗教,每次给你的两个同学那个号码将他

POJ2524并查集水题

Description There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in finding out how many different religions students in your university believe in. You know that there are n stud

hdu 4496 并查集 反向并查集 水题 D-City

觉得这道题以后可以和优先队列结合起来 嗯 就是说依次去掉前n条路求连通块数量 处理的时候  只要每次merge发现父亲不相等 然后进到里面合并的时候 num-- wa了好几次是因为最后输出的时候开了点的数量大小的数组而不是操作数量数组 orz D-City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 5334    Accepted

HDU1863(Kruskal+并查集水题)

https://cn.vjudge.net/problem/HDU-1863 省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本.现请你编写程序,计算出全省畅通需要的最低成本. Input测试输入包含若干测试用例.每个测试用例的第1行给出评估的道路条数 N.村庄数目M ( < 100 ):随后的 N 行对应村庄间道路的成本,每行给出一对正整数,分别是两个村庄的

POJ2236 Wireless Network 【并查集】

Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 16885   Accepted: 7091 Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computer

poj 2236:Wireless Network(并查集,提高题)

Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 16065   Accepted: 6778 Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computer