一道很不错的 LCT 题,感觉很有收获~
首先这道题是不断减边,维护最小生成树。
如果我们从后往前做就是不断加边的最小生成树,同时询问路径最大值。
LCT LCT? LCT LCT !
蛤蛤,LCT 维护边权。。。并不会做
然而如果我们将边视为点,就很好做了~
对于边 (
u
,
贾教(贾志鹏)的数据生成器,叫做 tube_solution。
然而似乎要翻墙。。。百度网盘,密码:t21j
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <iostream>
#include <algorithm>
template<class Num>void read(Num &x)
{
char c; int flag = 1;
while((c = getchar()) < '0' || c > '9')
if(c == '-') flag *= -1;
x = c - '0';
while((c = getchar()) >= '0' && c <= '9')
x = (x<<3) + (x<<1) + (c-'0');
x *= flag;
return;
}
template<class Num>void write(Num x)
{
if(x < 0) putchar('-'), x = -x;
static char s[20];int sl = 0;
while(x) s[sl++] = x%10 + '0',x /= 10;
if(!sl) {putchar('0');return;}
while(sl) putchar(s[--sl]);
}
const int maxn = 1e5 + 50, maxq = maxn, maxm = 1e6 + 50;
const int INF = 0x3f3f3f3f, Nya = -1, size = maxn + maxm;
#define REP(__i,__start,__end) for(int __i = (__start); __i <= (__end); __i++)
int n, m, q;
struct type_op
{
int id, tp, x, y, w;
type_op(int id = 0,int tp = 0,int x = 0,int y = 0):id(id),tp(tp),x(x),y(y){}
}op[maxq];
typedef std::pair<std::pair<int,int>,std::pair<int,int> > Edge;
Edge edge[maxm];
#define Vx first.first
#define Vy first.second
#define W second.first
#define Wr second.second
bool cmp(const Edge &a,const Edge &b)
{
return a.W < b.W;
}
int _fa[maxn];
int max[size], val[size];
int find(int x)
{
return (_fa[x] == x) ? x : (_fa[x] = find(_fa[x]));
}
#define check(x) (x)
namespace LCT
{
int pf[size], c[size][2], fa[size], rev[size];
void update(int x)
{
int maxL = max[c[x][0]], maxR = max[c[x][1]];
max[x] = x;
if(val[maxL] > val[max[x]]) max[x] = maxL;
if(val[maxR] > val[max[x]]) max[x] = maxR;
}
void pushdown(int x)
{
if(rev[x])
{
std::swap(c[x][0], c[x][1]);
rev[c[x][0]] ^= 1;
rev[c[x][1]] ^= 1;
rev[x] = 0;
}
}
void rotate(int x)
{
int y = fa[x], z = fa[y], i, j;
pushdown(y), pushdown(x);
j = (c[z][1] == y), i = (c[y][1] == x);
if(check(z)) c[z][j] = x;
else pf[x] = pf[y], pf[y] = 0;
if(c[x][i^1]) fa[c[x][i^1]] = y;
fa[x] = z, fa[y] = x;
c[y][i] = c[x][i^1], c[x][i^1] = y;
update(y), update(x);
}
void Splay(int v)
{
int g = fa[v], h = fa[g];
pushdown(v);
while(check(g))
{
if(check(h))
{
if((c[h][1] == g)^(c[g][1] == v))
rotate(v);
else
rotate(g);
}
rotate(v), g = fa[v], h = fa[g];
}
update(v);
}
void access(int v)
{
int u = v, e = v;
v = 0;
while(u)
{
Splay(u);
int &t = c[u][1];
if(check(t)) pf[t] = u, fa[t] = 0;
if(check(v)) pf[v] = 0, fa[v] = u;
t = v, update(u);
v = u, u = pf[u];
}
Splay(e);
}
void makeroot(int v)
{
access(v);
rev[v] ^= 1;
}
void join(int v,int u)
{
makeroot(v);
pf[v] = u;
access(v);
}
void cut(int v,int u)
{
makeroot(v);
access(u);
c[u][0] = fa[v] = 0;
}
int query(int u,int v)
{
makeroot(u);
access(v);
return max[v];
}
}
#undef check
void init()
{
int u, v, w;
read(n), read(m), read(q);
REP(i, 1, m)
{
read(u), read(v), read(w);
if(u > v) std::swap(u, v);
edge[i] = std::make_pair(std::make_pair(u, v), std::make_pair(w, 0));
}
REP(i, 1, q)
{
read(w), read(u), read(v);
if(u > v) std::swap(u, v);
op[i] = type_op(i, w, u, v);
}
}
int bound(int u,int v)
{
return std::upper_bound(edge + 1, edge + m + 1, std::make_pair(std::make_pair(u, v), std::make_pair(Nya, 0))) - edge;
}
void prework()
{
int u, v, sn = n;
std::sort(edge + 1, edge + m + 1);
REP(i, 1, m)
{
edge[i].Wr = i + n;
val[i + n] = edge[i].W;
max[i + n] = val[i + n];
}
REP(i, 1, q)
if(op[i].tp == 2)
{
int id = bound(op[i].x, op[i].y);
edge[id].W = INF, op[i].w = id + n;
}
std::sort(edge + 1, edge + m + 1, cmp);
REP(i, 1, n) _fa[i] = i;
REP(i, 1, m)
{
u = edge[i].Vx;
v = edge[i].Vy;
int x = find(u), y = find(v);
if(x != y)
{
_fa[x] = y;
LCT::join(u, edge[i].Wr);
LCT::join(v, edge[i].Wr);
if((--sn) == 1) break;
}
}
}
void solve()
{
std::sort(edge + 1, edge + m + 1);
for(int i = q; i >= 1; i--)
{
int t = LCT::query(op[i].x, op[i].y);
if(op[i].tp == 1)
{
op[i].w = val[t];
}
else
{
if(val[t] > val[op[i].w])
{
Edge &e = edge[t - n];
LCT::cut(e.Vx, t), LCT::cut(e.Vy, t);
LCT::join(op[i].x, op[i].w);
LCT::join(op[i].y, op[i].w);
}
}
}
REP(i, 1, q)
if(op[i].tp == 1)
write(op[i].w), puts("");
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("tube.in","r",stdin);
freopen("tube.out","w",stdout);
#endif
init(), prework(), solve();
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return 0;
}