幼儿园战争

题面传送门

灵魂出窍并查集,修改后留一个空壳以确保家人们的团结

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;

inline void input(int &x){
    int ans=0,f=1;
    char c=getchar();
    while(c>'9'||c<'0'){
        if(c=='-')f=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9'){
        ans=(ans<<1)+(ans<<3)+(c-48);
        c=getchar();
    }
    x=ans*f;
}

inline void output(int x){
    if(x<0)x=-x,putchar('-');
    if(x>9)output(x/10);
    putchar(x%10+48);
}

inline void writeln(int x){
    output(x);
    putchar('\n');
}

int n,m,fa[1000005],size[1000005],now[1000005],tot,cnt;

inline int getfa(int v){return fa[v]==v?v:fa[v]=getfa(fa[v]);}

int main(){
    int T;
    input(T);
    for(int emm=1;emm<=T;emm++){
        printf("Case #%d:\n",emm);
        input(n);input(m);
        for(int i=1;i<=n;i++){
            fa[i]=i;
            size[i]=1;
            now[i]=i;
        }
        tot=n;cnt=n;
        for(int i=1;i<=m;i++){
            char s[10];
            scanf("%s",s);
            if(s[0]=='q'){
                writeln(tot);
            }
            else if(s[0]=='f'){
                int x,y,yuanx,yuany,fax,fay;
                input(yuanx);input(yuany);x=now[yuanx];y=now[yuany];
                fax=getfa(x);fay=getfa(y);
                if(fax==fay)continue;
                if(size[fax]==size[fay]){
                    printf("Either is winner!\n");
                }
                else if(size[fax]>size[fay]){
                    printf("%d is winner!\n",yuanx);
                    fa[fay]=fax;
                    size[fax]+=size[fay];
                    tot--;
                }
                else if(size[fay]>size[fax]){
                    printf("%d is winner!\n",yuany);
                    fa[fax]=fay;
                    size[fay]+=size[fax];
                    tot--;
                }
            }
            else if(s[0]=='t'){
                int x,y,yuanx,yuany,fax,fay;
                input(yuanx);input(yuany);x=now[yuanx];y=now[yuany];
                fax=getfa(x);fay=getfa(y);
                int next=++cnt;
                fa[next]=fax;
                size[fax]++;
                size[fay]--;
                if(size[fay]==0)tot--;
                now[yuany]=next;
            }
            else if(s[0]=='r'){
                int x,yuanx,fax;
                input(yuanx);x=now[yuanx];
                fax=getfa(x);
                if(size[fax]==1)continue;
                int next=++cnt;
                fa[next]=next;
                size[next]=1;
                size[fax]--;
                tot++;
                now[yuanx]=next;
            }
            /*
            putchar('a');
            for(int i=1;i<=n;i++){
                printf("%d,%d ",size[getfa(now[i])],getfa(now[i]));
            }
            putchar('\n');
            */
        }
    }
}

转载于:https://www.cnblogs.com/Y15BeTa/p/11617704.html

对于幼儿园级别的篮球游戏,C++可能不是最直接的选择,因为它的学习曲线陡峭,而且更适合于年龄较大的孩子和青少年。然而,如果你的目标是创建一个简单易懂的基础教学游戏,你可以考虑使用更易于理解的语言如Python或者专门针对儿童开发的游戏引擎,比如pygame。 如果要以C++为基础设计一个基础篮球游戏,这里可以给出一个非常简单的概念示例,但请注意这并不是幼儿园级别的游戏,而是简化版的入门教程: ```cpp #include <iostream> #include <ctime> // 定义篮球得分类 class BasketballScore { public: int score; }; int main() { srand(time(0)); // 设置随机数种子 // 创建两个队伍的对象 BasketballScore teamA, teamB; // 初始化分数为0 teamA.score = 0; teamB.score = 0; // 模拟投篮(随机决定得分) for (int i = 0; i < 5; ++i) { // 假设每个回合投5次篮 if (rand() % 2 == 0) { // 队员A得分的概率为50% teamA.score += 1; std::cout << "Team A scored! (" << teamA.score << " points)\n"; } else { teamB.score += 1; std::cout << "Team B scored! (" << teamB.score << " points)\n"; } } // 比较分数并宣布获胜者 if (teamA.score > teamB.score) { std::cout << "Team A wins with a score of " << teamA.score << " to " << teamB.score << "!\n"; } else if (teamA.score < teamB.score) { std::cout << "Team B wins with a score of " << teamB.score << " to " << teamA.score << "!\n"; } else { stdcout << "It's a tie with both teams scoring " << teamA.score << " points.\n"; } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值