xtu read problem training 3 B - Gears

Gears

Time Limit: 2000ms

Memory Limit: 65536KB

This problem will be judged on ZJU. Original ID: 3789
64-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 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).

Source

ZOJ Monthly, June 2014

Author

FENG, Jingyi

解题:并查集

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #define LL long long
13 #define INF 0x3f3f3f3f
14 using namespace std;
15 const int maxn = 1010000;
16 int fa[maxn],dis[maxn],sum[maxn],mp[maxn],t,n, m;
17 void init() {
18     for(int i = 0; i <= n+m; i++) {
19         fa[i] = i;
20         mp[i] = i;
21         dis[i] = 0;
22         sum[i] = 1;
23     }
24     t = n+1;
25 }
26 int Find(int x) {
27     if(fa[x] != x) {
28         int root = Find(fa[x]);
29         dis[x] += dis[fa[x]];
30         fa[x] = root;
31     }
32     return fa[x];
33 }
34 int main() {
35     char st[10];
36     while(~scanf("%d %d",&n, &m)) {
37         init();
38         int x, y;
39         for(int i = 0; i < m; i++) {
40             scanf("%s",st);
41             if(st[0] == ‘L‘) {
42                 scanf("%d %d",&x, &y);
43                 x = mp[x];
44                 y = mp[y];
45                 int tx = Find(x);
46                 int ty = Find(y);
47                 if(tx != ty) {
48                     sum[tx] += sum[ty];
49                     fa[ty] = tx;
50                     dis[ty] = dis[x]+dis[y]+1;
51                 }
52             } else if(st[0] == ‘Q‘) {
53                 scanf("%d %d",&x, &y);
54                 x = mp[x];
55                 y = mp[y];
56                 if(Find(x) != Find(y)) puts("Unknown");
57                 else {
58                     if(abs(dis[x]-dis[y])&1) puts("Different");
59                     else puts("Same");
60                 }
61             } else if(st[0] == ‘D‘) {
62                 scanf("%d",&x);
63                 int tx = mp[x];
64                 tx = Find(tx);
65                 sum[tx] -= 1;
66                 mp[x] = ++t;
67             } else if(st[0] == ‘S‘) {
68                 scanf("%d",&x);
69                 x = mp[x];
70                 int tx = Find(x);
71                 printf("%d\n",sum[tx]);
72             }
73         }
74     }
75     return 0;
76 }

xtu read problem training 3 B - Gears

时间: 2024-10-12 22:13:46

xtu read problem training 3 B - Gears的相关文章

xtu read problem training 4 B - Multiplication Puzzle

Multiplication Puzzle Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 165164-bit integer IO format: %lld      Java class name: Main The multiplication puzzle is played with a row of cards, each containing a s

xtu read problem training 4 A - Moving Tables

Moving Tables Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 102964-bit integer IO format: %lld      Java class name: Main The famous ACM (Advanced Computer Maker) Company has rented a floor of a building wh

xtu read problem training 2 B - In 7-bit

In 7-bit Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 371364-bit integer IO format: %lld      Java class name: Main Very often, especially in programming contests, we treat a sequence of non-whitespace cha

xtu read problem training 3 A - The Child and Homework

The Child and Homework Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 437A64-bit integer IO format: %I64d      Java class name: (Any) Once upon a time a child got a test consisting of multiple-choice

xtu read problem training A - Dividing

A - Dividing Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Description Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles.

xtu read problem training B - Tour

B - Tour Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Description John Doe, a skilled pilot, enjoys traveling. While on vacation, he rents a small plane and starts visiting beautiful places. To save money, John must de

2014 Super Training #8 A Gears --并查集

题意: 有N个齿轮,三种操作1.操作L x y:把齿轮x,y链接,若x,y已经属于某个齿轮组中,则这两组也会合并.2.操作Q x y:询问x,y旋转方向是否相同(等价于齿轮x,y的相对距离的奇偶性).3.操作D x :拆下齿轮x,并且x所在的齿轮组不会断开4.操作S x : 查询齿轮x所在的齿轮组有多少齿轮.并查集,维护父节点的同时,dis记录一下每个节点到根节点的距离,并且用num记录一下以x为根节点的集合有多少个元素. 由于涉及到删除操作,删除的是根节点的话会导致信息丢失,所以在删除的时候直

A Gentle Guide to Machine Learning

A Gentle Guide to Machine Learning Machine Learning is a subfield within Artificial Intelligence that builds algorithms that allow computers to learn to perform tasks from data instead of being explicitly programmed. Got it? We can make machines lear

A Rigorous &amp; Readable Review on RNNs

A Rigorous & Readable Review on RNNs This post introduces a new Critical Review on Recurrent Neural Networks for Sequence Learning. Twelve nights back, while up late preparing pretty pictures for a review onRecurrent Neural Networks for Sequence Lear