【codeforces 791B】Bear and Friendship Condition
【题目链接】:http://codeforces.com/contest/791/problem/B
【题意】
给你m对朋友关系;
如果x-y是朋友,y-z是朋友
要求x-z也是朋友.
问你所给的图是否符合
【题解】
用并查集处理出每个朋友“团”的大小->就是连通块
然后这个连通块里面应该要有x*(x-1)/2条边;
按照这个规则,求出应该有的边数;
然后和所给的m对比;
相同则YES,否则NO
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 15e4+100;
int n, m;
int f[N];
LL cnt[N];
LL ans = 0;
bool bo[N];
int ff(int x)
{
if (f[x] != x)
return f[x] = ff(f[x]);
else
return x;
}
void in()
{
rei(n), rei(m);
rep1(i, 1, n)
f[i] = i, cnt[i] = 1;
int x, y;
rep1(i, 1, m)
{
rei(x), rei(y);
int r1 = ff(x), r2 = ff(y);
if (r1 != r2)
{
f[r1] = r2;
cnt[r2] += cnt[r1];
}
}
}
bool ga()
{
rep1(i, 1, n)
{
int t = ff(i);
if (bo[t]) continue;
bo[t] = true;
ans += 1LL*cnt[t]*(cnt[t]-1)/2;
if (ans > m)
{
return false;
}
}
if (ans != m)
return false;
else
return true;
}
void o()
{
if (ga())
puts("YES");
else
puts("NO");
}
int main()
{
//freopen("F:\\rush.txt", "r", stdin);
in();
o();
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}