[Solution]
This is a classic problem of link cut tree, although I saw some solutions using LCA.
We do it from back to the front, so that we can transfer deleting edge into adding edge. Use link cut tree to modify the minimal spanning tree is the only solution that I can come up with. Regard each edge as a single node on the tree and delete the biggest one on the path while adding edge.
[Code]
It's so long and ugly. Also, it runs very slow. Maybe it's because I'm not so familar with splay tree.
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <memory.h>
#include <algorithm>
#include <map>
using namespace std;
typedef long long qw;
typedef pair <qw, int> edgepair;
typedef map <qw, int> edgerec;
const int maxn = 200009;
const int maxq = 100009;
const int maxe = 1000009;
const int inf = 0x3f3f3f3f;
struct edge {
int a, b, v;
};
struct query {
int k, x, y, s, e;
};
int nextInt() {
int d, s = 0;
do
d = getchar();
while (!isdigit(d));
do
s = s * 10 + d - 48, d = getchar();
while (isdigit(d));
return s;
}
void printIntLn(int x) {
static int buf[12];
int p = 0;
if (!x)
puts("0");
else {
while (x)
buf[++ p] = x % 10, x /= 10;
while (p)
putchar(buf[p --] + 48);
putchar(10);
}
}
#define _l (qw)
#define mkword(x,y) (((_l x)<<32)|(y))
inline bool cmpEdge(const edge& a, const edge& b) {
return a. v < b. v;
}
int n, m, tq;
int ns[maxn], tns, np, ls[maxn], rs[maxn], pt[maxn], sz[maxn], vl[maxn], vt[maxn], rv[maxn];
int r[maxn];
edge e[maxe], en[maxn];
edgerec er;
query q[maxq];
inline int getRoot(int x) {
return (r[x] == x) ? x : (r[x] = getRoot(r[x]));
}
inline int newNode(int v0) {
int p = ns[-- tns];
ls[p] = 0;
rs[p] = 0;
pt[p] = 0;
sz[p] = 1;
vl[p] = v0;
vt[p] = v0;
rv[p] = 0;
return p;
}
#define valOf(x) ((x)?(vt[x]):0)
#define max(a,b) (((a)>(b))?(a):(b))
#define isRoot(x) (!pt[x] || ((x) != ls[pt[x]] && (x) != rs[pt[x]]))
#define update(p) {\
sz[p] = sz[ls[p]] + sz[rs[p]] + 1;\
vt[p] = max(max(valOf(ls[p]), valOf(rs[p])), vl[p]);\
}
#define fix(p) {\
if (rv[p]) {\
rv[ls[p]] ^= 1;\
rv[rs[p]] ^= 1;\
swap(ls[p], rs[p]);\
rv[p] = 0;\
}\
}
#define lRot(p) {\
int q = pt[p], a = pt[q];\
ls[q] = rs[p];\
if (rs[p])\
pt[rs[p]] = q;\
pt[p] = a;\
pt[q] = p;\
if (a) {\
if (q == ls[a])\
ls[a] = p;\
else if (q == rs[a])\
rs[a] = p;\
}\
rs[p] = q;\
update(q);\
update(p);\
}
#define rRot(p) {\
int q = pt[p], a = pt[q];\
rs[q] = ls[p];\
if (ls[p])\
pt[ls[p]] = q;\
pt[p] = a;\
pt[q] = p;\
if (a) {\
if (q == ls[a])\
ls[a] = p;\
else if (q == rs[a])\
rs[a] = p;\
}\
ls[p] = q;\
update(q);\
update(p);\
}
void splay(int p) {
static int r[maxn];
int tr = 1;
r[0] = p;
for (int q = p; !isRoot(q); q = pt[q])
r[tr ++] = pt[q];
for (int i = tr - 1; i >= 0; i --)
fix(r[i]);
while (!isRoot(p)) {
int f = pt[p], a;
if (isRoot(f)) {
if (p == ls[f])
lRot(p)
else
rRot(p)
break;
}
a = pt[f];
if (f == ls[a]) {
if (p == ls[f]) {
lRot(f)
lRot(p)
}
else {
rRot(p)
lRot(p)
}
}
else {
if (p == rs[f]) {
rRot(f)
rRot(p)
}
else {
lRot(p)
rRot(p)
}
}
}
}
int expose(int p) {
int q = 0;
for (; p; p = pt[p]) {
splay(p);
rs[p] = q;
update(p)
q = p;
}
for (; ls[q]; q = ls[q]);
return q;
}
void makeRoot(int p) {
expose(p);
splay(p);
rv[p] ^= 1;
}
void addEdge(int p, int q, int w) {
makeRoot(p);
int e = newNode(w);
rs[e] = p;
pt[p] = e;
pt[e] = q;
update(e);
en[e]. a = p;
en[e]. b = q;
}
int queryMax(int p, int q) {
makeRoot(p);
expose(q);
splay(q);
while (q)
if (vt[q] == vl[q])
return q;
else if (vt[ls[q]] == vt[q])
q = ls[q];
else
q = rs[q];
if (q)
splay(q);
return q;
}
void tryAdd(int u, int v, int w) {
int c = queryMax(u, v);
if (vl[c] > w) {
int p = en[c]. a;
int q = en[c]. b;
makeRoot(p);
expose(q);
splay(q);
ls[q] = 0;
pt[p] = 0;
ns[tns ++] = c;
addEdge(u, v, w);
}
else if (!c)
addEdge(u, v, w);
}
void buildTree() {
for (int i = 1; i <= n; i ++) {
ls[i] = 0;
rs[i] = 0;
pt[i] = 0;
sz[i] = 1;
vl[i] = 0;
vt[i] = 0;
rv[i] = 0;
r[i] = i;
}
tns = 0;
for (int i = n + 1; i < maxn; i ++)
ns[tns ++] = i;
sort(e, e + m, cmpEdge);
for (int i = 0; i < m && e[i]. v < inf; i ++)
if (getRoot(e[i]. a) != getRoot(e[i]. b)) {
addEdge(e[i]. a, e[i]. b, e[i]. v);
r[getRoot(e[i]. a)] = getRoot(e[i]. b);
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
n = nextInt();
m = nextInt();
tq = nextInt();
for (int i = 0; i < m; i ++) {
e[i]. a = nextInt();
e[i]. b = nextInt();
e[i]. v = nextInt();
if (e[i]. a > e[i]. b)
swap(e[i]. a, e[i]. b);
er. insert(edgepair(mkword(e[i]. a, e[i]. b), i));
}
for (int i = 0; i < tq; i ++) {
q[i]. k = nextInt();
q[i]. x = nextInt();
q[i]. y = nextInt();
if (q[i]. x > q[i]. y)
swap(q[i]. x, q[i]. y);
if (q[i]. k == 2) {
edgerec :: iterator it = er. find(mkword(q[i]. x, q[i]. y));
if (it != er. end()) {
q[i]. e = e[it-> second]. v;
e[it-> second]. v = inf;
}
}
}
buildTree();
for (int i = tq - 1; i >= 0; i --)
if (q[i]. k == 1)
q[i]. s = vl[queryMax(q[i]. x, q[i]. y)];
else
tryAdd(q[i]. x, q[i]. y, q[i]. e);
for (int i = 0; i < tq; i ++)
if (q[i]. k == 1)
printIntLn(q[i]. s);
}
[]
NOI2014 is coming and wish everyone RP <<= inf