电影节

Description
某届电影节评选电影,共有两部电影进入最后评选环节,有n名观众,每个人有一次投票的机会,每个人都按照规则投给其中一部电影。为了了解情况,记者随机询问了一些人,一共询问了m次,特别神奇的是,记者每次都询问两个人,而且这两个人都把票投给了同一部电影,观众编号为1~n。
Input
多组输入,每组第一行是两个整数n,m (2 <= n <=100000,0 <= m < n/2),接下来m行数据,表示m次询问,每行数据有两个整数a,b代表观众的编号(1 <= a,b <= n),观众a和观众b投票给了同一部电影,接下来一行是两个整数c,d(1 <= c,d <= n)。
Output
对于每一组输入,输出一行,如果观众c和观众d投票给同一部电影,输出”same”,如果不能确定,输出”not sure”。
Sample
Input
5 2
1 2
2 3
1 3
5 2
1 2
3 4
1 4
5 2
1 2
3 4
2 5
Output
same
not sure
not sure
Hint

并查集

#include <iostream>
#include<bits/stdc++.h>
int father[100002];
int findFather(int x)
{
    while(x!=father[x])
        x=father[x];
    return x;
}
int main()
{
    int n,i,m,a,b,c,d;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        for(i=1; i<=n; i++)father[i]=i;
        for(i=0; i<m; i++)
        {
            scanf("%d %d",&a,&b);
            father[b]=a;
        }
        scanf("%d %d",&c,&d);
        if(findFather(c)==findFather(d))
            printf("same\n");
        else printf("not sure\n");
    }
    return 0;
}

也可以这样:

#include <iostream>
#include<bits/stdc++.h>
int father[100002];
int findFather(int x)
{
    while(x!=father[x])
        x=father[x];
    return x;
}
void Union(int x,int y)
{int fatherX=findFather(x);
    int fatherY=findFather(y);
    if(fatherX!=fatherY)
        father[fatherY]=fatherX;
}
int main()
{
    int n,i,m,a,b,c,d;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        for(i=1; i<=n; i++)father[i]=i;
        for(i=0; i<m; i++)
        {
            scanf("%d %d",&a,&b);
            Union(a,b);
        }
        scanf("%d %d",&c,&d);
        if(findFather(c)==findFather(d))
            printf("same\n");
        else printf("not sure\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值