【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;
}
【codeforces 791B】Bear and Friendship Condition的更多相关文章
- 【32.89%】【codeforces 574D】Bear and Blocks
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 791D】 Bear and Tree Jumps
[题目链接]:http://codeforces.com/contest/791/problem/D [题意] 你可以从树上的节点一次最多走k条边. (称为跳一次); 树为无权树; 然后问你任意两点之 ...
- 【codeforces 791C】Bear and Different Names
[题目链接]:http://codeforces.com/contest/791/problem/C [题意] 给你n-k+1个限制 要求 a[i]..a[i]+k-1里面有相同的元素,或全都不同; ...
- 【codeforces 791A】Bear and Big Brother
[题目链接]:http://codeforces.com/contest/791/problem/A [题意] 给你两个数字a和b; a每次乘3,b每次乘2 问你什么时候a第一次大于b [题解] 傻逼 ...
- 【CF771A】Bear and Friendship Condition
题目大意:给定一张无向图,要求如果 A 与 B 之间有边,B 与 C 之间有边,那么 A 与 C 之间也需要有边.问这张图是否满足要求. 题解:根据以上性质,即:A 与 B 有关系,B 与 C 有关系 ...
- 【19.05%】【codeforces 680D】Bear and Tower of Cubes
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【Codeforces 385C】Bear and Prime Numbers
[链接] 我是链接,点我呀:) [题意] f[i]表示在x[]中有多少个数字是i的倍数 让你求出sum(f[i]) li<=i<=ri 且i是质数 [题解] 做筛法求素数的时候顺便把素数i ...
- 【Codeforces 639B】Bear and Forgotten Tree 3
[链接] 我是链接,点我呀:) [题意] [题解] 首先,因为高度是h 所以肯定1下面有连续的h个点依次连成一条链.->用了h+1个点了 然后,考虑d这个约束. 会发现,形成d的这个路径,它一定 ...
- 【Codeforces 639A】Bear and Displayed Friends
[链接] 我是链接,点我呀:) [题意] [题解] 时刻维护一下前K大的数字就好. 因为k<=6 然后数字不会减少只会增加. 因此只要维护一个大小为k的数组就ok. 保证这个数组是有序的. 写个 ...
随机推荐
- Linux学习总结(4)——Centos6.5使用yum安装mysql——快速上手必备
第1步.yum安装mysql [root@stonex ~]# yum -y install mysql-server 安装结果: Installed: mysql-server.x86_6 ...
- [TypeScript] Type check JavaScript files using JSDoc and Typescript 2.5
Typescript 2.5 adds JSDoc type assertion support for javascript file via ts-check service. First of ...
- amazeui学习笔记--css(常用组件5)--评论列表Comment
amazeui学习笔记--css(常用组件5)--评论列表Comment 一.总结 1.am-comment:使用am-comment来声明评论对象,这个是放在article里面的,虽然article ...
- TCP超时重传机制
TCP协议在能够发送数据之前就建立起了"连接".要实现这个连接,启动TCP连接的那一方首先将发送一个SYN数据包.这只是一个不包含数据的数据包, 然后,打开SYN标记.如果另一方同 ...
- Altium Designer如何统一改变pcb状态下的原件标号位置
原创 我用的是Altium Designer16版本 变成 步骤如下: 选中标号 右击 下边一步很重要: 点击应用和确定 在之后弹出的对话框中选则你要改变的位置,我这里是把标号改变到原件的右侧: 等待 ...
- JS学习笔记 - fgm练习 - 输入法下拉框 三元表达式
<script> window.onload = function() { var oBtn = document.getElementsByTagName('input')[0]; va ...
- malloc和realloc
malloc函数: extern void *malloc(unsigned int num_bytes); malloc 向系统申请分配指定size个字节的内存空间. 如果分配成功则返回指向被分配内 ...
- 新浪sae上安装原生wordpress4.1
1. 加入/改动wp-config.php文件 <?php /** * WordPress 基础配置文件. * * 本文件包括下面配置选项: MySQL 设置.数据库表名前缀. * 密匙.Wor ...
- [Nuxt] Build a Navigation Component in Vue.js and Use in a Nuxt Layout
You can isolate parts of templates you want to re-use into components, but you can also reuse those ...
- ZOJ 1494 Climbing Worm 数学水题
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=494 题目大意: 一只蜗牛要从爬上n英寸高的地方,他速度为u每分钟,他爬完u需要 ...