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+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(并查集)的更多相关文章
- SCAU 07校赛 10317 Fans of Footbal Teams
10317 Fans of Footbal Teams 时间限制:1000MS 内存限制:65535K 题型: 编程题 语言: 无限制 Description Two famous footba ...
- 10317 Fans of Footbal Teams
10317 Fans of Footbal Teams 时间限制:1000MS 内存限制:65535K提交次数:0 通过次数:0 题型: 编程题 语言: G++;GCC Description ...
- Codeforces Round #181 (Div. 2) B. Coach 带权并查集
B. Coach 题目连接: http://www.codeforces.com/contest/300/problem/A Description A programming coach has n ...
- Coach(并查集)
Description A programming coach has n students to teach. We know that n is divisible by 3. Let's ass ...
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
- 关押罪犯 and 食物链(并查集)
题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...
- bzoj1854--并查集
这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...
- [bzoj3673][可持久化并查集 by zky] (rope(可持久化数组)+并查集=可持久化并查集)
Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...
随机推荐
- MVC5 Entity Framework学习之创建复杂的数据模型
目录(?)[-] 使用属性来自定义数据模型 DataType属性 StringLength属性 Column 属性 完成对Student实体的更改 Required 属性 Display 属性 Ful ...
- vs2015打开cshtml文件失败的解决方法
最近不知道为什么,用vs2015打开cshtml识图文件的时候会报错.也不知道是什么原因,google之后得到解决方法如下: Close VS Delete the content of %Local ...
- hdu2063 匈牙利算法 二分最大匹配模版题
过山车 Time Limit: 1000 MS Memory Limit: 32768 KB 64-bit integer IO format: %I64d , %I64u Java class na ...
- c# 对象object转换
object app = new { name = "hyt", age = 18 }; Type t = app.GetType(); var name = t.GetType( ...
- php 备份数据库
<?php header("Content-type:text/html;charset=utf-8"); //配置信息 $cfg_dbhost = 'loca ...
- Openjudge-计算概论(A)-年龄与疾病
描述: 某医院想统计一下某项疾病的获得与否与年龄是否有关,需要对以前的诊断记录进行整理. 输入共2行,第一行为过往病人的数目n(0 < n <= 100),第二行为每个病人患病时的年龄.输 ...
- wpf 透明效果 需要DwmApi.dll文件,然后定义一个函数去画Aero区域,从而实现整个窗口的Aero化。
private void ExtendAeroGlass(Window window) { try { // 为WPF程序获取窗口句柄 IntPtr mainWindowPtr = new Windo ...
- 2015 ACM/ICPC Asia Regional Changchun Online
1001 Alisha’s Party 比赛的时候学长stl吃T.手写堆过. 赛后我贴了那两份代码都过.相差.2s. 于是用stl写水果. # include <iostream> # i ...
- javascript动画效果之匀速运动(修订版)
在编写多块同时触发运动的时候,发现一个BUG, timer = setInterval(show, 30);本来show是一个自定义函数,当设为timer = setInterval(show(one ...
- HDU2202--最大三角形(凸包,枚举)
Problem Description 老师在计算几何这门课上给Eddy布置了一道题目,题目是这样的:给定二维的平面上n个不同的点,要求在这些点里寻找三个点,使他们构成的三角形拥有的面积最大.Eddy ...