程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU More is better

HDU More is better

編輯:C++入門知識

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?在CODE上查看代碼片派生到我的代碼片
  1. [cpp] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片
    1. 簡單的並查集題目:

      WA了好幾次的代碼:

      [html] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片
      1. #include
      2. #include
      3. using namespace std;
      4. int father[10000010];
      5. int num[10000010];
      6. int Max;
      7. int Find(int x)
      8. {
      9. if(x!=father[x])
      10. father[x]=Find(father[x]);
      11. return father[x];
      12. }
      13. void merset(int x,int y)
      14. {
      15. int q=Find(x);
      16. int p=Find(y);
      17. if(q!=p)
      18. {
      19. num[y]=num[x]+num[y];
      20. num[x]=num[y];
      21. if(num[x]>Max)
      22. Max=num[x];
      23. if(q>p)
      24. father[q]=p;
      25. else
      26. father[p]=q;
      27. }
      28. }
      29. void Init()
      30. {
      31. for(int i=1;i<=10000000;i++)
      32. {
      33. father[i]=i;
      34. num[i]=1;
      35. }
      36. }
      37. int main()
      38. {
      39. int n;
      40. while(~scanf("%d",&n))
      41. {
      42. Init();
      43. Max=num[1];
      44. while(n--)
      45. {
      46. int x,y;
      47. scanf("%d%d",&x,&y);
      48. merset(x,y);
      49. }
      50. cout< }
      51. return 0;
      52. }

        之後通過畫圖,單步跟蹤,還有請教學長終於找出了錯誤來源:


        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?在CODE上查看代碼片派生到我的代碼片
        1. #include
        2. #include
        3. using namespace std;
        4. int father[10000010];
        5. int num[10000010];
        6. int Max;
        7. int Find(int x)
        8. {
        9. if(x!=father[x])
        10. father[x]=Find(father[x]);
        11. return father[x];
        12. }
        13. void merset(int x,int y)
        14. {
        15. int q=Find(x);
        16. int p=Find(y);
        17. if(q!=p)
        18. {
        19. num[p]=num[q]+num[p];
        20. num[q]=num[p];
        21. if(num[q]>Max)
        22. Max=num[q];
        23. if(q>p)
        24. father[q]=p;
        25. else
        26. father[p]=q;
        27. }
        28. }
        29. void Init()
        30. {
        31. for(int i=1;i<=10000000;i++)
        32. {
        33. father[i]=i;
        34. num[i]=1;
        35. }
        36. }
        37. int main()
        38. {
        39. int n;
        40. while(~scanf("%d",&n))
        41. {
        42. Init();
        43. Max=num[1];
        44. while(n--)
        45. {
        46. int x,y;
        47. scanf("%d%d",&x,&y);
        48. merset(x,y);
        49. }
        50. cout< }
        51. return 0;
        52. } [cpp] view plaincopyprint?
          1. 特別感謝浩浩學長的耐心指導~




  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved