zoj Gears 神奇并查集+删除+加根节点距离记录
来源:互联网 发布:csv导入mysql中文乱码 编辑:程序博客网 时间:2024/04/29 15:57
今天一直在练习并查集,发现这道题有点神奇,感觉这题出的特别好~~~~
Click here:
题意就就不说了:附上代码
#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;
}
- zoj Gears 神奇并查集+删除+加根节点距离记录
- ZOJ 3789 Gears (并查集 合并+删除+离根节点的距离)
- ZOJ 3789 Gears 并查集
- ZOJ 3789 Gears(并查集)
- 【并查集】 ZOJ 3789 Gears
- ZOJ 3789 Gears(并查集)
- ZOJ 3789 - Gears (并查集)
- zoj3789 Gears并查集
- zoj 3789 并查集 删除操作
- 并查集 ,删除节点(图画展示)
- 并查集(删除节点)
- 并查集节点删除于添加
- HDU:4707 Pet(并查集+某元素到根节点的距离计算)
- ZOJ 3659 Conquera New Region(并查集:维护根节点信息)
- (并查集+记录到根的距离)Dragon Balls--HDOJ
- POJ 1611 记录节点数的并查集
- hdu2473 Junk-Mail Filter (并查集删除节点)
- UVA 11987 Almost Union-Find 并查集节点删除
- HTML5事件
- UDP用打洞技术穿透NAT的原理与实现
- Java线程:新特征-原子量
- Android推送方案分析(
- Unity3D 播放视频
- zoj Gears 神奇并查集+删除+加根节点距离记录
- UDP Hole Puching技术:穿透防火墙建立UDP连接
- NYOJ-269 VF
- Effective C++ Item 31 降低文件间编译依存关系
- How to Display Image In Picturebox in VC++ from Iplimage and Mat
- 最近写的触发器,保存一下
- 国外的一些struts面试题1
- Java基础测试第9题有点疑虑
- 动态数组索引越界问题