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

如图,样例中1和2是对立的,那么把1和2+n连起来,再把2和1+n连起来。2和4是对立,那就把2和4+n连起来,4和2+n连起来。

很明显,最后连起来结果是1和4是同队

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

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int f[];
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=;i<=*n;i++)
f[i]=i;
char ch[];
int a,b;
for (int i=;i<m;i++)
{
scanf("%s%d%d",ch,&a,&b);
if (ch[]=='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[]=='D')
{
mix(a,b+n);
mix(a+n,b);
}
}
}
return ;
}

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

  1. SCAU 07校赛 10317 Fans of Footbal Teams

    10317 Fans of Footbal Teams 时间限制:1000MS  内存限制:65535K 题型: 编程题   语言: 无限制 Description Two famous footba ...

  2. 10317 Fans of Footbal Teams

    10317 Fans of Footbal Teams 时间限制:1000MS  内存限制:65535K提交次数:0 通过次数:0 题型: 编程题   语言: G++;GCC Description ...

  3. Codeforces Round #181 (Div. 2) B. Coach 带权并查集

    B. Coach 题目连接: http://www.codeforces.com/contest/300/problem/A Description A programming coach has n ...

  4. Coach(并查集)

    Description A programming coach has n students to teach. We know that n is divisible by 3. Let's ass ...

  5. BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]

    4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...

  6. 关押罪犯 and 食物链(并查集)

    题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...

  7. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  8. bzoj1854--并查集

    这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...

  9. [bzoj3673][可持久化并查集 by zky] (rope(可持久化数组)+并查集=可持久化并查集)

    Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...

随机推荐

  1. hdu 2546 饭卡 (01背包)

    Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负) ...

  2. 希望获取到页面中所有的checkbox怎么做?

    var domList = document.getElementsByTagName(‘input’); var checkBoxList = []; var len = domList.lengt ...

  3. 给当前的URL添加/更新新的参数

    /** * 给当前的URL添加/更新新的参数 * @param a 参数 * @param b 值 * @returns {string} 新的参数 * @constructor */ functio ...

  4. C# for循环嵌套

    今天,我主要学习了for循环的嵌套. 第一个部分主要学习了打印直角在不同方向的三角形,求阶乘的和以及打印九九乘法表等. 例:打印直角在左下角的三角形. Console.WriteLine(" ...

  5. yali项目的slider

    // 调用 var s41 = new slider({ target : '#slider411', titleActiveClass : 'j-active', itemActiveClass : ...

  6. python 实现excel转化成json文件

    1.准备工作 python 2.7 安装 安装xlrd -- pip install xlrd 2. 直接上代码 import xlrd from collections import Ordered ...

  7. DateUtils时间工具类探究

    首先声明一下,这个DateUtils工具类不是自己写的,而是在commons-lang-2.Xjar包或是commons-lang3-3.X.jar包中,具体在哪个jar包中,看程序具体添加了哪个版本 ...

  8. 复习之webview(观看张荣超视频)

    webview定义的两张方式: 1,是在xml文件里面进行定义: 2,实在代码里面定义:   3,在webview上显示内容必要的配置:   4常见的设置: 是否支持javascript 是否支持缩放 ...

  9. eclipse代码提示优化

    用Eclipse编写Android程序的代码提示功能主要是在java和xml文件中,有时候会失效,默认的提示功能有限. 1)java文件自动提示      Window->Preferences ...

  10. H5的新应用-指定视频的播放进度

    <!DOCTYPE html> <html> <head> <title>指定视频的播放进度</title> <meta http-e ...