标签:des style blog http java color
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24824 Accepted Submission(s): 7618
不想说什么了,我只是把提交poj1308的代码改了一下输出就过了。
思路:判断是否是一棵树,若是树,则就可以。1.树的入度为1出根节点外;2.只能有一个根节点,因为需要的是一棵树;3.不能有环。
#include <stdio.h> struct node { int use; int father; int num; }; struct node Node[100005]; void MakeSet(int n) { for(int i = 0; i<=n; i++) { Node[i].use = 0; Node[i].father = i; Node[i].num = 0; } } int FindSet(int x) { if(x != Node[x].father) { Node[x].father = FindSet(Node[x].father); } return Node[x].father; } void UnionSet(int a, int b) { int x = FindSet(a); int y = FindSet(b); if(x == y) return ; else Node[y].father = x; } int main() { int a, b; int k = 1, flag = 1, mark; MakeSet(100000); while(scanf("%d%d", &a, &b)!=EOF && (a!=-1 || b!=-1)) { if(a==0 && b==0) { mark = 0; for(int i = 1; i<=100000; i++) { if(Node[i].use==1 && Node[i].num>1) { flag = 0; break ; } if(Node[i].use==1 && Node[i].father==i) mark++; } if(mark > 1) flag = 0; if(flag) printf("Yes\n"); else printf("No\n"); MakeSet(100000); flag = 1; } else if(a == b || (a!=b && FindSet(a)==FindSet(b))) //多了一个这个判断,就是自己不能为自己的父亲节点,且不能构成环 { flag = 0; } else { Node[a].use = 1; Node[b].use = 1; Node[b].num++; UnionSet(a, b); } } return 0; }
标签:des style blog http java color
原文地址:http://www.cnblogs.com/fengxmx/p/3835693.html