Codeforces Gym 101194G Pandaria (2016 ACM-ICPC EC-Final G题, 并查集 + 线段树合并)
题目链接 2016 ACM-ICPC EC-Final Problem G
题意 给定一个无向图。每个点有一种颜色。
现在给定$q$个询问,每次询问$x$和$w$,求所有能通过边权值不超过$w$的边走到$x$的点的集合中,哪一种颜色的点出现的次数最多。
次数相同时输出编号最小的那个颜色。强制在线。
求哪种颜色可以用线段树合并搞定。
关键是这个强制在线。
当每次询问的时候,我们先要求出最小生成树在哪个时刻恰好把边权值不超过$w$的边都用并查集合并了。
在做最小生成树的时候每合并两个节点,另外开一个新的结点,原来两个点的父亲都指向这个新的结点。
然后倍增预处理,用类似求$LCA$的方法来得到询问的那个时刻。
时间复杂度$O(nlogn)$
#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 = 2e5 + 10;
const int M = N * 20; struct node{
int x, y, z;
void scan(){
scanf("%d%d%d", &x, &y, &z);
}
friend bool operator < (const node &a, const node &b){
return a.z < b.z;
}
} e[N]; int T, ca = 0;
int tot;
int n, m, q, ans;
int c[N], root[N], v[N], father[N];
int ls[M], rs[M], mx[M], ret[M];
int id, res[N];
int f[N][19]; int getfather(int x){
return father[x] == x ? x : father[x] = getfather(father[x]);
} void up(int i){
mx[i] = max(mx[ls[i]], mx[rs[i]]);
if (mx[i] == mx[ls[i]]) ret[i] = ret[ls[i]];
else ret[i] = ret[rs[i]];
} int build(int l, int r, int val){
int x = ++tot;
ls[x] = rs[x] = mx[x] = ret[x] = 0;
if (l == r){
mx[x] = 1;
ret[x] = val;
return x;
} int mid = (l + r) >> 1;
if (val <= mid) ls[x] = build(l, mid, val);
else rs[x] = build(mid + 1, r, val);
up(x);
return x;
} int Merge(int x, int y, int l, int r){
if (x == 0 || y == 0) return x + y;
if (l == r){
mx[x] += mx[y];
return x;
} int mid = (l + r) >> 1;
ls[x] = Merge(ls[x], ls[y], l, mid);
rs[x] = Merge(rs[x], rs[y], mid + 1, r);
up(x);
return x;
} int main(){ scanf("%d", &T);
while (T--){
tot = 0;
scanf("%d%d", &n, &m);
rep(i, 1, n) scanf("%d", c + i); rep(i, 1, n){
father[i] = i;
v[i] = 0;
f[i][0] = i;
root[i] = build(1, n, c[i]);
res[i] = ret[root[i]];
} id = n;
rep(i, 1, m) e[i].scan(); sort(e + 1, e + m + 1); rep(i, 1, m){
int x = e[i].x, y = e[i].y, z = e[i].z;
int fx = getfather(x), fy = getfather(y);
if (fx ^ fy){
++id;
f[id][0] = id;
father[id] = id;
v[id] = z;
father[fx] = father[fy] = id;
f[fx][0] = f[fy][0] = id;
root[id] = Merge(root[fx], root[fy], 1, n);
res[id] = ret[root[id]];
}
} rep(j, 1, 17){
rep(i, 1, id) f[i][j] = f[f[i][j - 1]][j - 1];
} printf("Case #%d:\n", ++ca);
scanf("%d", &q);
ans = 0;
while (q--){
int x, w;
scanf("%d%d", &x, &w);
x ^= ans, w ^= ans;
dec(i, 17, 0) if (v[f[x][i]] <= w) x = f[x][i];
printf("%d\n", ans = res[x]);
}
} return 0;
}
Codeforces Gym 101194G Pandaria (2016 ACM-ICPC EC-Final G题, 并查集 + 线段树合并)的更多相关文章
- ACM ICPC China final G Pandaria
目录 ACM ICPC China final G Pandaria ACM ICPC China final G Pandaria 题意:给一张\(n\)个点\(m\)条边的无向图,\(c[i]\) ...
- Codeforces 571D - Campus(并查集+线段树+DFS 序,hot tea)
Codeforces 题目传送门 & 洛谷题目传送门 看到集合的合并,可以本能地想到并查集. 不过这题的操作与传统意义上的并查集不太一样,传统意义上的并查集一般是用来判断连通性的,而此题还需支 ...
- Vladik and Entertaining Flags CodeForces - 811E (并查集,线段树)
用线段树维护每一块左右两侧的并查集, 同色合并时若不连通则连通块数-1, 否则不变 #include <iostream> #include <algorithm> #incl ...
- 2016 ACM/ICPC Asia Regional Shenyang Online 1003/HDU 5894 数学/组合数/逆元
hannnnah_j’s Biological Test Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...
- 2016 ACM/ICPC Asia Regional Qingdao Online 1001/HDU5878 打表二分
I Count Two Three Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 2016 ACM/ICPC Asia Regional Shenyang Online 1009/HDU 5900 区间dp
QSC and Master Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- 2016 ACM/ICPC Asia Regional Shenyang Online 1007/HDU 5898 数位dp
odd-even number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- 2016 ACM/ICPC Asia Regional Dalian Online 1002/HDU 5869
Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K ( ...
- 2016 ACM/ICPC Asia Regional Dalian Online 1006 /HDU 5873
Football Games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
随机推荐
- python_ 运算符与分支结构
# 运算符与分支结构 ### 运算符 - 赋值运算符 - 用'='表示,左边只能是变量. - 算术运算符 - +.-.*:加.减.乘 - /:除法运算,结果是浮点数 - //:除法运算,结果是整数 - ...
- 第三方库的安装:Pangolin
Pangolin: 一款开源的OPENGL显示库,可以用来视频显示.而且开发容易. 代码我们可以从Github 进行下载:https://github.com/stevenlovegrove/Pang ...
- 第一章 C++编程基础
第一章 C++编程基础 1.1 如何撰写C++程序 赋值 assignment复合赋值 (compound assignment) += 函数(function)是一块独立的程序代码序列(code s ...
- 软工实践 - 第十八次作业 Alpha 冲刺 (9/10)
队名:起床一起肝活队 组长博客:https://www.cnblogs.com/dawnduck/p/10035464.html 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过 ...
- FormsAuthentication类
理解代码: string cookieName = FormsAuthentication.FormsCookieName; FormsAuthentication类说明: // 摘要: // 为 W ...
- CentOS7 设置开机直接进入命令行界面
上网查询centsos设置开机直接进入命令行界面的方法都说修改/etc/inittab文件,将文件中的“ :id:5:initdefault:”改为“ :id:3:initdefault:”,即将默认 ...
- BZOJ 1208 [HNOI2004]宠物收养所 | SPlay模板题
题目: 洛谷也能评 题解: 记录一下当前树维护是宠物还是人,用Splay维护插入和删除. 对于任何一次询问操作都求一下value的前驱和后继(这里前驱和后继是可以和value相等的),比较哪个差值绝对 ...
- PHP:在class中定义常量注意事项
一.不能在成员函数中定义常量,否则会引发诡异地语法错误 syntax error, unexpected 'CONST' (T_CONST) 示例 /* 错误的方式 */ class A { publ ...
- Codeforces Round #504:D. Array Restoration
D. Array Restoration 题目链接:https://codeforces.com/contest/1023/problem/D 题意: 给出一个序列,现在要求对一个全为0的序列执行q次 ...
- linux进程服务监测流程
进程->端口监听->查阿里云端口开放->看防火墙 ps -ef | grep redis ->netstat -an |grep redis->安全组设置端口放行规则 ...