scau_10317 Fans of Footbal Teams并查集的应用

10317 Fans of Footbal Teams

时间限制:1000MS  内存限制:65535K
提交次数:0 通过次数:0

题型: 编程题   语言: G++;GCC

Description

Two famous football teams, named AC Milan(AC米兰) and Inter Milan(国际米兰) will have a match in GuangZhou City, which is 
exciting. So a lot of fans get together to watch the wonderful match. This trouble the local polices. In order to avoid a 
chaos caused by fans of the two teams, the police office in GuangZhou City decides to arrange the seats of the gymnasium(体育馆) 
during the match. All fans of AC Milan seat Noth, while all fans of Inter Milan seat South . However, the police first needs 
to identify which team a fan support. The present question is, given two fans; do they support a same team? You must give 
your judgment based on incomplete information. 

Assume N (N <= 10^5) fans are currently in GuangZhou City, numbered from 1 to N. You will be given M (M <= 10^5) messages 
in sequence, which are in the following two kinds: 

1. D [a] [b] 
where [a] and [b] are the numbers of two fans, and they support different teams. 

2. A [a] [b] 
where [a] and [b] are the numbers of two fans. This requires you to decide whether a and b support a same team. 



输入格式

The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. 
Each test case begins with a line with two integers N and M, followed by M lines each containing one message as described above. 


输出格式

For each message "A [a] [b]" in each case, your program should give the judgment based on the information got before. 
The answers might be one of "Support the same team.", "Support different teams." and "Not sure yet."


输入样例

1
5 5
A 1 2
D 1 2
A 1 2
D 2 4
A 1 4


输出样例

Not sure yet.
Support different teams.
Support the same team.


作者

admin

这题目做法有点巧妙,我是看网上大神的代码才打出来了,我也不是很懂,我只能稍微说一下解题思路,我一开始以为只要它们不同队伍,那么不是A队就是B队,但后来wa了仔细想想,其实,这是错误想法,因为,尽管知道a和b不同队伍,但是我们也不能确定a到底是A队还是B队,b到底是A队还是B队。

转换一种思路,假设说这种集合是有n个的,通过不同队的判断,将这个集合数缩小,并且我们可以判断每个集合之前是否相同,那么我们就要将父节点数组开大成两倍,利用a 对应的就是 a+n,a+n代表与a不同队,b 对应的就是 b+n,b+n代表与b不同队.那么我们只需要将father[a]=b+n就可以了,因为有上面所说的对应关系,这种队伍的对应关系就确定了。

那么去判断是否是同一队伍就比较简单了,find(a)==find(b)证明这两个节点有相同的父节点,那么就是说同一队伍,而不同队伍就是find(a)==find(b+n) 因为find(b+n)就是代表与b不同队伍的根节点,如果和find(a)相同,也就是说,a与b不同队伍,剩下的情况就是不清楚了。

通过这道题,我发现自己对并查集也只是稍微理解的状态而已,还要加把劲啊!!!

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <bitset>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
using namespace std;
int  father[200009];
int Find(int x)
{
    if(x!=father[x]) father[x]=Find(father[x]);
    return father[x];
}
void join(int a,int b)
{
    int x=Find(a);
    int y=Find(b);
    if(x!=y) father[x]=y;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m,i,a,b;
        char c[3];
        scanf("%d%d",&n,&m);
        for(i=1; i<=2*n; i++)
        {
            father[i]=i;
        }
        while(m--)
        {
            scanf("%s%d%d",c,&a,&b);
            if(c[0]=='D')
            {
                join(a,b+n);//用b+n表示a非
                join(b,a+n);//用a+n表示b非
                //也就是说,a+n与b联通,那么我们从a开始找,找到a+n(这一定是不同队的根节点)
                //然后从b开始找,它的父节点就是a+n因此我们可以得到a与b是不同队的
                //如果同队,那么它们的父节点应该相同
            }
            else
            {
                if(Find(a)==Find(b))
                {
                    printf("Support the same team.\n");
                }
                else if(Find(a)==Find(b+n))
                {
                    printf("Support different teams.\n");
                }
                else
                {
                    printf("Not sure yet.\n");
                }
            }
        }
    }
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值