连接:http://acm.hdu.edu.cn/showproblem.php?pid=1272
题意:要 要 要 要啥题意啊就是问题这个东西是不是一颗树
解决方式有很多种,说是冰茶集,但是你用别的方法也可以做啊~
思路:首先作为一棵树一定满足顶点数等于边数+1,但只满足了这个条件也不行(脑补一个圆)判断方法:比方说a和b俩节点,他俩爹要是一样,你再把a和b连上,那不就有环了吗
p.s. 我记得这个题在hdu上会爆栈来着……
还想硬广一下 #pragma comment(linker, "/STACK:1024000000,1024000000") 呢 不开心
#include <bits/stdc++.h>
using namespace std;
int pre[100005];
set<int> s;
int f(int x) {
return x == pre[x] ? x : pre[x] = f(pre[x]);
}
int main() {
int a, b;
while (scanf("%d%d",&a,&b) && a+b!=-2) {
if (a+b==0) {
puts("Yes"); continue;
}
s.clear();
int ans = 0;
for (int i = 0; i <= 100000; i++) {
pre[i] = i;
}
int flag = 0;
s.insert(a);
s.insert(b);
if (a==b) flag = 1;
else pre[b] = a;
ans++;
while(scanf("%d%d",&a,&b) && a+b) {
s.insert(a);s.insert(b);
if(a==b || f(a) == f(b)) flag = 1;
else pre[f(b)] = pre[f(a)];
ans++;
}
if(!flag && ans == s.size() -1) {
cout << "Yes" << ‘\n‘;
} else {
cout << "No" << ‘\n‘;
}
}
return 0;
}
原文:https://www.cnblogs.com/fengyuzhicheng/p/9201711.html