首页 技术 正文
技术 2022年11月15日
0 收藏 731 点赞 3,753 浏览 1243 个字

【题目链接】: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;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,023
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,361
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,774
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,853