Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

注意输入输出的空格和换行!!!别问我怎么知道的

Posted by 562786621 at 2021-07-22 14:44:30 on Problem 2524
#include <iostream>
#include <cstdio>
#define N 50010
using namespace std;

struct node
{
    int parent;
    int Rank;
}t[N];

void make_set(int n)
{
    for(int i=1;i<=n;i++)
    {
        t[i].parent=i;
        t[i].Rank=1;
    }
}

int find_(int x)
{
    if(x!=t[x].parent)
    {
        t[x].parent=find_(t[x].parent);
    }
    return t[x].parent;
}

void Union(int a,int b)
{
    int pa,pb;
    pa=find_(a);
    pb=find_(b);
    if(pa!=pb)
    {
        t[pb].parent=pa;
        t[pa].Rank+=t[pb].Rank;
    }
}

int main()
{
    int flag=0;
    while(1)
    {
        int m,n;
        scanf("%d%d",&n,&m);
        make_set(n);
        if(n==0&&m==0)break;
        int ans=n;
        int tem=0;
        while(m--)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            if(find_(a)!=find_(b))
            {
                Union(a,b);
            }
        }
        for(int i=1;i<=n;i++)
        {
            if(t[i].Rank>1&&find_(i)==i)
            {
                tem+=(t[i].Rank-1);
            }
        }
        ans-=tem;
        flag++;
        printf("Case %d: %d\n",flag,ans);
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator