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 201073609 at 2011-05-07 20:34:47 on Problem 2524
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
int num;
using namespace std;

struct students
{
    int depth;
    int brief;
}student[50001];
int find(int x)
{
    if(student[x].brief!=x) student[x].brief=find(student[x].brief);
    return student[x].brief;
}
void merge(int p,int q)
{
    if(find(p)==find(q)) return;
    num--;
    student[q].brief=find(p);
}

int main()
{
    int n,m,i,j,time=1;
    while(1)
    {
        num=0;
        scanf("%d%d",&n,&m);
        if(n==0 && m==0) break;
        num=n;
        for(i=1;i<=n;i++)
        {
            student[i].brief=i;
        }
        while(m--)
        {
            scanf("%d%d",&i,&j);
            merge(i,j);
        }
        printf("Case %d:%d\n",time,num);
        time++;
    }
    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