SCAU 07校赛 10317 Fans of Footbal Teams

10317 Fans of Footbal Teams

时间限制:1000MS  内存限制:65535K

题型: 编程题   语言: 无限制

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

解题思路

【题意】AC米兰和国际米兰要在广州比赛从而吸引众多的球迷前来观看,为了保证治安问题保安要为球迷分配观看的位置,现在需要知道哪些球迷支持相同的球队,哪些球迷支持不同的球迷,现在根据给的数据n(球迷的数量,编号从1到n),m(条信息,说明这两个球迷支持相反的球队或需要你输出判断这两个球迷支持球队的信息)

【随笔】开始认为只有两种情况,要么支持相同的球队,要么支持相反的球队,打算用一个数组就解决问题,后来发现这样下去有问题,比如出现两组支持不同球队的四个人,然后接下来的某条信息将其中的两个人联系起来,那么从中还可以得到四个人的信息,手写出数据来试着分析很容易发现其中的规律

D 1 2; D 2 4; D 3 5; D 7 8; D 5 2; D 1 7; D 6 9; D 6 10;

【解题】~并查集~给你的D中的数据主要分三种情况,两个球迷之前均没表明立场的;其一表明立场的;两者均有表明立场的;用一个数组构造并查集,用另一数组存储各自的死对头,死对头每次我都及时更新,其实后来想想是没必要的,因为敌人的敌人就是朋友,在构成的并查集里最终还是能找到源点表明不同的立场。存储死对头主要是想判断是否是”无法判断“这种情况。而存储死对头的还有一个作用是找到死对头的源点,将此时新增的死对头加入此行列。并查集查找源点的时候即使压缩路径,所以尽管每次都去查找,但是消耗的时间还不是很多。

【PS】这题恶心了好长的一段时间,主要是思路还不是很清晰,现在回想很多想法和考虑到的东西以及实现的方法还是没必要的,这里已经浪费了大半的时间

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<string>
  4 #include<cstring>
  5 #include<algorithm>
  6 #define MAXN 100010
  7 
  8 using namespace std;
  9 
 10 
 11 int middle[MAXN], digit[MAXN];
 12 char same[] = "Support the same team.";
 13 char dif[] = "Support different teams.";
 14 char mix[] = "Not sure yet.";
 15 
 16 int find_father(int fac)
 17 {//查找源头并及时压缩路径 
 18     return middle[fac] = fac == middle[fac] ? fac : find_father(middle[fac]);
 19 }
 20 
 21 /*
 22 void Print(int n)
 23 {
 24     static int cnt = 0;
 25     for(int i=1; i<=n; i++)
 26     {
 27         printf("middle[%d] = %d, D[%d] = %d\n", i, middle[i], i, digit[i]);
 28     }
 29     printf("-------------------%d----------------------\n", ++cnt);
 30 }
 31 */
 32 
 33 int main()
 34 {
 35     #ifndef ONLINE_JUDGE
 36     freopen("F:\\test\\input.txt", "r", stdin);
 37     #endif // ONLINE_JUDGE
 38 
 39     int T;
 40     int n, m, first, second, sum;
 41     int father, foster, fir_father, sec_father, fir_foster, sec_foster;
 42     char kind;
 43     bool falg = false;
 44     scanf("%d", &T);
 45     while(T--)
 46     {
 47         scanf("%d%d", &n, &m);
 48         for(int i=0; i<=n; ++i)
 49         {
 50             middle[i] = i;
 51             digit[i] = 0;
 52         }
 53         for(int i=0; i<m; ++i)
 54         {
 55             getchar();
 56             scanf("%c", &kind);
 57             scanf("%d%d", &first, &second);
 58             if(kind == 'A')
 59             {
 60                 fir_father = find_father(first);
 61                 sec_father = find_father(second);
 62                 if(fir_father == sec_father)
 63                     printf("%s\n", same);
 64                    else
 65                    {
 66                     fir_foster = find_father(digit[fir_father]);
 67                     if(fir_foster == sec_father)
 68                         printf("%s\n", dif);
 69                        else 
 70                            printf("%s\n", mix);
 71                 }
 72                 
 73             }
 74             else if(kind == 'D')
 75             {
 76                 if(!digit[first] && !digit[second])
 77                 {//互相存储死对头的编号 
 78                     digit[first] = second;
 79                     digit[second] = first;
 80                 }
 81                 else if(!digit[first] && digit[second])
 82                 {
 83                     father = find_father(second);
 84                     foster = find_father(digit[father]);
 85                     middle[foster] = first;
 86                     digit[first] = father;
 87                 }
 88                 else if(digit[first] && !digit[second])
 89                 {
 90                     father = find_father(first);
 91                     foster = find_father(digit[father]);
 92                     middle[foster] = second;
 93                     digit[second] = father;
 94                 }
 95                 else
 96                 {
 97                     fir_father = find_father(first);
 98                     sec_father = find_father(second);
 99                     fir_foster = find_father(digit[fir_father]);
100                     sec_foster = find_father(digit[sec_father]);
101                     middle[fir_foster] = sec_father;
102                     middle[sec_foster] = fir_father;
103                 }
104             }
105         }
106     }
107 
108     return 0;
109 }

 

转载于:https://www.cnblogs.com/liaoguifa/p/3186443.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值