1. 程式人生 > >1856 More is better(水+並查集)

1856 More is better(水+並查集)

More is better

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others)
Total Submission(s): 26832    Accepted Submission(s): 9611


Problem 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.
Author [email protected]
Source
Recommend lcy   |   We have carefully selected several similar problems for you:  
1232
 1325 1879 1863 1875 

並查集的裸題,題目大意:老師想讓幾個男同學來做一個專案,這些同學必須直接或間接的為朋友關係,求最多招幾個男同學。

思路:這裡集合的數量不確定,目的是求每個集合的元素數量,再加一個num陣列記錄每個集合的元素,初始為1,合併時相加即可。

試了一下不同輸入方式的時間,第一行為cin加取消同步的情況,第二行為cin不加取消同步,第三種是scanf加萬能標頭檔案,第四種是scanf不加萬能標頭檔案。

可以看出第二種直接超時,最優的方案還是寫檔名加scanf


附上AC程式碼:

#include<bits/stdc++.h>

using namespace std;
const int maxn=10000000+5;
int par[maxn],num[maxn];
int n,f1,f2;
int maxd,mind,maxcnt;

void init()
{
    for(int i=0;i<maxn;i++)
        par[i]=i,num[i]=1;
}

int find(int a)
{
    if(a==par[a]) return a;
    return par[a]=find(par[a]);
}

void unite(int a,int b)
{
    a=find(a);
    b=find(b);
    if(a==b)return ;
    par[a]=b;
    num[b]+=num[a];
}

int main()
{
    while(~scanf("%d",&n))
    {
        if(n==0){printf("1\n");continue;}
        init();
        maxd=INT_MIN;
        mind=INT_MAX;
        maxcnt=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&f1,&f2);
            maxd=max(maxd,max(f1,f2));
            mind=min(mind,min(f1,f2));
            unite(f1,f2);
        }
        for(int i=mind;i<=maxd;i++)
        {
                maxcnt=max(maxcnt,num[i]);
        }
        printf("%d\n",maxcnt);
    }
    return 0;
}

相關推薦

1856 More is better(+)

More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others) Total Submission(s): 26832    Acc

hdoj-1856-More is better

sub ont max ash cer careful gin search std More is better Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Ot

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 ther

More is better

#include<bits/stdc++.h> using namespace std; int Father[10000]; int FindFather(int x) { return Father[x] < 0 ? x : FindFather(Father[x]); }

hdu--1856 More is better

cstring float port one color iat win rec fin Mr Wang wants some boys to help him with a project. Because the project is rather complex, t

Hdoj 1856.More is better 題解

Problem 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.

【HDU 1856More is better (裸+記錄秩)

gin main 並查集 keep better sele scan ted dia Description Mr Wang wants some boys to help him with a project. Because the project is rather

臺州 OJ 2649 More is better

ould map get rect pac 路徑壓縮 esp after ace 描述 Mr Wang wants some boys to help him with a project. Because the project is rather complex,

More is better(

Description Mr Wang wants some boys to help him with a project. Because the project is rather complex, the more boys come, the bett

More Is better-

problem election gin one dia second complex ace href 原題鏈接:https://vjudge.net/problem/11137/origin Mr Wang wants some boys to help him wit

Codeforces Round #423 (Div. 2) C 思維, 或 線段樹 D 樹構造,

closed alt pda memset sed () back ref cup Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) C. String Reconstruction 思維,並查

51nod 1307 繩子與重物(了一發)

spa ons ios set const dfs scanf sin 並查集 http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1307 題意: 思路: 可以直接二分答案,然後dfs

HDU 1856

size content 最大 com ont ava ide tor equals http://acm.hdu.edu.cn/showproblem.php?pid=1856 More is better Time Limit: 5000/1000 MS (Jav

POJ - 1308 Is It A Tree?【

ges sin 分享 ive ostream rep test case 圖片 air A tree is a well-known data structure that is either empty (null, void, nothing) or is a set

hdu 4496 反向 題 D-City

ssi fat include connected fin lines bottom printf conn 覺得這道題以後可以和優先隊列結合起來 嗯 就是說依次去掉前n條路求連通塊數量 處理的時候 只要每次merge發現父親不相等 然後進到裏面合並的時候 num--

hdu1232

#include <stdio.h> #include <stdlib.h> int n,m; int father[1000],rank[1000]; int count; int find(int x) { if(father[x]==x) retu

------M - 小希的迷宮/N - Is It A Tree?

上次Gardon的迷宮城堡小希玩了很久(見Problem B),現在她也想設計一個迷宮讓Gardon來走。但是她設計迷宮的思路不一樣,首先她認為所有的通道都應該是雙向連通的,就是說如果有一個通道連通了房間A和B,那麼既可以通過它從房間A走到房間B,也可以通過它從房間B走到房間A,為了提高難度

HDU-1325-Is It A Tree?(

Problem Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directe

HDU - 1856 ,尋找最大集合元素個數

#include<stdio.h> #include<iostream> #include<algorithm> using namespace std; int fa[10000000]; int mark[10000000]; int findroot(int

hdu 1325/poj 1308 Is It A Tree?

A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between