*【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 4Output
YESInput
4 4 3 1 2 3 3 4 1 2Output
NOInput
10 4 4 3 5 10 8 9 1 2Output
YESInput
3 2 1 2 2 3Output
NONote
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.
?
?
題目大意:
? ??朋友關(guān)系,定義如果x和y是朋友,y和z是朋友,那么x和z也使朋友,給出朋友關(guān)系圖,問你這張圖是否正確。
解題報(bào)告:
? ?其實(shí)就是判斷一個(gè)圖的每連通分支,是否都是完全圖。如果用dfs做
AC代碼:(這里是用vector存邊,其實(shí)用鄰接表存邊更妥當(dāng),時(shí)間也更快)
#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);}//暴力所有的點(diǎn) 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 ; }?再貼一個(gè)網(wǎng)絡(luò)上的并查集AC代碼:(還未看)維護(hù)兩個(gè)權(quán)值,一個(gè)集合的點(diǎn)的個(gè)數(shù)和,一個(gè)集合包含的邊的個(gè)數(shù)和。
#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; }?
總結(jié)
以上是生活随笔為你收集整理的*【CodeForces - 791B】Bear and Friendship Condition (图论,判断完全图,dfs乱搞或带权并查集)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 票房破66亿:《壮志凌云2》成2022年
- 下一篇: rtmservice.exe - rtm