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

Author

FENG, Jingyi
 
解题:并查集
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
int fa[maxn],dis[maxn],sum[maxn],mp[maxn],t,n, m;
void init() {
for(int i = ; i <= n+m; i++) {
fa[i] = i;
mp[i] = i;
dis[i] = ;
sum[i] = ;
}
t = n+;
}
int Find(int x) {
if(fa[x] != x) {
int root = Find(fa[x]);
dis[x] += dis[fa[x]];
fa[x] = root;
}
return fa[x];
}
int main() {
char st[];
while(~scanf("%d %d",&n, &m)) {
init();
int x, y;
for(int i = ; i < m; i++) {
scanf("%s",st);
if(st[] == '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]+;
}
} else if(st[] == 'Q') {
scanf("%d %d",&x, &y);
x = mp[x];
y = mp[y];
if(Find(x) != Find(y)) puts("Unknown");
else {
if(abs(dis[x]-dis[y])&) puts("Different");
else puts("Same");
}
} else if(st[] == 'D') {
scanf("%d",&x);
int tx = mp[x];
tx = Find(tx);
sum[tx] -= ;
mp[x] = ++t;
} else if(st[] == 'S') {
scanf("%d",&x);
x = mp[x];
int tx = Find(x);
printf("%d\n",sum[tx]);
}
}
}
return ;
}

最新文章

  1. Web性能测试的简介
  2. Android线程处理
  3. 三大UML建模工具Visio、Rational Rose、PowerDesign的区别
  4. Divide and conquer:Dropping tests(POJ 2976)
  5. c# 类型初始值设定项引发异常
  6. JS原型与原型链终极详解(转)
  7. xcode新建项目介绍
  8. C# webBrowser操作 javascript
  9. 单例模式及C++实现代码
  10. Swift实现JSON转Model - HandyJSON使用讲解
  11. 全站 HTTPS 没你想象的那么简单
  12. pandas读取excel中指定数据的行数
  13. lvm管理:扩展lv、删除pv、lv等
  14. Springboot项目修改html后不需要重启---springboot项目的热部署
  15. CSS3实战之box-sizing
  16. Sping框架初步使用1
  17. select 自动选择 检查下拉列表
  18. 再也不学AJAX了!(二)使用AJAX
  19. STM32F030 使用硬件 SPI
  20. 2017年10月26日 git上传文件失败的文件

热门文章

  1. iphone 开发Categories 、Extensions 区别 --转
  2. nodejs的mysql模块学习笔记(结合业务)
  3. WPF学习12:基于MVVM Light 制作图形编辑工具(3)
  4. unity内存管理
  5. 开发原生安卓cordova插件(有原生界面)
  6. Android性能分析工具Profile GPU rendering详细介绍
  7. Openrisc的or1200
  8. TFS强制删除离职人员签出锁定项的方法(转)
  9. 原创:E325: ATTENTION vim超完整超给力的问题与解决方法
  10. instance of type of object.prototype.tostring 区别