codeforces 609E. Minimum spanning tree for each edge 树链剖分

题目链接

给一个n个节点m条边的树, 每条边有权值, 输出m个数, 每个数代表包含这条边的最小生成树的值。

先将最小生成树求出来, 把树边都标记。 然后对标记的边的两个端点, 我们add(u, v), add(v, u)。 对于每一次输出, 如果这条边被标记了, 那么直接输出mst的值。 否则, 加上这条边之后一定会出现一个环, 我们就把环上的最长的那条边删掉。 查询最长的那条边可以用树链剖分。

 #include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 2e5+;
int head[maxn*], son[maxn], sz[maxn], deep[maxn], top[maxn], w[maxn], f[maxn], cnt, num;
int maxx[maxn<<], fa[maxn];
struct node
{
int to, nextt;
}e[maxn*];
struct ed
{
int u, v, id, val, mark;
ed(){}
ed(int u, int v, int val, int id, int mark = ):u(u), v(v), val(val), id(id), mark(mark){}
}edge[maxn];
bool cmp1(ed a, ed b) {
return a.id<b.id;
}
bool cmp2(ed a, ed b) {
if(a.val == b.val)
return a.id<b.id;
return a.val<b.val;
}
void init() {
mem1(head);
num = cnt = ;
}
void add(int u, int v, int w) {
e[num].to = v, e[num].nextt = head[u], head[u] = num++;
}
void dfs1(int u, int fa) {
sz[u] = ;
deep[u] = deep[fa]+;
son[u] = -;
f[u] = fa;
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(v == fa)
continue;
dfs1(v, u);
sz[u] += sz[v];
if(son[u]==-||sz[v]>sz[son[u]])
son[u] = v;
}
}
void dfs2(int u, int tp) {
w[u] = ++cnt, top[u] = tp;
if(~son[u])
dfs2(son[u], tp);
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(v == f[u]||v == son[u])
continue;
dfs2(v, v);
}
}
void pushUp(int rt) {
maxx[rt] = max(maxx[rt<<], maxx[rt<<|]);
}
void update(int p, int val, int l, int r, int rt) {
if(l == r) {
maxx[rt] = val;
return ;
}
int m = l+r>>;
if(p<=m)
update(p, val, lson);
else
update(p, val, rson);
pushUp(rt);
}
int query(int L, int R, int l, int r, int rt) {
if(L<=l&&R>=r) {
return maxx[rt];
}
int m = l+r>>, ret = ;
if(L<=m)
ret = max(ret, query(L, R, lson));
if(R>m)
ret = max(ret, query(L, R, rson));
return ret;
}
int find(int u, int v) {
int f1 = top[u], f2 = top[v], ret = ;
while(f1 != f2) {
if(deep[f1]<deep[f2]) {
swap(f1, f2);
swap(u, v);
}
ret = max(ret, query(w[f1], w[u], , cnt, ));
u = f[f1];
f1 = top[u];
}
if(u == v)
return ret;
if(deep[u]>deep[v])
swap(u, v);
return max(ret, query(w[son[u]], w[v], , cnt, ));
}
int findd(int u) {
return fa[u] == u?u:fa[u] = findd(fa[u]);
}
int main()
{
int t, n, u, v, val, m;
while(~scanf("%d%d", &n, &m)) {
init();
ll mst = ;
deep[] = ;
for(int i = ; i<=m; i++) {
scanf("%d%d%d", &u, &v, &val);
edge[i] = ed(u, v, val, i);
}
for(int i = ; i<=n; i++)
fa[i] = i;
sort(edge+, edge++m, cmp2);
for(int i = ; i<=m; i++) {
u = findd(edge[i].u);
v = findd(edge[i].v);
if(u == v)
continue;
edge[i].mark = ;
fa[v] = u;
mst += edge[i].val;
}
for(int i = ; i<=m; i++) {
if(edge[i].mark) {
add(edge[i].u, edge[i].v, edge[i].val);
add(edge[i].v, edge[i].u, edge[i].val);
}
}
dfs1(, );
dfs2(, );
for(int i = ; i<=m; i++) {
if(deep[edge[i].u]>deep[edge[i].v]) {
swap(edge[i].u, edge[i].v);
}
if(edge[i].mark)
update(w[edge[i].v], edge[i].val, , cnt, );
}
sort(edge+, edge++m, cmp1);
for(int i = ; i<=m; i++) {
if(edge[i].mark) {
printf("%I64d\n", mst);
} else {
int tmp = find(edge[i].u, edge[i].v);
printf("%I64d\n", mst-tmp+edge[i].val);
}
}
}
return ;
}
上一篇:MyEclipse中代码格式化后自动换行


下一篇:jquery 时间控件怎么能禁止输入只能选择日期?