日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

*【CodeForces - 791B】Bear and Friendship Condition (图论,判断完全图,dfs乱搞或带权并查集)

發布時間:2023/12/10 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 *【CodeForces - 791B】Bear and Friendship Condition (图论,判断完全图,dfs乱搞或带权并查集) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

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.

?

?

題目大意:

? ??朋友關系,定義如果x和y是朋友,y和z是朋友,那么x和z也使朋友,給出朋友關系圖,問你這張圖是否正確。

解題報告:

? ?其實就是判斷一個圖的每連通分支,是否都是完全圖。如果用dfs做

AC代碼:(這里是用vector存邊,其實用鄰接表存邊更妥當,時間也更快)

#include<iostream> #include<vector> using namespace std; bool vis[150000 + 5]; vector<int > vv[150000 + 5]; int n,m; int cnt; bool dfs(int x) {for(int i = 0; i<vv[x].size(); i++) {if(vis[vv[x][i] ] == 0) {cnt++;vis[vv[x][i] ] = 1;if(vv[x].size() !=vv[vv[x][i]].size() ) return 0;if( dfs(vv[x][i] ) == 0 ) return 0;} }return 1; }int main() {cin>>n>>m;int u,v;while(m--) {scanf("%d %d",&u,&v);vv[u].push_back(v);vv[v].push_back(u);}//暴力所有的點 int flag = 1;for(int i = 1; i<=n; i++) {if(vis[i] == 1) continue;if(vv[i].size() == 0) continue; cnt = 1;vis[i] = 1;if(dfs(i) == 0 ) { // cout<<11111<<endl;flag = 0;break;}if(cnt-1 != vv[i].size() ) { // cout<<cnt<<endl;flag = 0;break;} }if(flag == 0) printf("NO\n");else printf("YES\n");return 0 ; }

?再貼一個網絡上的并查集AC代碼:(還未看)維護兩個權值,一個集合的點的個數和,一個集合包含的邊的個數和。

#include<iostream> #include<algorithm> #include<string.h> #include<stdio.h> #include<vector> #include<string> #include<cmath> #include<set> #include<queue> using namespace std; #define ll long long #define inf 0x3f3f3f3f const int mod = 1000000007; const int maxm = 1005; const int maxn = 150050; int n, m; int f[maxn]; ll num[maxn]; ll e[maxn]; int F(int x){if (f[x] == x)return x;return f[x] = F(f[x]); } int main(){int x, y;scanf("%d%d", &n, &m);for (int i = 1; i <= n; i++){f[i] = i;num[i] = 1;e[i] = 0;}for (int i = 0; i < m; i++){scanf("%d%d", &x, &y);if (F(x) == F(y)){ e[f[x]]++; }else{num[f[y]] += num[f[x]];e[f[y]] += e[f[x]] + 1;f[f[x]] = f[y];}}bool flag = 1;for (int i = 1; i <= n; i++){if (F(i) == i){ll u = num[i] * (num[i] - 1) / 2;if (e[i] != u){ flag = 0; break; }}}if (flag)printf("YES");else printf("NO");return 0; }

?

總結

以上是生活随笔為你收集整理的*【CodeForces - 791B】Bear and Friendship Condition (图论,判断完全图,dfs乱搞或带权并查集)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。