标签:names 并查集 turn www. www mat bzoj cst stream
多刷水题有益健康。。。
题目链接:
http://www.lydsy.com/JudgeOnline/problem.php?id=1116
首先,在这到题中无向边是不算度数的,(看样例就知道了)。。。
考虑如果所有点都联通,那么如果这个图是一颗树,显然会有一个点没有入度,而如果图里至少有一个环,就能满足题目条件。。。
那就用并查集判一下环就可以了。。。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
int n,m,x,y;
int f[100005],c[100005];
int find(int p){
if(f[p]==0) f[p]=p;
if(f[p]==p) return f[p];
f[p]=find(f[p]);
return f[p];
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%d%d",&x,&y);
x=find(x);
y=find(y);
if(x!=y){
f[x]=y;
c[y]=max(c[y],c[x]);
}
if(x==y) c[y]=1;
}
for(int i=1;i<=n;i++){
x=find(i);
if(c[x]==0){
printf("NIE\n");
return 0;
}
}
printf("TAK\n");
return 0;
}
This passage is made by Iscream-2001.
标签:names 并查集 turn www. www mat bzoj cst stream
原文地址:http://www.cnblogs.com/Yuigahama/p/7738410.html