hrbust 哈理工oj 2016 势力较量 (并查集)

来源:互联网 发布:淘宝买的邮票是真的吗 编辑:程序博客网 时间:2024/05/05 13:20

势力较量Time Limit: 1000 MSMemory Limit: 32768 KTotal Submit: 123(48 users)Total Accepted: 70(46 users)Rating: Special Judge: NoDescription

在战火纷乱的古代有许多小国家。在这些小国家中,就会有一些强大的,一些弱小的。势力大的就会吞并势力小的,

形成更大的势力国家。现在给出你一些目前的国家势力关系,你能预算出以后的局势吗?


为了简化问题,给每个国家编号,像“国家1”, “国家2”......“国家N”

国家的较量首先从人数上进行比较,人数多的能打赢人数少的。如果两个国家的人数相同,就根据国家头目的编号来判断,

我们假设编号大的国家能打得过编号小的国家。

当两个国家相遇的时候,就会有一场打斗,且一定要分出胜负,输的就会归顺于赢得, 形成一个新的国家。Input多组测试数据,处理到文件结束。对于每组数据:
第一行两个整数N,M。N为国家个数,M为关系数目。
接下来M行,每行两个整数u, v。表示国家u,国家v相遇,有一场打斗。
然后试一个整数Q,表示Q个询问。
接下来Q行,每行一个整数S。
(N<100, M < 100, Q < 100)OutputCase k:
对于每个询问,输出两个整数,
第一个整数表示S所在国家的头目,第二个整数表示S所在国家里边一共有多少个小国家。Sample Input5 3
1 2
1 3
4 5
2
1
5


5 4
1 2
1 3
1 4
1 5
4
2
3
4
5Sample OutputCase 1:
2 3
5 2
Case 2:
2 5
2 5
2 5
2 5
Source2014 Winter Holiday Contest 4#include<stdio.h>
#include<string.h>
using namespace std;
struct data
{
    int num,pos;
} pre[103];
int find(int x)
{
    if(pre[x].pos!=x)
    {
        return find(pre[x].pos);
    }
    return x;
}
int main()
{
    int n,m;
    int t=0;
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=1; i<=n; i++)
        {
            pre[i].pos=i;
            pre[i].num=1;
        }
        for(int i=1; i<=m; i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            int x=find(a);
            int y=find(b);
            if(x==y)
                continue;
            if(pre[x].num>pre[y].num)
            {
                pre[y].pos=x;
                 pre[x].num+=pre[y].num;
            }
            if(pre[x].num<pre[y].num)
            {
                pre[x].pos=y;
                pre[y].num+=pre[x].num;
            }
            if(pre[x].num==pre[y].num)
            {
                if(pre[x].pos<pre[x].pos)
                {
                    pre[y].pos=x;
                    pre[x].num+=pre[y].num;
                }
                else
                {
                    pre[x].pos=y;
                    pre[y].num+=pre[x].num;
                }
            }
        }
        int q;
        scanf("%d",&q);
        printf("Case %d:\n",++t);
        while(q--)
        {
            int z;
            scanf("%d",&z);
            int asd=find(z);
            printf("%d %d\n",asd,pre[asd].num);
        }
    }
}

0 0