题目1444:More is better

 

时间限制:3 秒

内存限制:100 兆

特殊判题:

提交:1362

解决:640

题目描述:

Mr Wang wants some boys to help him with a project. Because the project is rather complex, the more boys come, the better it will be. Of course there are certain requirements.Mr Wang selected a room big enough to hold the boys. The boy who are not been chosen has to leave the room immediately. There are 10000000 boys in the room numbered from 1 to 10000000 at the very beginning. After Mr Wang's selection any two of them who are still in this room should be friends (direct or indirect), or there is only one boy left. Given all the direct friend-pairs, you should decide the best way.

输入:

The first line of the input contains an integer n (0 ≤ n ≤ 100 000) - the number of direct friend-pairs. The following n lines each contains a pair of numbers A and B separated by a single space that suggests A and B are direct friends. (A ≠ B, 1 ≤ A, B ≤ 10000000)

输出: 

The output in one line contains exactly one integer equals to the maximum number of boys Mr Wang may keep.


总感觉这个Mr Wang是个恋童癖有木有==||


 

  1 #include <iostream>

  2 

  3 using namespace std;

  4 

  5  

  6 

  7  

  8 

  9  

 10 

 11 struct LNode

 12 

 13 {

 14 

 15    LNode * lchild,*rchild;

 16 

 17    int data;

 18 

 19 };

 20 

 21  

 22 

 23  

 24 

 25 int Tree[10000001];

 26 

 27 int sum[10000001];

 28 

 29  

 30 

 31 int getroot(int x)

 32 

 33 {

 34 

 35     if(Tree[x]==-1) return x;

 36 

 37 else return getroot(Tree[x]);

 38 

 39 }

 40 

 41  

 42 

 43  

 44 

 45  

 46 

 47 int main()

 48 

 49 {

 50 

 51        int n;

 52 

 53    while(cin>>n)

 54 

 55    {

 56 

 57      int i;

 58 

 59  for(i=0;i<10000001;i++)

 60 

 61  {

 62 

 63     Tree[i]=-1;

 64 

 65 sum[i]=1;

 66 

 67  }

 68 

 69  

 70 

 71  

 72 

 73  

 74 

 75  for(i=0;i<n;i++)

 76 

 77  {

 78 

 79     int a,b;

 80 

 81 cin>>a>>b;

 82 

 83 a=getroot(a);

 84 

 85 b=getroot(b);

 86 

 87 if(a!=b)

 88 

 89 {

 90 

 91    Tree[a]=b;

 92 

 93    sum[b]+=sum[a];

 94 

 95 }

 96 

 97  }

 98 

 99  

100 

101  int temp=1;

102 

103  for(i=0;i<10000001;i++)

104 

105  {

106 

107    if(sum[i]>temp) temp=sum[i];

108 

109  }

110 

111  

112 

113  cout<<temp<<endl;

114 

115  

116 

117    }

118 

119         return 0;

120 

121 }

 

 

你可能感兴趣的:(more)