scau_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

这题目做法有点巧妙,我是看网上大神的代码才打出来了,我也不是很懂,我只能稍微说一下解题思路,我一开始以为只要它们不同队伍,那么不是A队就是B队,但后来wa了仔细想想,其实,这是错误想法,因为,尽管知道a和b不同队伍,但是我们也不能确定a到底是A队还是B队,b到底是A队还是B队。

转换一种思路,假设说这种集合是有n个的,通过不同队的判断,将这个集合数缩小,并且我们可以判断每个集合之前是否相同,那么我们就要将父节点数组开大成两倍,利用a 对应的就是 a+n,a+n代表与a不同队,b 对应的就是 b+n,b+n代表与b不同队.那么我们只需要将father[a]=b+n就可以了,因为有上面所说的对应关系,这种队伍的对应关系就确定了。

那么去判断是否是同一队伍就比较简单了,find(a)==find(b)证明这两个节点有相同的父节点,那么就是说同一队伍,而不同队伍就是find(a)==find(b+n) 因为find(b+n)就是代表与b不同队伍的根节点,如果和find(a)相同,也就是说,a与b不同队伍,剩下的情况就是不清楚了。

通过这道题,我发现自己对并查集也只是稍微理解的状态而已,还要加把劲啊!!!

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <bitset>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
using namespace std;
int  father[200009];
int Find(int x)
{
    if(x!=father[x]) father[x]=Find(father[x]);
    return father[x];
}
void join(int a,int b)
{
    int x=Find(a);
    int y=Find(b);
    if(x!=y) father[x]=y;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m,i,a,b;
        char c[3];
        scanf("%d%d",&n,&m);
        for(i=1; i<=2*n; i++)
        {
            father[i]=i;
        }
        while(m--)
        {
            scanf("%s%d%d",c,&a,&b);
            if(c[0]=='D')
            {
                join(a,b+n);//用b+n表示a非
                join(b,a+n);//用a+n表示b非
                //也就是说,a+n与b联通,那么我们从a开始找,找到a+n(这一定是不同队的根节点)
                //然后从b开始找,它的父节点就是a+n因此我们可以得到a与b是不同队的
                //如果同队,那么它们的父节点应该相同
            }
            else
            {
                if(Find(a)==Find(b))
                {
                    printf("Support the same team.\n");
                }
                else if(Find(a)==Find(b+n))
                {
                    printf("Support different teams.\n");
                }
                else
                {
                    printf("Not sure yet.\n");
                }
            }
        }
    }
}



提供了基于BP(Back Propagation)神经网络结合PID(比例-积分-微分)控制策略的Simulink仿真模型。该模型旨在实现对杨艺所著论文《基于S函数的BP神经网络PID控制器及Simulink仿真》中的理论进行实践验证。在Matlab 2016b环境下开发,经过测试,确保能够正常运行,适合学习和研究神经网络在控制系统中的应用。 特点 集成BP神经网络:模型中集成了BP神经网络用于提升PID控制器的性能,使之能更好地适应复杂控制环境。 PID控制优化:利用神经网络的自学习能力,对传统的PID控制算法进行了智能调整,提高控制精度和稳定性。 S函数应用:展示了如何在Simulink中通过S函数嵌入MATLAB代码,实现BP神经网络的定制化逻辑。 兼容性说明:虽然开发于Matlab 2016b,但理论上兼容后续版本,可能会需要调整少量配置以适配不同版本的Matlab。 使用指南 环境要求:确保你的电脑上安装有Matlab 2016b或更高版本。 模型加载: 下载本仓库到本地。 在Matlab中打开.slx文件。 运行仿真: 调整模型参数前,请先熟悉各模块功能和输入输出设置。 运行整个模型,观察控制效果。 参数调整: 用户可以自由调节神经网络的层数、节点数以及PID控制器的参数,探索不同的控制性能。 学习和修改: 通过阅读模型中的注释和查阅相关文献,加深对BP神经网络与PID控制结合的理解。 如需修改S函数内的MATLAB代码,建议有一定的MATLAB编程基础。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值