题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272
题意:中文题0.0
思路:这道题是在并查集专题里卖弄找到的,但是一看题目就是要求图中有没有环,那么直接判断边数是否等于点数-1就行了。
注意:这里需要特判一下第一个数据就是0,0的情况,答案是Yes
代码:
#include <cstdio>
#include <cmath>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <cctype>
#include <sstream>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 1e5 + 5;
int main () {
//freopen ("in.txt", "r", stdin);
int u, v;
int en = 0, pn = 0;
bool vis[maxn];
while (~scanf ("%d%d", &u, &v)) {
if (u == 0 && v == 0) {printf ("Yes\n");continue;}
memset(vis, 0, sizeof(vis));
en=0,pn=0;
if (u == -1 && v == -1) break;
if (!vis[u]) pn++,vis[u]=1;
if (!vis[v]) pn++,vis[v]=1;
while (~scanf ("%d%d", &u, &v)) {
if (u == 0 && v == 0) break;
if (!vis[u]) pn++,vis[u]=1;
if (!vis[v]) pn++,vis[v]=1;
en++;
}
if (++en!=pn-1) printf ("No\n");
else printf ("Yes\n");
}
return 0;
}