日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/10 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 *【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乱搞或带权并查集)的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 99reav| 国产波霸爆乳一区二区 | 美女18网站 | 短视频在线观看 | 日韩人妻精品无码一区二区三区 | 国产激情免费 | 成人性生交大片免费看 | 中文字幕+乱码+中文乱码91 | 一本之道av | 久色88 | 久久久精品视频在线 | 美女黄页网站 | 中文天堂在线视频 | 香蕉国产片 | 人人看人人做 | www黄色 | 国产视频黄色 | 丁香花高清在线 | 桃色视频网 | 日韩精品人妻中文字幕有码 | 国产麻豆免费观看 | 90岁老太婆乱淫 | 黄色三级免费 | 黄色一级片免费看 | 性欧美欧美巨大69 | 久久精品久久久 | 国产精品破处 | 欧美 日韩 中文字幕 | 国产精品日韩欧美大师 | 91性高潮久久久久久久 | 成人黄色激情网 | wwwjavhd| 六月丁香激情 | 91精品免费 | www.日韩精品 | 成人综合色站 | 国产一区二区视频在线免费观看 | 草草影院av | 青青免费在线视频 | 成人1区2区 | 中国zzji女人高潮免费 | 麻豆网站免费观看 | 国产色av| 日韩极品在线观看 | www.青青操 | 久久中文字幕网 | 成人午夜小视频 | 欧美另类日韩 | 欧美日性视频 | 久久不卡免费视频 | 穿情趣内衣被c到高潮视频 欧美性猛交xxxx黑人猛交 | 亚洲精品色图 | 亚洲精品一级片 | 久久精品无码一区二区三区 | 国产精品久久国产愉拍 | 99在线精品视频免费观看软件 | sm在线看 | 欧美综合一区 | 六月婷婷综合网 | h视频亚洲| 手机看片日韩久久 | 成人首页 | 成人av一区 | 狠狠干av | 午夜精品久久久久久久99老熟妇 | 欧美福利视频导航 | 天天天操操操 | 久久久电影 | 欧美日韩免费 | 四虎1515hh.com | 成人动漫在线观看视频 | 午夜精品网站 | 日本一道本在线 | 亚洲美女av在线 | 亚洲蜜桃在线 | 老妇高潮潮喷到猛进猛出 | 久久在线免费观看 | 欧洲亚洲女同hd | 亚洲综合自拍偷拍 | 外国电影免费观看高清完整版 | 成人网站免费观看 | 13日本xxxxxⅹxxx20 | 亚欧美在线 | 无码人妻精品一区二区三 | 精品黑人| 亚洲高清视频一区二区 | 国内精品人妻无码久久久影院蜜桃 | 亚洲一区二区三区四区电影 | 美女免费福利视频 | 福利视频99 | 成人免费看片视频 | 在线视频这里只有精品 | 女人一区二区 | 亚洲一区免费视频 | 国产在线一区二 | 亚洲成人久久精品 | 欧美日韩国产一区二区三区在线观看 | 超碰九七 | 97超碰网|