ZOJ 3789 Gears(并查集)

题目大意:给你一些齿轮,然后有的可也组成齿轮组,每个齿轮能够组合的条件是一个为顺时针转动,一个为逆时针转动。给你一些操作:

L:给你u,v把u,v连在一起;

D:给你一个u,把u从齿轮组中拿出来;

Q:给你u,v。让你判断u和v的转动方向是否相同;(注意:这里判断要首先是同祖先的,然后通过深度差判断,差为偶数则相同,否则不同)

S:给你一个u,让你求出这个齿轮所在的齿轮组中一共有多少的齿轮。


解题思路:在保存祖先的同时在用dis数组记录当前节点与祖先节点的距离,同时注意,我们并不是真正的删除节点我们只是用其他的数子,来代替了这个节点所以。需要用一个mp数组转换一下,给的数和实际意义上存的数字。其他的就很好求了啊,注意要压缩路径。

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).


#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-7
#define M 1000100
#define LL __int64
#define INF 0x3f3f3f3f
#define PI 3.1415926535898

const int maxn = 1010000;

using namespace std;

int fa[maxn];
int dis[maxn];
int sum[maxn];
int mp[maxn];
int t;
int n, m;

void init()
{
    for(int i = 0; i <= n+m; i++)
    {
        fa[i] = i;
        mp[i] = i;
        dis[i] = 0;
        sum[i] = 1;
    }
    t = n+1;
}

int Find(int x)
{
    if(fa[x] != x)
    {
        int root = Find(fa[x]);
        dis[x] += dis[fa[x]];
        return fa[x] = root;
    }
    return x;
}

int main()
{
    char st[10];
    while(~scanf("%d %d",&n, &m))
    {
        init();
        int x, y;
        for(int i = 0; i < m; i++)
        {
            scanf("%s",st);
            if(st[0] == 'L')
            {
                scanf("%d %d",&x, &y);
                x = mp[x];
                y = mp[y];
                int tx = Find(x);
                int ty = Find(y);
                if(tx != ty)
                {
                    sum[tx] += sum[ty];
                    fa[ty] = tx;
                    dis[ty] = dis[x]+dis[y]+1;
                }
            }
            else if(st[0] == 'Q')
            {
                scanf("%d %d",&x, &y);
                x = mp[x];
                y = mp[y];
                int tx = Find(x);
                int ty = Find(y);
                if(tx != ty)
                    puts("Unknown");
                else
                {
                    if(abs(dis[x]-dis[y])%2 == 0)
                        puts("Same");
                    else
                        puts("Different");
                }
            }
            else if(st[0] == 'D')
            {
                scanf("%d",&x);
                int xx = mp[x];
                int tx = Find(xx);
                sum[tx] -= 1;
                mp[x] = ++t;
            }
            else if(st[0] == 'S')
            {
                scanf("%d",&x);
                x = mp[x];
                int tx = Find(x);
                printf("%d\n",sum[tx]);
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值