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;
}

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");
                }
            }
        }
    }
    return 0;
}

ZOJ 3789 Gears

时间: 2024-08-14 22:17:50

ZOJ 3789 Gears的相关文章

ZOJ 3789 Abs Problem

有时候像这种题,没有明显的思路,感觉像规律题.那么先暴力打表,再找规律就很快了. 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3798 先上我的暴力打表,这种肯定是TLE的,只用它发现规律就好了. #include<cstdio> #include<cstring> #include<algorithm> #define INF 0x3f3f3f3f #define N 100 us

ZOJ 3789 并查集

点击打开链接 题意:只说那几个操作把,L将u与v连接,若u左旋,则v右旋,不会出现不合法的条件,Q问u与v的关系,若已知的条件不能判断在则Unknown,旋转方向不一样则Different,一样则Same,然后还有个查询S,问当前u所在的集合的元素个数,D则为删除,但删除后不改变集合其它元素的关系 思路:这题的方向我们可以用到根节点距离来表示,而距离的统计可以直接在路径压缩时完成,元素个数在合并时也可以完成,有点不好想的是这个删除的操作的办法,因为在删除时有可能就将根节点删除了,那么我们可以在找

xtu read problem training 3 B - Gears

Gears Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 378964-bit integer IO format: %lld      Java class name: Main Bob has N (1 ≤ N ≤ 2*105) gears (numbered from 1 to N). Each gear can rotate clockwise or co

zoj题目分类

饮水思源---zoj 转载自:http://bbs.sjtu.edu.cn/bbscon,board,ACMICPC,file,M.1084159773.A.html 注:所有不是太难的题都被归成了“简单题”,等到发现的时候已经太晚了,我太死脑筋 了……:( 有些题的程序我找不到了,555……:( SRbGa的题虽然都很经典……但是由于其中的大部分都是我看了oibh上的解题报告后做 的,所以就不写了…… 题目排列顺序没有规律……:( 按照个人感觉,最短路有的算做了DP,有的算做了图论. 有些比较

ZOJ Monthly, June 2014 解题报告

A.Another Recurrence Sequence B.Gears 题目大意:有n个齿轮,一开始各自为一组,之后进行m次操作,包括以下4种类型: 1.合并两组齿轮,合并的两个应该反向旋转 2.把某个齿轮从所在组删除,自为一组,但不影响同组其它齿轮的状态与关系 3.询问两个齿轮是同向.反向或无关系(即不在同一组) 4.询问某个齿轮所在组的齿轮总数 分析:典型的并查集操作,但是注意两点: 1.由于操作3要询问两个齿轮的相对状态,因此对并查集中每个元素应当保存它的状态信息.状态是相对的,只需要

概率dp ZOJ 3640

Help Me Escape Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3640 Appoint description:  System Crawler  (2014-10-22) Description Background     If thou doest well, shalt thou not be accepted? an

zoj 2156 - Charlie&#39;s Change

题目:钱数拼凑,面值为1,5,10,25,求组成n面值的最大钱币数. 分析:dp,01背包.需要进行二进制拆分,否则TLE,利用数组记录每种硬币的个数,方便更新. 写了一个 多重背包的 O(NV)反而没有拆分快.囧,最后利用了状态压缩优化 90ms: 把 1 cents 的最后处理,其他都除以5,状态就少了5倍了. 说明:貌似我的比大黄的快.(2011-09-26 12:49). #include <stdio.h> #include <stdlib.h> #include <

ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 最小生成树 Kruskal算法

题目链接:ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 Building a Space Station Time Limit: 2 Seconds      Memory Limit: 65536 KB You are a member of the space station engineering team, and are assigned a task in the construction process of the statio

ZOJ 3607 Lazier Salesgirl (贪心)

Lazier Salesgirl Time Limit: 2 Seconds      Memory Limit: 65536 KB Kochiya Sanae is a lazy girl who makes and sells bread. She is an expert at bread making and selling. She can sell the i-th customer a piece of bread for price pi. But she is so lazy