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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

【HDU - 1856】 More is better(并查集)(还需要空间优化。。)

發布時間:2023/12/10 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【HDU - 1856】 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.?
InputThe 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)OutputThe 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 8Sample 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.

解題報告:????

????裸一個并查集,最后查的時候記錄一下最大值就好了。

ac代碼:

#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> const int MAX = 10000000 + 5; using namespace std;int f[MAX]; int sum[MAX]; int getf(int v) {return f[v]==v? v : f[v]=getf( f[v] ) ; } void merge(int u,int v) {int t1,t2;t1=getf(u);t2=getf(v);if(t1!=t2) {f[t2]=t1;}} void init() {for(int i = 0; i<MAX; i++) {f[i]=i;sum[i]=0;} } int maxx,u,v; int main() {int n;while(~scanf("%d",&n)) {if(n==0) {printf("1\n");continue; }init();maxx=0;while(n--) {scanf("%d %d",&u,&v);merge(u,v);if(u>maxx||v>maxx) maxx=max(u,v);}for(int i = 1; i<=maxx; i++) {sum[getf(f[i])] ++;}printf("%d\n",*max_element(sum+1,sum+maxx+1));}return 0 ;}

ac代碼2:(空間優化了一點點,只開了一個數組)

#include<cstdio> #include<algorithm> #include<cstring> using namespace std;int pre[10000005]; int fin(int x) {if(pre[x]==x){return x;}else{return pre[x]=fin(pre[x]);} }void join(int x,int y) {int t1=fin(x);int t2=fin(y);if(t1!=t2){pre[t1]=t2;} }int main() {int n;while(~scanf("%d",&n)){if(n==0)//特判,被坑在這了{printf("1\n");continue;}int maxn=0,a,b;for(int i=0;i<=10000004;i++){pre[i]=i;}for(int i=0;i<n;i++){scanf("%d%d",&a,&b);join(a,b);if(max(a,b)>maxn)//優化技巧{maxn=max(a,b);}}int sum=0,sum1=0;for(int i=1;i<=maxn;i++){sum1=0;if(pre[i]==i)for(int j=1;j<=maxn;j++){if(i==fin(j)){sum1++;}}sum=max(sum,sum1);//更細最大值}printf("%d\n",sum);}return 0; }

總結:

????n==0時別忘特殊處理一下!!!

????有個地方時間優化的十分正確 ?maxx的記錄。

????還可以再時間優化:merge的時候,在合并點的同時(t2合并入t1),sum[t1]+=sum[t2];(即把t2的成員也都加入t1)

????有個缺點 占內存太多啦!有沒有更好的方法?

總結

以上是生活随笔為你收集整理的【HDU - 1856】 More is better(并查集)(还需要空间优化。。)的全部內容,希望文章能夠幫你解決所遇到的問題。

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