日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

More is better

發布時間:2024/10/12 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 More is better 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目描述:

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.

樣例輸入:
4 1 2 3 4 5 6 1 6 4 1 2 3 4 5 6 7 8
樣例輸出:
4 2


#include<iostream> #include<stdio.h> #include<algorithm> #define N 10000001 //將一個數定義在預處理部分 using namespace std; int path[N]; int sum[N]; int findroot(int a){int temp=a;while (path[a] != -1){a=path[a];}int temp2;//改進,使樹的高度變矮,寬度增加,方便找根 while (path[temp]!= -1){temp2=path[temp];path[temp]=a;temp=temp2;}return a; }int main (){int n;while (cin>>n){for (int i=1;i<=N;i++){path[i]=-1; sum[i]=1;}int ans=0;int a,b;while (n--){cin >>a>>b;a=findroot(a);b=findroot(b);if (a!=b){path[a]=b;sum[b]+=sum[a];sum[a]=0;} }for (int i=1;i<=N;i++){if (sum[i] > ans)ans =sum[i];}cout<<ans<<endl;} return 0; }

?

跟上一道暢通工程幾乎一樣

加了一個在合并集合時同時合并(加)元素個數的功能

ps:不能想著在找根的過程中數元素個數,因為不一定是葉子,數出來的不對,

轉載于:https://www.cnblogs.com/yexiaoqi/p/7232679.html

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的More is better的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。