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

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

  1. 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 Code ...

  2. 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: 3 ...

  3. xtu read problem training 4 A - Moving Tables

    Moving Tables Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ...

  4. xtu read problem training 4 B - Multiplication Puzzle

    Multiplication Puzzle Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. O ...

  5. xtu read problem training B - Tour

    B - Tour Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Descriptio ...

  6. xtu read problem training A - Dividing

    A - Dividing Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Descri ...

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

    题意: 有N个齿轮,三种操作1.操作L x y:把齿轮x,y链接,若x,y已经属于某个齿轮组中,则这两组也会合并.2.操作Q x y:询问x,y旋转方向是否相同(等价于齿轮x,y的相对距离的奇偶性). ...

  8. A Gentle Guide to Machine Learning

    A Gentle Guide to Machine Learning Machine Learning is a subfield within Artificial Intelligence tha ...

  9. Bias vs. Variance(1)--diagnosing bias vs. variance

    我们的函数是有high bias problem(underfitting problem)还是 high variance problem(overfitting problem),区分它们很得要, ...

随机推荐

  1. 水题 Codeforces Round #286 (Div. 2) A Mr. Kitayuta's Gift

    题目传送门 /* 水题:vector容器实现插入操作,暴力进行判断是否为回文串 */ #include <cstdio> #include <iostream> #includ ...

  2. 在solr客户端删除库中的数据

    1.在solr客户端,访问你的索引库(我认为最方便的方法) 1)documents type 选择 XML 2)documents 输入下面语句<delete><query>* ...

  3. 可能是最漂亮的Spring事务管理详解 专题

    微信阅读地址链接:可能是最漂亮的Spring事务管理详解 事务概念回顾 什么是事务? 事务是逻辑上的一组操作,要么都执行,要么都不执行. 事物的特性(ACID): 原子性: 事务是最小的执行单位,不允 ...

  4. android开发学习 ------- git - 将代码回滚到任意版本

    不小心将一个东西错误提交到git - 远程仓库上 参考  https://www.cnblogs.com/wancy86/p/5848024.html 你的git可能关联了多个远程仓库,每个关联的代码 ...

  5. 第一个 swift 项目

    今天 学习了 一丢丢 swift,特此记录一下 ! 原来创建的时候 ,只要把 语言 由以前的Object-C改为Swift,变创建好了自己的swift工程 第一个简单的swift demo 上代码 i ...

  6. Springboot + Websocket + Sockjs + Stomp + Vue + Iview 实现java后端日志显示在前端web页面上

    话不多说,看代码. 一.pom.xml 引入spring boot websocket依赖 <dependency> <groupId>org.springframework. ...

  7. [ CQOI 2018 ] 异或序列

    \(\\\) Description 给出一个长为 \(n\) 的数列 \(A\) 和 \(k\),多次询问: 对于一个区间 \([L_i,R_i]\),问区间内有多少个不为空的子段异或和为 \(k\ ...

  8. 2018.7.21NOIP模拟赛?解题报告

    题面 预计得分:70 + 60 + 30 = 160 实际得分:40 + 60 + 0 = 100 T1数组开小了 T2比赛结束后5min AC T3加了个记忆话搜索wa了.. T1 zbq吊打std ...

  9. NPM、nodeJS安装,grunt自动化构建工具学习总结

    一:安装 npm是随nodeJs安装包一起安装的包管理工具,能解决NodeJS代码部署上的很多问题: 常见的使用场景有以下几种: 允许用户从NPM服务器下载别人编写的第三方包到本地使用. 允许用户从N ...

  10. 洛谷 P1765 手机_NOI导刊2010普及(10)

    题目描述 一般的手机的键盘是这样的: 1 2 abc 3 def 4 ghi 5 jkl 6 mno 7 pqrs 8 tuv 9 wxyz * 0 # 要按出英文字母就必须要按数字键多下.例如要按出 ...