hdu 1272

并查集,这题打代码只用了半个小时,调试却调了四个小时,一直WA,最后发现竟是当输入的图一个顶点也没有的时候,必须输出Yes……无语啊,这种题目真是没意思,特殊情况都不说明,害死人!

/*
* hdu1272/linux.c
* Created on: 2011-8-4
* Author : ben
*/
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
#include
<math.h>

#define MAXN 100005

int parent[MAXN];
int exist[MAXN];

int myfind(int i) {
int r = i;
while (parent[r] != r) {
r
= parent[r];
}
return r;
}

void merge(int a, int b) {
if (a < b) {
parent[b]
= a;
}
else {
parent[a]
= b;
}
}

void work() {
int a, b, flag, edge, vertex, i;
scanf(
"%d%d", &a, &b);
while (a != -1 && b != -1) {
if(a == 0 && b == 0) {
puts(
"Yes");
scanf(
"%d%d", &a, &b);
continue;
}
flag
= 1;
edge
= vertex = 0;
memset(exist,
0, sizeof(exist));
for (i = 0; i < MAXN; i++) {
parent[i]
= i;
}
while (a != 0 && b != 0) {
if (!exist[a]) {
exist[a]
= 1;
vertex
++;
}
if (!exist[b]) {
exist[b]
= 1;
vertex
++;
}
edge
++;
if (myfind(a) == myfind(b)) {
flag
= 0;
}
else {
merge(myfind(a), myfind(b));
}
scanf(
"%d%d", &a, &b);
}
if (flag && (vertex == edge + 1)) {
puts(
"Yes");
}
else {
puts(
"No");
}
scanf(
"%d%d", &a, &b);
}
}

int main() {
#ifndef ONLINE_JUDGE
freopen(
"data.in", "r", stdin);
#endif
work();
return 0;
}

你可能感兴趣的:(HDU)