HDOJ-1856 More is better&&POJ-1611(并查集)

More is better

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


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.


  明明就是判断所有树里面点的个数的最大值,为什么单单线性扫一遍会wa,在合并两子集的函数中累加可以ac,是我理解错了吗,求解答

ac代码:

#include
#include
#include
#include
using namespace std;

const int maxn = 10000005;
int f[maxn];
int sum[maxn];
void Init()
{
    for(int i = 1;i <= maxn;i++)
    {
        f[i] = i;
        sum[i] =1;
    }
}

int getf(int t)
{
    if(f[t] == t)
        return t;
    else
    {
        f[t] = getf(f[t]);//状态压缩
        return f[t];
    }
}
void merge(int u,int v)
{
    int t1 = getf(u);
    int t2 = getf(v);
    if(t1 != t2)
    {
        f[t2] = t1;
        //靠左原则
        sum[t1] += sum[t2];
    }
}
int main()
{
    int n,x,y;
    while(~scanf("%d",&n))
    {
        if(n == 0)
        {
            cout<<1<


wa代码:

#include
#include
#include
#include

using namespace std;

const int maxn = 10000005;
int f[maxn];
int sum[maxn];
void Init()
{
    for(int i = 1;i <= maxn;i++)
        f[i] = i;
    memset(sum,0,sizeof(sum));
}

int getf(int t)
{
    if(f[t] == t)
        return t;
    else
    {
        f[t] = getf(f[t]);//状态压缩
        return f[t];
    }
}
void merge(int u,int v)
{
    int t1 = getf(u);
    int t2 = getf(v);
    if(t1 != t2)
    {
        f[t2] = t1;
        //靠左原则
    }
}
int main()
{
    int n,x,y;
    while(~scanf("%d",&n))
    {
        if(n == 0)
        {
            cout<<1<

POJ-1611

#include
#include
using namespace std;
int n,m,k;
int f[30005],sum[30005];
int getf(int u)
{
    if(u == f[u])
        return u;
    else
    {
        f[u] = getf(f[u]);
        return f[u];
    }
}
void merge(int u,int v)
{
    u = getf(u);
    v = getf(v);
    if(u != v)
    {
        f[v] = u;
        sum[u] += sum[v];
        //靠左
    }
}
int main()
{
    int p,q;
    while(~scanf("%d%d",&n,&m)&&(n||m))
    {
        for(int i = 0;i < n;i++)
        {
            f[i] = i;
            sum[i] = 1;
        }

        for(int i = 1;i <= m;i++)
        {
            scanf("%d",&k);
            k--;
            scanf("%d",&p);
            while(k--)
            {
                scanf("%d",&q);
                merge(p,q);
            }
        }
        printf("%d\n",sum[getf(0)]);//这里的getf(0)不能替换
                                    //成f[0],因为此时还未进行路径压缩
    }
    return 0;
}


你可能感兴趣的:(图论,数据结构)