Codeforces 828F Best Edge Weight - 随机堆 - 树差分 - Kruskal - 倍增算法
You are given a connected weighted graph with n vertices and m edges. The graph doesn't contain loops nor multiple edges. Consider some edge with id i. Let's determine for this edge the maximum integer weight we can give to it so that it is contained in all minimum spanning trees of the graph if we don't change the other weights.
You are to determine this maximum weight described above for each edge. You should calculate the answer for each edge independently, it means there can't be two edges with changed weights at the same time.
The first line contains two integers n and m (2 ≤ n ≤ 2·105, n - 1 ≤ m ≤ 2·105), where n and m are the number of vertices and the number of edges in the graph, respectively.
Each of the next m lines contains three integers u, v and c (1 ≤ v, u ≤ n, v ≠ u, 1 ≤ c ≤ 109) meaning that there is an edge between vertices u and v with weight c.
Print the answer for each edge in the order the edges are given in the input. If an edge is contained in every minimum spanning tree with any weight, print -1 as the answer.
4 4
1 2 2
2 3 2
3 4 2
4 1 3
2 2 2 1
4 3
1 2 2
2 3 2
3 4 2
-1 -1 -1
题目大意 给定一个无向连通带权图,求每条边在所有最小生成树中的最大权值(如果可以无限大就输出-1)。
对于求1条边在无向连通图带权图的所有最小生成树中的最大权值可以用二分再用Kruskal进行check,但是如果每条边都这么做就会T掉。
所以考虑整体二分,然而并不行。
所以考虑先跑一遍Kruskal,然后分类讨论一下:
1)考虑一条非树边可以取的最大权值。
考虑把它加入树中,那么会形成1个环,为了保证最小生成树的边权和最小,方法是删掉环上权值最大的一条边。
所以找到它连接的两端在树上形成的简单路径中边权最大的一个,它的边权-1就是这条非树边的答案。
这个操作可以用树链剖分或者倍增解决。
2)考虑一条树边可以取到的最大权值
具体考虑一条树边会比较难做(不过好像有同学设计了时间戳把它搞定了),但是对于每条非树边都会对它连接的两端在树上形成的简单路径上的所有边有个边权的限制,就是不能超过它的边权 - 1,否则会被它替换掉。
这个区间取min操作可以用树链剖分。然而考试的时候我脑子瓦特了,觉得线段树不能区间取min(可能是脑补了一个求和操作)
然后想到了只有到最后会一起求得树边的答案,于是想到了差分。
为了维护这个最小值,又想到了可并堆。由于要删除,所以可以用下面这个方法构造可删堆(一位dalao的博客提到这个黑科技,我就学习了一下)
再开一个堆记录要删除的元素,如果两个堆堆顶元素相同,则都弹出堆顶元素。
于是便又有一个名为树差分 + 可并堆的zz做法。
Code
/**
* Codeforces
* Problem#828F
* Accepted
* Time: 420ms
* Memory: 57864k
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <bitset>
#ifdef WIN32
#define Auto "%I64d"
#else
#define Auto "%lld"
#endif
using namespace std;
typedef bool boolean;
#define ll int
#define smin(_a, _b) _a = min(_a, _b)
#define smax(_a, _b) _a = max(_a, _b)
#define fi first
#define sc second
const signed int inf = (signed) (~0u >> );
const signed ll llf = (signed ll) (~0ull >> );
typedef pair<int, int> pii; template<typename T>
inline void readInteger(T& u) {
static char x;
while(!isdigit(x = getchar()));
for(u = x - ''; isdigit(x = getchar()); u = u * + x - '');
} template<typename T>
class Matrix {
public:
T* p;
int row;
int col;
Matrix():p(NULL) { }
Matrix(int row, int col):row(row), col(col) {
p = new T[(row * col)];
} T* operator [] (int pos) {
return p + (pos * col);
}
};
#define matset(a, i, s) memset(a.p, i, sizeof(s) * a.row * a.col) typedef class Edge {
public:
int u;
int v;
int w;
boolean seced;
int rid; Edge(int u = , int v = , int w = ):u(u), v(v), w(w), seced(false) { } boolean operator < (Edge b) const {
return w < b.w;
}
}Edge; typedef class Node {
public:
int val;
Node *nxt[]; Node(int val = inf):val(val) { nxt[] = nxt[] = NULL; }
}Node; typedef pair<Node*, Node*> pnn;
#define limit 1000000 Node pool[limit];
Node *top = pool; Node* newnode(int x) {
if(top == pool + limit)
return new Node(x);
*top = Node(x);
return top++;
} Node* merge(Node* &l, Node* r) {
if(l == NULL) return r;
if(r == NULL) return l;
if(l->val > r->val) swap(l, r);
int p = rand() % ;
l->nxt[p] = merge(l->nxt[p], r);
return l;
} int n, m;
Edge* edge;
int* res;
int* ans; inline void init() {
readInteger(n);
readInteger(m);
edge = new Edge[(m + )];
for(int i = ; i <= m; i++) {
readInteger(edge[i].u);
readInteger(edge[i].v);
readInteger(edge[i].w);
edge[i].rid = i;
}
} int *f; int find(int x) {
return (f[x] != x) ? (f[x] = find(f[x])) : (x);
} vector<int> *g;
vector<int> *add;
vector<int> *del; inline void Kruskal() {
f = new int[(n + )];
g = new vector<int>[(n + )];
for(int i = ; i <= n; i++)
f[i] = i;
sort(edge + , edge + m + );
int fin = ;
for(int i = ; i <= m && fin < n - ; i++) {
if(find(edge[i].u) != find(edge[i].v)) {
f[find(edge[i].u)] = find(edge[i].v);
g[edge[i].u].push_back(i);
g[edge[i].v].push_back(i);
edge[i].seced = true;
fin++;
}
}
} const int BZMAX = ;
int* dep;
Matrix<int> bz;
Matrix<int> bzm; void dfs1(int node, int fa, int lastv) {
dep[node] = dep[fa] + ;
bz[node][] = fa;
bzm[node][] = lastv;
for(int i = ; i < BZMAX; i++)
bz[node][i] = bz[bz[node][i - ]][i - ], bzm[node][i] = max(bzm[node][i - ], bzm[bz[node][i - ]][i - ]);
for(int i = ; i < (signed)g[node].size(); i++) {
if(!edge[g[node][i]].seced) continue;
int e = (edge[g[node][i]].u == node) ? (edge[g[node][i]].v) : (edge[g[node][i]].u);
if(e == fa) continue;
dfs1(e, node, edge[g[node][i]].w);
}
} pii lca(int u, int v) {
if(dep[u] < dep[v]) swap(u, v);
int rtmax = ;
int ca = dep[u] - dep[v];
for(int i = ; i < BZMAX; i++)
if(ca & ( << i)) {
smax(rtmax, bzm[u][i]);
u = bz[u][i];
}
if(u == v) return pii(u, rtmax);
for(int i = BZMAX - ; ~i; i--) {
if(bz[u][i] != bz[v][i]) {
smax(rtmax, bzm[u][i]);
smax(rtmax, bzm[v][i]);
u = bz[u][i];
v = bz[v][i];
}
}
smax(rtmax, bzm[u][]);
smax(rtmax, bzm[v][]);
return pii(bz[u][], rtmax);
} pair<Node*, Node*> dfs2(int node, int fa, int tofa) {
Node* rt = NULL;
Node* dl = NULL;
for(int i = ; i < (signed)g[node].size(); i++) {
if(!edge[g[node][i]].seced) continue;
int e = (edge[g[node][i]].u == node) ? (edge[g[node][i]].v) : (edge[g[node][i]].u);
if(e == fa) continue;
pnn ap = dfs2(e, node, g[node][i]);
rt = merge(rt, ap.fi);
dl = merge(dl, ap.sc);
}
for(int i = ; i < (signed)add[node].size(); i++)
rt = merge(rt, newnode(add[node][i]));
for(int i = ; i < (signed)del[node].size(); i++)
dl = merge(dl, newnode(del[node][i]));
while(dl && rt->val == dl->val) {
rt = merge(rt->nxt[], rt->nxt[]);
rt = merge(rt->nxt[], rt->nxt[]);
dl = merge(dl->nxt[], dl->nxt[]);
}
res[tofa] = (!rt) ? (-) : (rt->val);
return pnn(rt, dl);
} inline void solve() {
res = new int[(m + )];
dep = new int[(n + )];
bz = Matrix<int>(n + , BZMAX);
bzm = Matrix<int>(n + , BZMAX);
dep[] = ;
for(int i = ; i < BZMAX; i++)
bz[][i] = bzm[][i] = ;
dfs1(, , );
add = new vector<int>[(n + )];
del = new vector<int>[(n + )];
for(int i = ; i <= m; i++) {
if(edge[i].seced) continue;
int u = edge[i].u, v = edge[i].v;
pii l = lca(u, v);
res[i] = l.sc - ;
add[u].push_back(edge[i].w - );
add[v].push_back(edge[i].w - );
del[l.fi].push_back(edge[i].w - );
}
dfs2(, , );
ans = new int[(m + )];
for(int i = ; i <= m; i++)
ans[edge[i].rid] = res[i];
for(int i = ; i <= m; i++)
printf("%d ", ans[i]);
} int main() {
srand();
init();
Kruskal();
solve();
return ;
}
Codeforces 828F Best Edge Weight - 随机堆 - 树差分 - Kruskal - 倍增算法的更多相关文章
- codeforces 1017C - Cloud Computing 权值线段树 差分 贪心
https://codeforces.com/problemset/problem/1070/C 题意: 有很多活动,每个活动可以在天数为$[l,r]$时,提供$C$个价格为$P$的商品 现在从第一天 ...
- 【CodeForces】827 D. Best Edge Weight 最小生成树+倍增LCA+并查集
[题目]D. Best Edge Weight [题意]给定n个点m条边的带边权无向连通图,对每条边求最大边权,满足其他边权不变的前提下图的任意最小生成树都经过它.n,m<=2*10^5,1&l ...
- CF#633 D. Edge Weight Assignment
D. Edge Weight Assignment 题意 给出一个n个节点的树,现在要为边赋权值,使得任意两个叶子节点之间的路径权值异或和为0,问最多,最少有多少个不同的权值. 题解 最大值: 两个叶 ...
- CF 633 div1 1338 B. Edge Weight Assignment 构造
LINK:Edge Weight Assignment 这场当时没打 看到这个B题吓到我了 还好当时没打. 想了20min才知道怎么做 而且还不能证明. 首先考虑求最小. 可以发现 如果任意两个叶子节 ...
- cf827D Best Edge Weight (kruskal+倍增lca+并查集)
先用kruskal处理出一个最小生成树 对于非树边,倍增找出两端点间的最大边权-1就是答案 对于树边,如果它能被替代,就要有一条非树边,两端点在树上的路径覆盖了这条树边,而且边权不大于这条树边 这里可 ...
- [Codeforces 266E]More Queries to Array...(线段树+二项式定理)
[Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...
- [Codeforces 280D]k-Maximum Subsequence Sum(线段树)
[Codeforces 280D]k-Maximum Subsequence Sum(线段树) 题面 给出一个序列,序列里面的数有正有负,有两种操作 1.单点修改 2.区间查询,在区间中选出至多k个不 ...
- codeforces 1217E E. Sum Queries? (线段树
codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...
- treap(堆树)
# 2018-09-27 17:35:58 我实现的这个treap不能算是堆.有问题 最近对堆这种结构有点感兴趣,然后想用指针的方式实现一个堆而不是利用数组这种结构,于是自己想到了一个用二叉树结构实现 ...
随机推荐
- Ubuntu系统下容器化部署gitlab
容器化部署gitlab 获取镜像文件 1. 下载镜像文件 docker pull beginor/gitlab-ce:-ce. 2. 创建GitLab 的配置 (etc) . 日志 (log) .数据 ...
- System.ValueTuple 未定義或匯入預先定義的類型
System.ValueTuple 没有定义或者导入 'System.ValueTuple´2´ is not defined or imported System.ValueTuple 未定義或匯入 ...
- Java 11 新特性介绍
Java 11 已于 2018 年 9 月 25 日正式发布,之前在Java 10 新特性介绍中介绍过,为了加快的版本迭代.跟进社区反馈,Java 的版本发布周期调整为每六个月一次——即每半年发布一个 ...
- Java线程池定制ThreadPoolExecutor官方定制实例
1.仍然先看构造方法:ThreadPoolExecutor构造方法 public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,lon ...
- 2019 学而思java面试笔试题 (含面试题解析)
本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.学而思等公司offer,岗位是Java后端开发,因为发展原因最终选择去了学而思,入职一年时间了,也成为了面试官 ...
- HTML不换行,多余用省略号代替
最主要的css样式: white-space: nowrap; overflow: hidden; text-overflow: ellipsis; 例子: <!DOCTYPE html> ...
- SQLi_Labs通关文档【1-65关】
SQLi_Labs通关文档[1-65关] 为了不干扰自己本机环境,SQL-LAB我就用的码头工人,跑起来的,搭建也非常简单,也就两条命令 docker pull acgpiano/sqli-labs ...
- vim替换字符串
1. s 命令来替换字符串 :s/vivian/sky/ #替换当前行第一个 vivian 为 sky :s/vivian/sky/g #替换当前行所有 vivian 为 sky :n,$s/vivi ...
- Apache Kafka是数据库吗?
最近思路有些枯竭,找些务虚的话题来凑.本文内容完全来自于Martin Kelppmann在2019年Kafka伦敦峰会上的演讲.顺便提一句,Kelppmann是<Designing Data-I ...
- RxJS——Operators
RxJS 的操作符(operators)是最有用的,尽管 Observable 是最基本的.操作符最基本的部分(pieces)就是以申明的方式允许复杂的异步代码组合简化. 什么是操作符? 操作符是函数 ...