Codeforces 791B. Bear and Friendship Condition 联通快 完全图

B. Bear and Friendship Condition
time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output

Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures).

There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves.

Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z.

For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well.

Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes.

Input

The first line of the input contain two integers n and m (3 ≤ n ≤ 150 000, ) — the number of members and the number of pairs of members that are friends.

The i-th of the next m lines contains two distinct integers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input.

Output

If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes).

Examples
Input
4 3
1 3
3 4
1 4
Output
YES
Input
4 4
3 1
2 3
3 4
1 2
Output
NO
Input
10 4
4 3
5 10
8 9
1 2
Output
YES
Input
3 2
1 2
2 3
Output
NO
Note

The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not.

题目链接:http://codeforces.com/contest/791/problem/B
题意:求一个无向图的每个联通快是否都是完全图。
思路:dfs搜索或者并查集。m条边和n个点的无向完全图满足关系m=n*(n-1)/2,或者每个点的度de[i]都满足de[i]+1=n;
代码:
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<map>
 7 #include<vector>
 8 using namespace std;
 9 typedef long long ll;
10 const int MAXN=2e5+100,INF=0x3f3f3f3f,MOD=1e9+7;
11 int n,m;
12 map<int,vector<int> >G;
13 int vis[MAXN];
14 int current_cc=0;
15 map<int,vector<int> >cc;
16 void dfs(int u)
17 {
18     vis[u]=1;
19     cc[current_cc].push_back(u);
20     for(int i=0; i<G[u].size(); i++)
21     {
22         int v=G[u][i];
23         if(!vis[v]) dfs(v);
24     }
25 }
26 void find_cc()
27 {
28     current_cc=0;
29     memset(vis,0,sizeof(vis));
30     for(int i=1; i<=n; i++)
31     {
32         if(vis[i]) continue;
33         current_cc++;
34         dfs(i);
35     }
36 }
37 int main()
38 {
39     scanf("%d%d",&n,&m);
40     for(int i=1; i<=m; i++)
41     {
42         int u,v;
43         scanf("%d%d",&u,&v);
44         G[u].push_back(v);
45         G[v].push_back(u);
46     }
47     find_cc();
48     int ans=1;
49     for(int i=1; i<=current_cc; i++)
50     {
51         long long cou=0;
52         long long x=cc[i].size();
53         for(int j=0; j<x; j++)
54         {
55             int u=cc[i][j];
56             cou+=G[u].size();
57         }
58         cou=cou/2;
59         if(cou!=x*(x-1)/2) ans=0;
60     }
61     if(ans==1) cout<<"YES"<<endl;
62     else cout<<"NO"<<endl;
63     return 0;
64 }
dfs求联通快
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<map>
 7 #include<queue>
 8 #include<stack>
 9 #include<vector>
10 using namespace std;
11 typedef long long ll;
12 const int MAXN=2e5+100,INF=0x3f3f3f3f,MOD=1e9+7;
13 map<int,vector<int> >G;
14 queue<int>Q;
15 int pa[MAXN];
16 int findset(int x)
17 {
18     return pa[x]==x?x:pa[x]=findset(pa[x]);
19 }
20 ll de[MAXN];
21 ll x[MAXN];
22 int main()
23 {
24     int n,m;
25     scanf("%d%d",&n,&m);
26     for(int i=1; i<=n; i++) pa[i]=i,de[i]=0;
27     for(int i=1; i<=m; i++)
28     {
29         int u,v;
30         scanf("%d%d",&u,&v);
31         de[u]++,de[v]++;
32         int fau=findset(u),fav=findset(v);
33         if(fau>fav) pa[fau]=fav;
34         else pa[fav]=fau;
35     }
36     for(int i=1; i<=n; i++)
37         x[findset(i)]++;;
38     int ans=1;
39     for(int i=1;i<=n;i++)
40     {
41         //cout<<pa[i]<<" "<<x[pa[i]]<<" "<<de[i]<<endl;
42         if(x[pa[i]]!=de[i]+1) ans=0;
43         if(ans==0) break;
44     }
45     if(ans==1) cout<<"YES"<<endl;
46     else cout<<"NO"<<endl;
47     return 0;
48 }
并查集
I am a slow walker,but I never walk backwards.
原文地址:https://www.cnblogs.com/GeekZRF/p/6580210.html