日韩性视频-久久久蜜桃-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乱搞或带权并查集)的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 夜色一区二区三区 | 国模私拍在线 | 国产精品美女久久 | 国产又大又黑又粗免费视频 | 亚州精品视频 | 黑白配在线观看免费观看 | 欧洲久久久久久 | 亚洲天堂av在线播放 | 久久久一| 亚洲免费三区 | 国内精品人妻无码久久久影院蜜桃 | 99re这里只有精品首页 | 成人黄色小视频 | 人妻激情文学 | 国产精品久久久久久久蜜臀 | 亚洲免费婷婷 | www.黄色片 | 成人精品| 欧美国产高清 | 诱夫1v1高h | 日本黄色录像片 | 爱爱色图| 青青青国内视频在线观看软件 | 国产三区四区视频 | 在线无码va中文字幕无码 | 五月天久久综合 | 亚洲第一视频 | 男女啊啊啊视频 | 国产一区二区视频在线 | 开心激情五月婷婷 | av片手机在线观看 | 牛牛视频在线观看 | 国产av第一区 | 日韩三级欧美 | 国产精品免费一区二区三区 | 亚洲欧美在线视频 | 一级黄色片网站 | 秋霞欧美视频 | a毛片在线| 青青草91久久久久久久久 | 无码无套少妇毛多18pxxxx | av片在线观看网站 | 天天干夜夜玩 | 国内老熟妇对白xxxxhd | 免费在线观看网址入口 | 182av| 欧美日韩不卡在线 | 亚洲黄色一区二区三区 | 亚洲码在线观看 | 91精品国产一区二区三竹菊影视 | 久久99久久99精品免观看软件 | 精品视频一区二区三区在线观看 | 高清国产一区二区三区四区五区 | 国产精品自拍视频一区 | 男女视频免费看 | 97av视频 | 性高潮免费视频 | 天堂网8 | 亚洲男女av | 国产av人人夜夜澡人人爽 | 亚洲色图综合在线 | 这里只有精品视频在线观看 | 免费av一区二区三区 | 欧美日韩免费做爰视频 | 另类小说亚洲色图 | 古装做爰无遮挡三级聊斋艳谭 | 久久婷婷婷| 女人的av | 久射久 | 精品国产黄 | 91偷拍一区二区三区精品 | 黄色永久视频 | 99在线免费观看视频 | 大粗鳮巴久久久久久久久 | 国产精品自在线 | 最新版天堂资源在线 | 国产精品一区二区毛片 | 四虎8848精品成人免费网站 | 欧美中文字幕在线观看 | 日本人六九视频 | 手机在线不卡av | 日本熟妇一区二区三区四区 | a男人天堂 | 色播五月综合 | 看毛片的网站 | 黄色天堂网站 | 日本黄视频在线观看 | 日韩欧美国产网站 | 黄a视频 | 国产一二三在线视频 | 91看视频| 久久精品日韩 | 国产福利精品一区 | 国产麻豆剧果冻传媒白晶晶 | 欧美成人播放 | 国产欧美久久一区二区三区 | 99爱视频| 国产精品乱码一区二区三区 | 免费黄色av |