xtu read problem training 3 B - Gears
Gears
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
#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的更多相关文章
- 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 ...
- 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 ...
- xtu read problem training 4 A - Moving Tables
Moving Tables Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ...
- xtu read problem training 4 B - Multiplication Puzzle
Multiplication Puzzle Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. O ...
- xtu read problem training B - Tour
B - Tour Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descriptio ...
- xtu read problem training A - Dividing
A - Dividing Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Descri ...
- 2014 Super Training #8 A Gears --并查集
题意: 有N个齿轮,三种操作1.操作L x y:把齿轮x,y链接,若x,y已经属于某个齿轮组中,则这两组也会合并.2.操作Q x y:询问x,y旋转方向是否相同(等价于齿轮x,y的相对距离的奇偶性). ...
- A Gentle Guide to Machine Learning
A Gentle Guide to Machine Learning Machine Learning is a subfield within Artificial Intelligence tha ...
- Bias vs. Variance(1)--diagnosing bias vs. variance
我们的函数是有high bias problem(underfitting problem)还是 high variance problem(overfitting problem),区分它们很得要, ...
随机推荐
- 线段树+扫描线 HDOJ 5091 Beam Cannon(大炮)
题目链接 题意: 给出若干个点的坐标,用一个W*H的矩形去覆盖,问最多能覆盖几个点. 思路: 这是2014上海全国邀请赛的题目,以前写过,重新学习扫描线.首先把所有点移到第一象限([0, 40000] ...
- Eclipse Neon Java版本安装Java EE插件
Help→Install New Software 地址:Neon - http://download.eclipse.org/releases/neon/201703231000 选择 Web,X ...
- Excel 宏练习
任务描述: 利用 Excel 绘制函数图像 f(x)=x^2/3+0.9*(3.3-x^2)^1/2*sin(a*x),并通过按钮事件来刷新图像. 问题分析: 可以参考类似 Matlab 绘图的方式, ...
- and or类比c中的 bool?a :b
a = "heaven" b = "hell" c = True and a or b print c d = False and a or b ...
- 真tm无聊,这几天。。。
临近期末了,每天都要和学霸一起上自习. 很不喜欢学习和自己未来没多大用的东西 老师画的那些重点是对我们好吗~ 每天感觉都在折磨自己,不想学,学不进去,心里很烦躁,浮躁. 人生苦短->_-> ...
- 项目错误提示Multiple markers at this line
新安装个Myeclipse,导入以前做的程序后程序里好多错,第一行提示: Multiple markers at this line - The type java.lang.Obje ...
- 组件的 state 和 setState
state 我们前面提到过,一个组件的显示形态是可以由它数据状态和配置参数决定的.一个组件可以拥有自己的状态,就像一个点赞按钮,可以有“已点赞”和“未点赞”状态,并且可以在这两种状态之间进行切换.Re ...
- 页面html图片按钮多种写法
原地址:http://blog.163.com/weison_hi/blog/static/17680404720118534033788/ 第一种: 在一般情况下按钮提交表单: <form i ...
- Dev GridView 属性说明
说明 Options OptionsBehavior 视图的行为选项 AllowIncrementalSearch 允许用户通过输入想得到的列值来定位行 AllowPartialRedrawOnS ...
- pre-network android 网络优化预加载框架
网络优化是所有app开发中非常重要的一部分,如果将网络请求前置就是在点击跳转activity之前开始网络加载那么速度将会有质的提升.也就是网络预先加载框框架. 网络预加载框架,监听式网络前置加载框架- ...