题目:
http://ac.jobdu.com/problem.php?pid=1481
http://acm.nyist.net/JudgeOnline/problem.php?pid=129
http://poj.org/problem?id=1308
http://acm.hdu.edu.cn/showproblem.php?pid=1272
题目意思就是判断一些给定的支点构成的树是不是一颗合法的树,
判断是不是一颗合法的树如下:
1、该树只有一个根节点
2、不存在环
对于上述两种情况采用如下进行判断:
1、定义一个标识符,当这个点出现,并且父节点和自己相同的节点个数大于1,表示有多个根节点,反之不是
2、在寻找父节点时,这两点指向同一个父节点,则出现了回路
nyoj 和poj 直接使用下代码就ok ,hdu上面相应的改改(代码最后一个)一开始一直RE 后来看看了题目 是10^5,(纠结了)
/*hdu 1272*/ #include<stdio.h> #include <string.h> #define N 100005 int f[N]; int a[N]; int flag = 1; int max = 0; void init() { memset(a,0,sizeof(a)); memset(f,0,sizeof(f)); max = 0; flag = 1; int i; for(i = 0; i <= N; i++) f[i] = i; } int find_f(int x) { if(x == f[x]) return x; else return f[x] = find_f(f[x]); } void add(int x,int y) { int fx = find_f(x); int fy = find_f(y); if(fx != fy) f[fy] = fx; else flag = 0; } int main() { int x,y; int fx,fy; init(); while(1) { scanf("%d%d",&x,&y); if(x == -1 && y == -1) break; if(x ==0 && y == 0) { int t = 0; if(flag) { int i; for(i = 0; i <= max; i++) if(a[i] == 1) { if(i == f[i]) t++; } } if(t > 1) flag = 0; if(flag) printf("Yes\n"); else printf("No\n"); init(); } else { if(flag) { max = max > (x > y ? x : y) ? max: (x > y ? x : y) ; add(x,y); a[x] = 1; a[y] = 1; } } } return 0; }
/*** 判断是否有环,当他们的父相同则会出现环,然后是判断是否还有孤立的点(树存在) */ #include<stdio.h> #include <string.h> #define N 10005 int f[N]; int a[N]; int in[N]; int flag = 1; int max = 0; void init() { memset(a,0,sizeof(a)); memset(in,0,sizeof(in)); memset(f,0,sizeof(f)); max = 0; flag = 1; int i; for(i = 0; i <= N; i++) f[i] = i; } int find_f(int x) { if(x == f[x]) return x; else return find_f(f[x]); } int main() { int x,y; int fx,fy; init(); int tt = 0; while(1) { scanf("%d%d",&x,&y); if(x == -1 && y == -1) break; if(x == 0 && y == 0) { int t = 0,i; if(flag) { for(i = 0; i <= max; i++) if(a[i] == 1) { if(i == f[i]) t++; } } if(t > 1) flag = 0; tt++; if(flag) printf("Case %d is a tree.\n",tt); else printf("Case %d is not a tree.\n",tt); init(); } else { a[x] = 1; a[y] = 1; in[y]++; max = max > (x > y ? x : y) ? max : (x > y ? x : y); if(in[y] > 1) { flag = 0; } if(flag == 1) { fx = find_f(x); fy = find_f(y); //printf("%d %d\n",fx,fy); if(fx != fy) f[fy] = fx; else flag = 0; } } } return 0; }
最后跟新贴一个javaAC全部的代码
1 import java.util.Scanner; 2 3 //判断是否是树 4 5 public class Main { 6 private static My_Tree[] tree = new My_Tree[10007] ;//定义对象数组保存输入的值 7 private static boolean Main_flag = false; 8 private static int t = 1; 9 10 public static void init(){ 11 for(int i = 0; i < 10007; i++) 12 tree[i] =new My_Tree(i,i,0,false); 13 Main.Main_flag = false; 14 } 15 public static int find_f(int x){ 16 if(tree[x].getN() == tree[x].getF()) 17 return tree[x].getF(); 18 else 19 return find_f(tree[x].getF()); 20 } 21 public static void main(String[] args) { 22 Scanner cin = new Scanner(System.in); 23 int n,m; 24 Main.init(); 25 while(true){ 26 n = cin.nextInt(); 27 m = cin.nextInt(); 28 // System.out.println(Main.Main_flag); 29 if(n == -1 && m == -1) 30 break; 31 if(n == 0 && m == 0){ 32 int t_tmp = 0; 33 if(!Main.Main_flag){ 34 for(int i = 0; i < 10007; i++) 35 if(tree[i].isFlag()){ 36 // tree[i].print(); 37 if(tree[i].getF() == tree[i].getN()) 38 t_tmp++; 39 } 40 } 41 if(t_tmp > 1) 42 Main.Main_flag = true; 43 if(!Main.Main_flag) 44 System.out.println("Case "+(Main.t++)+" is a tree."); 45 else 46 System.out.println("Case "+(Main.t++)+" is not a tree."); 47 Main.init(); 48 } 49 else{ 50 int fx,fy; 51 52 tree[n].setFlag(true); 53 tree[m].setFlag(true); 54 tree[m].setIn(tree[m].getIn()+1); 55 if(tree[m].getIn() > 1) 56 Main.Main_flag = true; 57 58 if(!Main.Main_flag){ 59 fx = Main.find_f(n); 60 fy = Main.find_f(m); 61 if(fx != fy) 62 tree[m].setF(fx); 63 else 64 Main.Main_flag = true; 65 } 66 } 67 } 68 } 69 70 } 71 72 class My_Tree{ 73 private int n,f,in; 74 private boolean flag; 75 public My_Tree(int n, int f, int in, boolean flag) { 76 super(); 77 this.n = n; 78 this.f = f; 79 this.in = in; 80 this.flag = flag; 81 } 82 public My_Tree() { 83 super(); 84 } 85 86 public int getN() { 87 return n; 88 } 89 public void setN(int n) { 90 this.n = n; 91 } 92 public int getF() { 93 return f; 94 } 95 public void setF(int f) { 96 this.f = f; 97 } 98 public int getIn() { 99 return in; 100 } 101 public void setIn(int in) { 102 this.in = in; 103 } 104 public boolean isFlag() { 105 return flag; 106 } 107 public void setFlag(boolean flag) { 108 this.flag = flag; 109 } 110 public void print(){ 111 System.out.println("--->"+n+f+in+flag); 112 } 113 } 114 /************************************************************** 115 Problem: 1481 116 User: yangyin1217 117 Language: Java 118 Result: Accepted 119 Time:790 ms 120 Memory:105768 kb 121 ****************************************************************/