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.

作者

admin

  题意很好理解,,,就是给出信息语句告诉你哪两个人是支持不同队的,,,然后还有询问语句询问两个人是否是支持同一队伍;我解这题的解法是略有点奇葩的,,,算是套用了poj 1182(并查集经典题目,推荐也去做了)这道题的解法。。。既然是只提供信息说某两个人是支持不同的队伍,那么用 f[i]=k 表示"第i个人支持的队伍是跟某个人支持的队伍相同的"这一事件;而 f[i+n]=p 表示 f[i]=i  这个事件的对立事件;注意!这里的数组f[i]=k的意思是编号i的人支持某支队伍的事件,而不是指编号为i的人!如果数据给了D 2 5的话,那就把f[2]和f[5+n]合并,f[2+n]和f[5]合并;因为如果编号2和5的人支持不同的队伍,那么f[2]和 f[5]的对立事件发[5+n]必定是同时存在的,而f[2]的对立事件f[2+n]是和f[5]同时存在的。

  猴,下面放代码吧,,,,

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <cmath>
 6 #include <cstdlib>
 7 #include <cctype>
 8 #include <queue>
 9 #include <stack>
10 #include <map>
11 #include <vector>
12 #include <set>
13 #include <utility>
14 #define ll long long
15 #define inf 0x3f3f3f3f
16 using namespace std;
17
18 int t,n,m,temp;
19 int f[200500];//记录父亲结点
20 int father(int u) //寻找父亲
21 {
22     if(f[u]==u)
23         return u;
24     return f[u]=father(f[u]);
25 }
26 void unite(int x,int y)//合并x和y两个集合
27 {
28     int tx=father(x),ty=father(y);
29     if(tx!=ty)
30         f[ty]=tx;
31 }
32 bool same(int x,int y)//判断x和y是否属于同一集合
33 {
34     return father(x)==father(y);
35 }
36 int main()
37 {
38     //freopen("input.txt","r",stdin);
39     scanf("%d",&t);
40     while(t--)
41     {
42         char str[3];
43         int a,b;
44         scanf("%d%d",&n,&m);
45         temp=2*n;
46         for(int i=1;i<=temp;i++)
47             f[i]=i;
48         while(m--)
49         {
50             scanf("%s%d%d",str,&a,&b);
51             if(str[0]==‘D‘)
52             {   //将a和b+n合并,b和a+n合并
53                 unite(a,b+n);
54                 unite(a+n,b);
55             }
56             else
57             {
58                 if(same(a,b+n))
59                     printf("Support different teams.\n");
60                 else if(same(a,b))
61                     printf("Support the same team.\n");
62                 else
63                     printf("Not sure yet.\n");
64
65             }
66         }
67     }
68     return 0;
69 }

  其实这题也可以不用这么傻逼的做法的,,同宿舍里的大神告诉我其实用结构体存数据,然后里面再加个数据域用来存放第i个人的对立队伍的集合编号就可以了

时间: 2024-10-13 16:05:09

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

POJ——T2271 Guardian of Decency

http://poj.org/problem?id=2771 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5932   Accepted: 2463 Description Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an excursion, but he is

poj——2771 Guardian of Decency

poj——2771    Guardian of Decency Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5916   Accepted: 2458 Description Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an excursion, but he i

poj 2771 Guardian of Decency

Guardian of Decency http://poj.org/problem?id=2771 Time Limit: 3000MS   Memory Limit: 65536K       Description Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an excursion, but he is afraid that som

uva 12083 Guardian of Decency (二分图匹配)

uva 12083 Guardian of Decency Description Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an excursion, but he is afraid that some of them might become couples. While you can never exclude this poss

POJ2771 Guardian of Decency

Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5513   Accepted: 2319 Description Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an excursion, but he is afraid that some of them might

poj 2771 Guardian of Decency【最大点独立集】

K - Guardian of Decency Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2771 Appoint description:  System Crawler  (2014-08-17) Description Frank N. Stein is a very conservative high-school teac

POJ2771_Guardian of Decency(二分图/最大独立集=N-最大匹配)

解题报告 http://blog.csdn.net/juncoder/article/details/38159017 题目传送门 题意: 看到题目我就笑了,,, 老师认为这样的两个学生不是一对: 身高相差40以上(年龄都不是距离了,身高又算什么) 不同性别(sad,,,就不允许基友存在呀,,,谁的肥皂掉了,,,) 喜欢不一样的歌曲类型(你总不能要求两人整天听小苹果吧,,,,,,你是我的小丫小苹果,,,,,,) 喜欢一样的运动( they are likely to be fans of dif

POJ 2771 二分图(最大独立集)

Guardian of Decency Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5244   Accepted: 2192 Description Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an excursion, but he is afraid that