标签:protected 就是 cte 合并 递归 play 还需要 ref turn
题目链接:http://poj.org/problem?id=1988
1 #include<iostream> 2 #include<stdio.h> 3 #include<cstring> 4 #include<cmath> 5 #include<vector> 6 #include<stack> 7 #include<map> 8 #include<set> 9 #include<list> 10 #include<queue> 11 #include<string> 12 #include<algorithm> 13 #include<iomanip> 14 using namespace std; 15 16 struct node 17 { 18 int parent; 19 int date; 20 }; 21 22 int * total; 23 int * under; 24 25 class DisJoinSet 26 { 27 protected: 28 int n; 29 30 node * tree; 31 public: 32 DisJoinSet(int n); 33 ~DisJoinSet(); 34 void Init(); 35 int Find(int x); 36 void Union(int x,int y); 37 }; 38 39 DisJoinSet::DisJoinSet(int n) 40 { 41 this->n = n; 42 tree = new node[n+2]; 43 total = new int[n+2]; 44 under = new int[n+2]; 45 Init(); 46 } 47 DisJoinSet::~DisJoinSet() 48 { 49 delete[] under; 50 delete[] total; 51 delete[] tree; 52 } 53 54 void DisJoinSet::Init() 55 { 56 for(int i = 1;i <= n ;i ++) 57 { 58 tree[i].date = i; 59 tree[i].parent = i; 60 total[i] = 1; 61 under[i] = 0; 62 } 63 } 64 int DisJoinSet::Find(int x) 65 { 66 //int temp = tree[x].parent; 67 if(x != tree[x].parent) 68 { 69 int par = Find(tree[x].parent); 70 under[x] += under[tree[x].parent];//把父亲结点下面的个数加到自己头上 71 tree[x].parent = par; 72 return tree[x].parent; 73 } 74 else 75 { 76 return x; 77 } 78 } 79 80 void DisJoinSet::Union(int x,int y) 81 { 82 int pa = Find(x); 83 int pb = Find(y); 84 if(pa == pb)return ; 85 else 86 { 87 tree[pa].parent = pb;//x的根变为y的根 即把x所在的堆放在y所在的堆上面 88 under[pa] = total[pb];//pa下的数量即原来y所在栈里的元素total 89 total[pb] += total[pa];//更新y的totoal 90 } 91 } 92 93 int main() 94 { 95 int p; 96 while(scanf("%d",&p) != EOF) 97 { 98 if(p == 0)break; 99 DisJoinSet dis(p); 100 char s1[2]; 101 for(int i = 0 ;i < p ;i++) 102 { 103 104 int s2; 105 int s3; 106 scanf("%s",s1); 107 if(s1[0] == ‘M‘) 108 { 109 scanf("%d%d",&s2,&s3); 110 int pa = dis.Find(s2); 111 int pb = dis.Find(s3); 112 if(pa != pb) 113 { 114 dis.Union(s2,s3); 115 } 116 } 117 if(s1[0] == ‘C‘) 118 { 119 scanf("%d",&s2); 120 dis.Find(s2); 121 cout<<under[s2]<<endl; 122 } 123 } 124 dis.~DisJoinSet(); 125 } 126 return 0; 127 }
标签:protected 就是 cte 合并 递归 play 还需要 ref turn
原文地址:https://www.cnblogs.com/ygsworld/p/11141079.html