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.

题意

有两个足球队,场上的人只支持其中一个队。给出命令A或D,再输入数字a和b,若命令是A,则查询a和b是否支持同一个队,若命令是D,则说明a和b支持不同的队

题解

我们假设a和a+n站在对立面,那么,当a和b是对立时,a和b+n就是同队的,根据这个关系,我们在合并时,可以合并a和b+n,a+n和b;

然后通过并查集查询,就很容易了。代码如下

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int f[100100];
int find(int x)
{
    int r=x,i=x,t;
    while (r!=f[r]) r=f[r];
    while (f[i]!=r)
    {
        t=f[i];
        f[i]=r;
        i=t;
    }
    return r;
}

void mix(int x,int y)
{
    int fx=find(x),fy=find(y);
    if (fx!=fy) f[fx]=fy;
}
int main()
{
    int T;
    scanf("%d",&T);
    while (T--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
         for (int i=0;i<=2*n;i++)
         f[i]=i;
         char ch[5];
         int a,b;
         for (int i=0;i<m;i++)
         {
             scanf("%s%d%d",ch,&a,&b);
             if (ch[0]==‘A‘)
             {
                 if (find(a)==find(b+n))
                 printf("Support different teams.\n");
                 else if (find(a)==find(b))
                 printf("Support the same team.\n");
                 else
                 printf("Not sure yet.\n");
             }
             else if (ch[0]==‘D‘)
             {
                 mix(a,b+n);
                 mix(a+n,b);
             }
         }
    }
    return 0;
}
时间: 2024-10-21 09:58:57

10317 Fans of Footbal Teams(并查集)的相关文章

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 tog

CodeForces 745C Hongcow Builds A Nation 并查集

题意: 给了你n个城市 m条边 k个政府 每个政府管辖的区域内不能和其他政府的区域有相连 即政府之间不存在路径 问你在维护这种关系的同时 最多再加多少条边 思路: 先找出来每个联通块 再找出来没有归属的孤立的点 把他们都放到最大的联通块里 然后每个联通块之间的点两两连边是n*(n-1)/2条边 最后算出来的ans-m就好了 (看别人的博客学了一个max_element 1 #include<bits/stdc++.h> 2 #define cl(a,b) memset(a,b,sizeof(a

并查集(个人模版)

并查集: 1 int find(int a) 2 { 3 int r=a; 4 while(f[r]!=r) 5 r=f[r]; 6 int i=a; 7 int j; 8 while(i!=r) 9 { 10 j=f[i]; 11 f[i]=r; 12 i=j; 13 } 14 return r; 15 } 16 int merge(int a,int b) 17 { 18 int A,B; 19 A=find(a); 20 B=find(b); 21 if(A!=B) 22 { 23 f[B

并查集应用

题目描述: One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of all the phone calls

【bzoj3674】 可持久化并查集加强版

http://www.lydsy.com/JudgeOnline/problem.php?id=3674 (题目链接) 题意 维护并查集3个操作:合并:回到完成第k个操作后的状态:查询. Solution 其实就是用主席树的叶子节点维护并查集的可持久化数组fa[]. 细节 终于认识到了按秩合并的强大,单纯写个路径压缩Re飞,写了路径压缩+按秩合并比单纯的按秩合并每快多少→_→ 代码 // bzoj3674 #include<algorithm> #include<iostream>

BZOJ1015[JSOI2008]星球大战starwar[并查集]

1015: [JSOI2008]星球大战starwar Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 5253  Solved: 2395[Submit][Status][Discuss] Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过特殊的以太隧道互相直接或间接地连接. 但好景不长,很快帝国又重

HDU 5606 tree 并查集

tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ans?i??=size[findset(i)],size表示每个并查集根的size Ans_i=size[findset(i)],sizeAns?i??=size[findset(i)],size表示每个并查集根的sizesize. #include<cstdio> #include<cstring> #include<algorithm>

HDU 5441 离线处理 + 并查集

题意:给n个节点m条带权值边的无向图.然后q个问题,每次询问点对的数目,点对需要满足的条件是:1)连通:2)其路径的最大权值不能超过询问值. 分析:如果没次询问一次,dfs一次,很可能超时,因此可以用并查集.离线处理,把边按权值排序,把问题按大小排序.然后离线的过程就是不断向图中加边的过程. 比如样例如下: 然后离线处理,排完序后将会是一条一条的加边:问题也排了序,因此是个累加过程... 1 #include <cstdio> 2 #include <iostream> 3 #in

poj1988 Cube Stacking(并查集

题目地址:http://poj.org/problem?id=1988 题意:共n个数,p个操作.输入p.有两个操作M和C.M x y表示把x所在的栈放到y所在的栈上(比如M 2 6:[2 4]放到[1 6]上为[2 4 1 6]),C x为输出x下面有几个数. 思路:并查集每个集合以栈最下面的数为根,维护两个数组num[x]表示x所在集合节点总数,count[x]表示x下方节点个数.每次查找压缩路径的时候更新count(换父节点的时候每轮都把父节点的count加给儿子,就可以一直更新到x所在栈