ZOJ 3789 Gears


并查集,

删除节点操作,可以用新建节点代替

维护每个点到跟节点的距离


Gears

Time Limit: 2 Seconds       Memory Limit: 65536 KB

Bob has N (1 ≤ N ≤ 2*105) gears (numbered from 1 to N). Each gear can rotate clockwise or counterclockwise. Bob thinks that assembling gears is much more exciting than just playing with a single one. Bob wants to put some gears into some groups. In each gear group, each gear has a specific rotation respectively, clockwise or counterclockwise, and as we all know, two gears can link together if and only if their rotations are different. At the beginning, each gear itself is a gear group.

Bob has M (1 ≤ N ≤ 4*105) operations to his gears group:

  • "L u v". Link gear u and gear v. If two gears link together, the gear groups which the two gears come from will also link together, and it makes a new gear group. The two gears will have different rotations. BTW, Bob won't link two gears with the same rotations together, such as linking (a, b), (b, c), and (a, c). Because it would shutdown the rotation of his gears group, he won't do that.
  • "D u". Disconnect the gear u. Bob may feel something wrong about the gear. He will put the gear away, and the gear would become a new gear group itself and may be used again later. And the rest of gears in this group won't be separated apart.
  • "Q u v". Query the rotations between two gears u and v. It could be "Different", the "Same" or "Unknown".
  • "S u". Query the size of the gears, Bob wants to know how many gears there are in the gear group containing the gear u.

Since there are so many gears, Bob needs your help.

Input

Input will consist of multiple test cases. In each case, the first line consists of two integers N and M. Following M lines, each line consists of one of the operations which are described above. Please process to the end of input.

Output

For each query operation, you should output a line consist of the result.

Sample Input
3 7
L 1 2
L 2 3
Q 1 3
Q 2 3
D 2
Q 1 3
Q 2 3
5 10
L 1 2
L 2 3
L 4 5
Q 1 2
Q 1 3
Q 1 4
S 1
D 2
Q 2 3
S 1

Sample Output
Same
Different
Same
Unknown
Different
Same
Unknown
3
Unknown
2

Hint

Link (1, 2), (2, 3), (4, 5), gear 1 and gear 2 have different rotations, and gear 2 and gear 3 have different rotations, so we can know gear 1 and gear 3 have the same rotation, and we didn't link group (1, 2, 3) and group (4, 5), we don't know the situation about gear 1 and gear 4. Gear 1 is in the group (1, 2, 3), which has 3 gears. After putting gear 2 away, it may have a new rotation, and the group becomes (1, 3).


Author:  FENG, Jingyi
Source:  ZOJ Monthly, June 2014



#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn=600600;

int Sz[maxn],dist[maxn],fa[maxn],now[maxn],next;
int n,m;

void init(int n,int m)
{
    memset(dist,0,sizeof(dist));
    next=n+1;
    for(int i=1;i<=n+m;i++)
    {
        Sz[i]=1;fa[i]=i;
        if(i<=n) now[i]=i;
    }
}

int Find(int x)
{
    if(fa[x]==x) return x;
    int temp=Find(fa[x]);
    dist[x]+=dist[fa[x]];///到gen节点的距离
    return fa[x]=temp;
}

void Union(int x,int y)
{
    int X=Find(x),Y=Find(y);
    if(X==Y) return ;
    fa[X]=Y;
    Sz[Y]+=Sz[X];
    dist[X]=dist[Y]+1;
    dist[x]=dist[y]+1;
}

void debug()
{
    for(int i=1;i<=5;i++)
    {
        Find(now[i]);
        cout<<i<<" : "<<now[i]<<" :: "<<dist[now[i]]<<endl;
    }
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        init(n,m);
        char op[5];int a,b;
        while(m--)
        {
            scanf("%s",op);
            if(op[0]=='L')
            {
                scanf("%d%d",&a,&b);
                int na=now[a],nb=now[b];
                Union(na,nb);
            }
            else if(op[0]=='D')
            {
                scanf("%d",&a);
                int na=now[a];
                int A=Find(na);
                Sz[A]--;Sz[na]=0;
                now[a]=next++;
            }
            else if(op[0]=='S')
            {
                scanf("%d",&a);
                int na=now[a];
                int A=Find(na);
                printf("%d\n",Sz[A]);
            }
            else if(op[0]=='Q')
            {
                scanf("%d%d",&a,&b);
                int na=now[a],nb=now[b];
                int A=Find(na),B=Find(nb);
                if(A!=B) puts("Unknown");
                else
                {
                    if ((dist[na]-dist[nb])&1) puts("Different");
                    else puts("Same");
                }
            }
      ///      debug();
        }
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值