# HDU1272

#include<iostream>
#include<algorithm>
#include<cstring>
#include<set>
using namespace std;
const int maxx = 1e5 + 10;
int pre[maxx];
int ranks[maxx];
int a[maxx];
int vis[maxx];
int n, m;
int flag;
struct node {
int x, y;
}num[maxx];
int cmp(node a, node b) {
if (a.x == b.x) {
return a.y < b.y;
}
return a.x < b.x;
}
void init() {
for (int i = 0; i <= maxx; i++) {
pre[i] = i;
ranks[i] = 0;
a[i] = 0;
}
}
int find(int x) {
int r = x;
if (pre[x] == r) {
return x;
}
return pre[x] = find(pre[x]);
}
void unio(int x, int y) {
int fx = find(x);
int fy = find(y);
if (fx == fy) {
flag=0;
return;
}
if (ranks[fx] < ranks[fy]) {
pre[fx] = fy;
}
else {
pre[fy] = fx;
if (ranks[fx] == ranks[fy]) {
ranks[fx]++;
}
}
}
void Kruskal(int n) {//克鲁斯卡而算法判断是否有环
int cnt = 0;
for (int i = 1; i <= n; i++) {
if (find(num[i].x) != find(num[i].y)) {//根节点不同
unio(num[i].x, num[i].y);
cnt++;
}
}
if (cnt == n) {//可以构成最小生成树
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
}
int main() {
while (cin >> n >> m) {
init();
memset(vis,0,sizeof(vis));
if (n == -1 && m == -1)break;
if(n==0&&m==0){//为零的时候也是满足的
cout<<"Yes"<<endl;
continue;
}
int cnt = 1;
flag=1;
vis[n]=1;
vis[m]=1;
unio(n,m);
num[cnt].x = n; num[cnt].y = m;
while (cin >> n >> m) {
if (n == 0 && m == 0)break;
cnt++;
num[cnt].x = n; num[cnt].y = m;
unio(n,m);
vis[n]=1;vis[m]=1;
}
if(flag==0){
cout<<"No"<<endl;
continue;
}else{
int count=0;
for(int i=1;i<=maxx;i++){
if(vis[i]==1&&find(i)==i){
count++;
}
}
if(count==1){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
}
//sort(num + 1, num + cnt + 1, cmp);
//Kruskal(cnt);
}
return 0;
}

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 打赏

打赏

Keep_Trying_Go

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 举报
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文

08-04 192
08-13 584