题目链接  道路修建 EXT

考虑并查集的启发式合并,合并的时候小的子树的根成为大的子树的根的儿子。

可以证明这样整棵树的深度不会超过$logn$。

两个根合并的时候,产生的新的边的边权为当前的时间。

那么询问的时候答案就为$x$到$y$的最短路径上的所有边的边权最大值。

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) const int N = 5e5 + 10; int T;
int n, m;
int num;
int ans;
int father[N], c[N], root[N], deep[N], sz[N];
int now;
unordered_set <int> s[N]; int main(){ scanf("%d", &T);
while (T--){
scanf("%d%d", &n, &m);
num = n;
ans = 0;
rep(i, 0, n + 1){
s[i].clear();
s[i].insert(i);
} rep(i, 1, n){
c[i] = 0;
sz[i] = 1;
father[i] = i;
deep[i] = 1;
root[i] = i;
} rep(i, 1, m){
int op, x, y;
scanf("%d%d%d", &op, &x, &y);
x ^= ans;
y ^= ans; if (op == 0){
if (root[x] == root[y]){
printf("%d\n", ans = num);
continue;
} --num;
int fx = root[x], fy = root[y];
if (sz[fx] < sz[fy]){
swap(fx, fy);
swap(x, y);
} c[fy] = i; sz[fx] += sz[fy];
father[fy] = fx;
sz[fy] = 0;
for (auto u : s[fy]){
root[u] = fx;
++deep[u];
s[fx].insert(u);
} s[fy].clear();
printf("%d\n", ans = num);
} else{
if (root[x] != root[y]){
printf("%d\n", ans = 0);
continue;
} now = 0;
if (deep[x] < deep[y]) swap(x, y);
while (deep[x] != deep[y]){
now = max(now, c[x]);
x = father[x];
} while (true){
if (x == y) break;
now = max(now, c[x]);
now = max(now, c[y]);
x = father[x];
y = father[y];
} printf("%d\n", ans = now);
}
}
} return 0;
}

  

quailty's Contest #1 道路修建 EXT(启发式合并)的更多相关文章

  1. 【BZOJ-2435】道路修建 (树形DP?)DFS

    2435: [Noi2011]道路修建 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3115  Solved: 1002[Submit][Statu ...

  2. 【bzoj2435】[NOI2011]道路修建

    题目描述 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他们只愿意修建恰好 n – 1条双向道路. 每条道路的修 ...

  3. 【NOI2011】道路修建 BFS

    [NOI2011]道路修建 Description 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他们只愿意修建 ...

  4. 【BZOJ】2435: [Noi2011]道路修建(树形dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2435 我怎么感觉那么水.. 坑的是,dfs会爆...好吧..用bfs.. //upd:我的智商也是醉 ...

  5. bzoj 2435: [Noi2011]道路修建 树上 dp

    2435: [Noi2011]道路修建 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...

  6. 2435: [Noi2011]道路修建 - BZOJ

    Description 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他们只愿意修建恰好 n – 1条双向道路. ...

  7. NOI2011道路修建

    2435: [Noi2011]道路修建 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1974  Solved: 550[Submit][Status ...

  8. BZOJ 2435: [Noi2011]道路修建( dfs )

    NOI的水题...直接一遍DFS即可 ------------------------------------------------------------------------- #includ ...

  9. 道路修建 2(自创题+题解)(From NOI2011)

    道路修建这道题想来各位不陌生(传送门在此——Bzoj2435),看了此题,一开始以为是最初各个点处于分散状态,然后做了一下,直到发现标程都有点问题,才发现原题是说本来各点已经处于连接完毕的状态(phi ...

随机推荐

  1. python利用PIL库使图片高斯模糊

    一.安装PIL PIL是Python Imaging Library简称,用于处理图片.PIL中已经有图片高斯模糊处理类,但有个bug(目前最新的1.1.7bug还存在),就是模糊半径写死的是2,不能 ...

  2. python学习笔记一:数据类型

    一.Python文件类型 1.源代码 hello.py: 1 #!/usr/bin/python 2 print "hello world" 2.字节代码:python源文件经编译 ...

  3. 在cmd运行脚本

    1.打开cmd 2.cd到脚本目录,运行所有脚本的上级目录,我的是cd C:\Users\Administrator\PycharmProjects\webtest\TestSuit 3.使用Pyth ...

  4. springboot13 Hikari 和Introspector

    SpringBoot Initializr Introspector(内省) class TestReflect { @Test fun testReflect() { //获取字节码对象 val c ...

  5. c++知识点总结-模板特化

    类模板的全特化与偏特化 类模板 template<typename T1, typename T2> class Test { public: Test(T1 i,T2 j):a(i),b ...

  6. 【AtCoder】AGC012

    AGC012 A - AtCoder Group Contest 从最后开始间隔着取就行 #include <bits/stdc++.h> #define fi first #define ...

  7. Codeforces Round #306 (Div. 2) 550A Two Substrings

    链接:http://codeforces.com/contest/550/problem/A 这是我第一次玩cf这种比赛,前面做了几场练习,觉得div2的前面几个还是比较水的. 所以看到这道题我果断觉 ...

  8. css实现0.5像素

    .border{ position: relative; } .border:before{ content: ''; position: absolute; width: 200%; height: ...

  9. vs2008升级正式版

    1.VS2008简体中文正式版序列号 1.Visual Studio 2008 Professional Edition: XMQ2Y-4T3V6-XJ48Y-D3K2V-6C4WT 2.Visual ...

  10. for 循环里的i++

    写代码的时间也不短了,今天看快速排序的算法的时候才去更深层次得理解... for (语句 1; 语句 2; 语句 3) {     被执行的代码块 } 语句 1 (代码块)开始前执行 语句 2 定义运 ...