• 两个团伙


    题目:

    有n个人分别属于两个团伙,从1到n编号,接下来m组形如 ch x y的数据,ch为“D"表示 x, y属于不同的团伙,ch为"A"表示询问x,y是否属于同一个团伙。

    输入

    输入一个整数T(1 le T le 20)T(1T20),表示数据的数目。

    对于每一组数据。

    第一行给出两个整数,n(1 le n le 10^5), m(1 le m le 10^5)n(1n 105),m(1m105)。
    接下来m行,每行形如ch x y。

    输出

    对于A x y的询问操作,如果x,y属于同一团伙输出 "In the same gang.";如果属于不同团伙,输出 "In different gangs." ;如果不确定,输出 "Not sure yet."。

    样例

    输入

    1
    5 5
    A 1 2
    D 1 2
    A 1 2
    D 2 4
    A 1 4

    输出

    Not sure yet.
    In different gangs.
    In the same gang.


    /*************************************************************************
    > Author: Henry Chen
    > Mail: 390989083@qq.com
    > Created Time: 日 8/23 21:25:56 2020
    ************************************************************************/

    #include<bits/stdc++.h>
    using namespace std;
    int fa[100009],sm[100009],sn[100009];
    int find(int x)
    {
    if(fa[x] == x) return x;
    int k = find(fa[x]);
    sm[x] += sm[fa[x]];
    fa[x] = k;
    return fa[x];
    }
    void work()
    {
    int n,m;
    cin >> n >> m;
    for(int i = 1;i <= n;i++)
    {
    fa[i] = i;
    sm[i] = 0;
    sn[i] = 0;
    }
    while(m--)
    {
    char c;
    scanf("%*c%c",&c);
    int x,y;
    scanf("%d%d",&x,&y);
    if(c == 'A')
    {
    if(find(x) != find(y)) puts("Not sure yet.");
    else if((sm[x]+sm[y])%2 == 0) puts("In the same gang.");
    else puts("In different gangs.");
    }
    else
    {
    if(find(x) != find(y))
    {
    int fx = find(x);
    int fy = find(y);
    if(sn[fx] > sn[fy])
    {
    fa[fy] = fx;
    sm[fy] = (sm[x]+sm[y]+1)%2;

    }
    else
    {
    fa[fx] = fy;
    sm[fx] = (sm[x]+sm[y]+1)%2;
    if(sn[fx] == sn[fy])
    {
    sn[fx]++;
    }
    }
    }
    }
    }
    }
    int main()
    {
    int t;
    cin >> t;
    while(t--)
    {
    work();
    }
    return 0;
    }


    备注:调试花了n多时间,垃圾cin 差17倍。。。

  • 相关阅读:
    关于使用CodeFirst,修改类或上下文时操作数据库报错解决方法
    解决:启用多线程调用webBrowsers函数报错:指定的转换无效
    强制IE浏览器或WebBrowser控件使用指定版本显示网页
    EF:分页查询 + 条件查询 + 排序
    (.NET高级课程笔记)委托、事件总结
    windows server 2008 磁盘挂载
    Git的使用
    VLAN原理详解
    Jumpserver的部署和使用
    进程与线程的理解
  • 原文地址:https://www.cnblogs.com/mzyy1001/p/13561858.html
Copyright © 2020-2023  润新知