More is better
Time Limit:1000MS
Memory Limit:102400KB
64bit
IO Format:%I64d & %I64u
Submit Status
Description
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.
Input
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)
Output
The output in one line contains exactly one integer equals to the maximum number of boys Mr Wang may keep.
Sample Input
4
1 2
3 4
5 6
1 6
4
1 2
3 4
5 6
7 8
Sample Output
4
2
Hint
A and B are friends(direct or indirect), B and C are friends(direct or indirect),
then A and C are also friends(indirect).
In the first sample {1,2,5,6} is the result.
In the second sample {1,2},{3,4},{5,6},{7,8} are four kinds of answers.
[cpp] view
plaincopyprint?
-
[cpp] view
plaincopyprint?
-
簡單的並查集題目:
WA了好幾次的代碼:
[html] view
plaincopyprint?
-
#include
-
#include
-
using namespace std;
-
int father[10000010];
-
int num[10000010];
-
int Max;
-
int Find(int x)
-
{
-
if(x!=father[x])
-
father[x]=Find(father[x]);
-
return father[x];
-
-
}
-
void merset(int x,int y)
-
{
-
int q=Find(x);
-
int p=Find(y);
-
if(q!=p)
-
{
-
num[y]=num[x]+num[y];
-
num[x]=num[y];
-
if(num[x]>Max)
-
Max=num[x];
-
if(q>p)
-
father[q]=p;
-
else
-
father[p]=q;
-
}
-
}
-
void Init()
-
{
-
for(int i=1;i<=10000000;i++)
-
{
-
father[i]=i;
-
num[i]=1;
-
}
-
}
-
int main()
-
{
-
int n;
-
while(~scanf("%d",&n))
-
{
-
-
Init();
-
Max=num[1];
-
while(n--)
-
{
-
int x,y;
-
scanf("%d%d",&x,&y);
-
merset(x,y);
-
}
-
cout<
}
-
-
-
return 0;
-
}
之後通過畫圖,單步跟蹤,還有請教學長終於找出了錯誤來源:
x y 可以理解成當前要合並的兩個集合,但是他們又不一定不是兩個樹根 P Q則是find路徑壓縮後他們的樹根(可能是他們本身也可能是別的)他們兩個還有子樹指向它們,加X Y的數量 max就只能是他們子樹的數量加起來 而不是他們合並的那個最大數量。
每次都只是更新了某個子樹上的全部數量
(第一次是准確的)但是根樹上的節點沒有再次更新。。。每次子樹變動 即合並的子節點不同 就會有損失。
試下1 2 2 3 1 4 第一次更新num[1]=num[2]=2;第二次就是num[2]=num[3]=3;但此次num[1]還是2 第三次 就是num[1]=num[4]=3 ,損失了一個。。。
如果一直更新根數 就是num[1]=num[2]=2,num[1]=num[3]=3;num[1]=num[4]=4。
正確代碼:
[cpp] view
plaincopyprint?
-
#include
-
#include
-
using namespace std;
-
int father[10000010];
-
int num[10000010];
-
int Max;
-
int Find(int x)
-
{
-
if(x!=father[x])
-
father[x]=Find(father[x]);
-
return father[x];
-
-
}
-
void merset(int x,int y)
-
{
-
int q=Find(x);
-
int p=Find(y);
-
if(q!=p)
-
{
-
num[p]=num[q]+num[p];
-
num[q]=num[p];
-
if(num[q]>Max)
-
Max=num[q];
-
if(q>p)
-
father[q]=p;
-
else
-
father[p]=q;
-
}
-
}
-
void Init()
-
{
-
for(int i=1;i<=10000000;i++)
-
{
-
father[i]=i;
-
num[i]=1;
-
}
-
}
-
int main()
-
{
-
int n;
-
while(~scanf("%d",&n))
-
{
-
-
Init();
-
Max=num[1];
-
while(n--)
-
{
-
int x,y;
-
scanf("%d%d",&x,&y);
-
merset(x,y);
-
}
-
cout<
}
-
-
-
return 0;
-
}
[cpp] view
plaincopyprint?
-
特別感謝浩浩學長的耐心指導~