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. Caffe+Ubuntu14.04+CUDA7.5 环境搭建(新人向)指南

    序 本文针对想学习使用caffe框架的纯新手,如果文中有错误欢迎大家指出. 由于我在搭建这个环境的时候参考了许多网上的教程,但是没有截图,所以文中图片大多来源于网络. 本文没有安装matlab的步骤, ...

  2. java的克隆

    java有深拷贝和浅拷贝的区别. 浅拷贝:他是指拷贝该对象时,仅仅是拷贝了对象的本身(包括对象的基本数据类变量),不会拷贝引用数据类型的变量,也就是拷贝出来的新对象基本数据类型的值不变,引用数据类型的 ...

  3. RPM安装gcc gcc-c++扩展

    rpm -ivh cpp--.el5.i386.rpm 回车 rpm -ivh kernel-headers--.el5.i386.rpm 回车 rpm -ivh glibc-headers-.i38 ...

  4. 证明:一个整数a若不能被6整除,则a2+24必能被24整除。(整除理论,1.1.4)

    证明:一个整数a若不能被6整除,则a2+24必能被24整除. 证明: 因为,a不能被6整除 所以,a不可以同时被2和3整除 所以,a一定是一个奇数, 所以,令a=2k+1,k是整数: 又因为,a2+2 ...

  5. MySQL Administrator的简单操作

    A.创建数据库 1.运行MySQL Administrator服务器,如下图. 2.点击"Catalogs",下面出现已有的数据库,右击已有的数据库,选择“Create New S ...

  6. 如何通过Maven的Tomcat插件运行Web工程

    去tomcat官网http://tomcat.apache.org/,左侧栏Apache Tomcat下的Maven Plugin,点进去选择最新版本Version 2.2 通过介绍可知,使用tomc ...

  7. hibernate在配置文件中配置对象关系映射文件即hbm文件路径的写法

    hbm文件如果在src下,则<mapping resource="*.hbm.xml"/> 如果在实体类包中,则<mapping resource="c ...

  8. win安装Theano

    艰辛的安装Theano过程,把其中遇到的问题记录下来,三台机子都尝试了安装Theao,系统分别为:A机:win7 64-bit(笔记本).B机:win7 64-bit(台式机).C机:win8 64- ...

  9. 总结的git操作命令小抄集

    .gitignore 本地仓库主目录下,用于定义提交时忽略的文件   git add <file-name> 将修改或新增的文件存入暂存区   git reset HEAD <fil ...

  10. WinDbg抓取程序报错dump文件的方法

    程序崩溃的两种主要现象: a. 程序在运行中的时候,突然弹出错误窗口,然后点错误窗口的确定时,程序直接关闭 例如: “应用程序错误” “C++错误之类的窗口” “程序无响应” “假死”等 此种崩溃特点 ...