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. .Net Core 学习资料

    官方网站:https://www.microsoft.com/net/core#windows   官方文档:https://docs.asp.net/en/latest/intro.html   中 ...

  2. 简单介绍移动端CSS3单位rem的用法

    PC端大部份是用px单位,小部分用em单位,而移动端,请全部用rem单位吧.目前大部份设备,包括但不限于iOS 5+.Android 2.3+.Window Phone 8+都是可以兼容的,具体兼容表 ...

  3. 高精度运算专题-输出函数与字符串转数字函数(Output function and the string to number function)

    输出函数:这个函数别看它小,但浓缩的都是精华啊 作用:对于高精度的数组进行倒序输出 思路:首先从被传入的数组第一位开始,一直往前扫输出就可以了(i--) 注释:因为每个数组的第一位是用来存储这个数组的 ...

  4. SharePoint 网站登录不上,3次输入用户名/密码白页、

    来源于:http://www.cnblogs.com/jianyus/p/3249091.html 新搭建的SharePoint 2013环境,第一次干的这么憋屈的慌,先是接了一个Ghost的服务器, ...

  5. XP 右键扩展设置 1.0 免费绿色版

    软件名称: xp右键扩展设置软件 1.0 免费绿色版软件语言: 简体中文授权方式: 免费软件运行环境: Win7 / Vista / Win2003 / WinXP / Win2008软件大小: 57 ...

  6. drupal7的node的内容的存储位置

    标题是存在node表中的,但是实际内容存在表field_data_body中

  7. 多态性Polymorphism

    一.多态性的概念:   1.多态:在面向对象方法中一般是这样表述多态性的: 向不同的对象发送同一个消息,不同的对象在接收时会产生不同的行为(即方法).也可以说,多态性是“一个接口,多种方法”. 2.从 ...

  8. tmux commands

    最近在学Linux,用到tmux这个命令,看到很多快捷键的介绍,个人觉得不太好用,因此把几个常用的命令记录下来,以便以后学习和使用. 常用tmux commands: tmux ls         ...

  9. SourceTree基础

    克隆(clone):从远程仓库URL加载创建一个与远程仓库一样的本地仓库 提交(commit):将暂存文件上传到本地仓库(我们在Finder中对本地仓库做修改后一般都得先提交一次,再推送) 检出(ch ...

  10. MFC中MessageBeep与sndPlaySound播放声音函数使用

    MessageBeep(0x00000000L);    //用来播放系统默认音频文件,如0x00000000L为系统提示音,具体音频对应规则,请参照MSDN. sndPlaySound函数用来播放指 ...