【题目链接】: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;
}

【codeforces 791B】Bear and Friendship Condition的更多相关文章

  1. 【32.89%】【codeforces 574D】Bear and Blocks

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  2. 【codeforces 791D】 Bear and Tree Jumps

    [题目链接]:http://codeforces.com/contest/791/problem/D [题意] 你可以从树上的节点一次最多走k条边. (称为跳一次); 树为无权树; 然后问你任意两点之 ...

  3. 【codeforces 791C】Bear and Different Names

    [题目链接]:http://codeforces.com/contest/791/problem/C [题意] 给你n-k+1个限制 要求 a[i]..a[i]+k-1里面有相同的元素,或全都不同; ...

  4. 【codeforces 791A】Bear and Big Brother

    [题目链接]:http://codeforces.com/contest/791/problem/A [题意] 给你两个数字a和b; a每次乘3,b每次乘2 问你什么时候a第一次大于b [题解] 傻逼 ...

  5. 【CF771A】Bear and Friendship Condition

    题目大意:给定一张无向图,要求如果 A 与 B 之间有边,B 与 C 之间有边,那么 A 与 C 之间也需要有边.问这张图是否满足要求. 题解:根据以上性质,即:A 与 B 有关系,B 与 C 有关系 ...

  6. 【19.05%】【codeforces 680D】Bear and Tower of Cubes

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. 【Codeforces 385C】Bear and Prime Numbers

    [链接] 我是链接,点我呀:) [题意] f[i]表示在x[]中有多少个数字是i的倍数 让你求出sum(f[i]) li<=i<=ri 且i是质数 [题解] 做筛法求素数的时候顺便把素数i ...

  8. 【Codeforces 639B】Bear and Forgotten Tree 3

    [链接] 我是链接,点我呀:) [题意] [题解] 首先,因为高度是h 所以肯定1下面有连续的h个点依次连成一条链.->用了h+1个点了 然后,考虑d这个约束. 会发现,形成d的这个路径,它一定 ...

  9. 【Codeforces 639A】Bear and Displayed Friends

    [链接] 我是链接,点我呀:) [题意] [题解] 时刻维护一下前K大的数字就好. 因为k<=6 然后数字不会减少只会增加. 因此只要维护一个大小为k的数组就ok. 保证这个数组是有序的. 写个 ...

随机推荐

  1. oracle里long类型的总结

    转自原文oracle中long类型为什么不推荐使用 不是不推荐使用的,是一般用不到,而有些时候是会用到的,所以不能一概而论.1.LONG 数据类型中存储的是可变长字符串,最大长度限制是2GB.2.对于 ...

  2. 语言模型(Language Modeling)与统计语言模型

    1. n-grams 统计语言模型研究的是一个单词序列出现的概率分布(probability distribution).例如对于英语,全体英文单词构成整个状态空间(state space). 边缘概 ...

  3. 编程——C语言的问题,比特率

    1;; bps是bits per second的缩写,是指传输速度,音乐采样速度等,表示为: 比特/秒.

  4. 关于DOM的有关总结

    1.获取DOM元素 document.getElementById()  通过id获取DOM元素 document.getElementsByClassName() 通过类名获取DOM元素 docum ...

  5. 关于js盒子模型的知识梳理

    盒子模型 JS盒子模型中的13个常用属性: clientWidth/clientHeight:可视区域的宽高,宽高+PADDING组成 clientTop/clientLeft:上边框和左边框的宽度 ...

  6. nios博文

    小时不识月:http://www.cnblogs.com/yuphone/archive/2010/09/20/1831448.html cracy bingo:http://www.cnblogs. ...

  7. [React Intl] Render Content Based on a Number using react-intl FormattedMessage (plural)

    Using the react-intl FormattedMessage component, we’ll learn how to render content conditionally in ...

  8. POJ 1364 King (UVA 515) 差分约束

    http://poj.org/problem?id=1364 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemi ...

  9. 【z04】计算系数

    [题目链接]:http://noi.qz5z.com/viewtask.asp?id=z04 [题解] 用二项式定理可以写出下列通式 组合数可以用杨辉三角搞出来; a的x次方直接乘就好了;指数也不大. ...

  10. IOS RGB颜色转换

    - (UIColor *)getColor:(NSString *)hexColor { unsigned int red,green,blue; NSRange range; range.lengt ...