题目大意:
就是现在初始的时候有一个只有n个点的图(n <= 1e5), 现在进行m( m <= 1e5 )次操作
每次操作要么添加一条无向边, 要么询问之前结点u和v最早在哪一次操作的时候连通了
大致思路:
这个题表示只想到了O(m*logn*logn)的做法....
首先用并查集维护连通性
当添加边的时候,如果两个点在不同连通块,就选取其连通块的代表结点连一条边, 权值为该操作的编号, 否则不作操作.
那么在n次操作之后, 会得到一个森林
然后我们离线处理询问
如果操作为询问, 则就相当于问树上两个点路径上的最大边权值就是答案(同一连通块, 也就是同一棵树的情况)不在一棵树上就是-1
如果操作为添加边, 那么就把之前的添加的边的权值设置为-1即可
对于边上边权的修改和树的路径的最大边权询问, 树链剖分即可解决
貌似有更简单的方法....不过表示只想到了这个比较明显的做法....并查集只会用来表示连通性好捉急....
代码如下:
Result : Accepted Memory : 13412 KB Time : 638 ms
/* * Author: Gatevin * Created Time: 2015/11/21 14:02:38 * File Name: Sakura_Chiyo.cpp */ #include<iostream> #include<sstream> #include<fstream> #include<vector> #include<list> #include<deque> #include<queue> #include<stack> #include<map> #include<set> #include<bitset> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> #include<cctype> #include<cmath> #include<ctime> #include<iomanip> using namespace std; const double eps(1e-8); typedef long long lint; #define maxn 100010 int top[maxn]; int grandson[maxn]; int dep[maxn]; int siz[maxn]; int belong[maxn]; int father[maxn]; int Q[maxn]; int cnt; int hson[maxn]; int q, s; bool vis[maxn]; int T, n, m; int id[maxn]; int antiID[maxn]; struct Edge { int u, v, w, nex; Edge(int _u, int _v, int _w, int _nex) { u = _u, v = _v, w = _w, nex = _nex; } Edge(){} }; int head[maxn]; int tot; Edge edge[maxn << 1]; int w[maxn]; int idx; void add_Edge(int x, int y, int w) { edge[++tot] = Edge(x, y, w, head[x]); head[x] = tot; } void split(int root) { int l = 0, r = 1; dep[Q[r] = root] = 1; father[root] = -1; w[root] = 0; while(l < r) { int x = Q[++l]; if(head[x] == -1) continue; for(int j = head[x]; j + 1; j = edge[j].nex) { int y = edge[j].v; if(y == father[x]) continue; w[y] = edge[j].w; dep[Q[++r] = y] = dep[x] + 1; father[y] = x; } } for(int i = r ; i ; i--) { int x = Q[i], p = -1; siz[x] = 1; if(head[x] == -1) continue; for(int j = head[x]; j + 1; j = edge[j].nex) { int y = edge[j].v; if(y == father[x]) continue; siz[x] += siz[y]; if(p == -1 || (p > 0 && siz[y] > siz[p])) p = y; } if(p == -1) { hson[x] = -1; grandson[++cnt] = x; belong[top[cnt] = x] = cnt; } else { hson[x] = p; belong[x] = belong[p]; top[belong[x]] = x; } } //int idx = 0; //memset(vis, 0, sizeof(vis)); for(int i = r; i; i--) { int x = Q[i]; if(vis[x]) continue; vis[x] = 1; id[x] = ++idx; antiID[idx] = x; while(father[x] != -1 && belong[father[x]] == belong[x] && !vis[father[x]]) { x = father[x]; id[x] = ++idx; antiID[idx] = x; vis[x] = 1; } } return; } #define lson l, mid, rt << 1 #define rson mid + 1, r , rt << 1 | 1 int val[maxn << 2]; void pushUp(int rt) { val[rt] = max(val[rt << 1], val[rt << 1 | 1]); return; } void build(int l, int r, int rt) { if(l == r) { val[rt] = w[antiID[l]]; return; } int mid = (l + r) >> 1; build(lson); build(rson); pushUp(rt); } void update(int l, int r, int rt, int pos, int value) { if(l == r) { val[rt] = value; return; } int mid = (l + r) >> 1; if(pos <= mid) update(lson, pos, value); else update(rson, pos, value); pushUp(rt); return; } int query(int l, int r, int rt, int L, int R) { if(l >= L && r <= R) return val[rt]; int mid = (l + r) >> 1; int ret = 0; if(mid >= L) ret = max(ret, query(lson, L, R)); if(mid + 1 <= R) ret = max(ret, query(rson, L, R)); return ret; } int answer(int x, int y) { int ans= 0; while(top[belong[x]] != top[belong[y]]) { if(dep[top[belong[x]]] < dep[top[belong[y]]]) swap(x, y); ans = max(ans, query(1, n, 1, id[x], id[top[belong[x]]])); x = father[top[belong[x]]]; } if(x == y) return ans; if(dep[x] < dep[y]) swap(x, y); ans = max(ans, query(1, n, 1, id[x], id[hson[y]])); return ans; } void change(int x, int w) { x <<= 1; int u = edge[x].u, v = edge[x].v; if(father[u] == v) update(1, n, 1, id[u], w); else update(1, n, 1, id[v], w); return; } struct Op { int type, u, v, fu, fv, edge; bool add; Op(int _t, int _u, int _v) { type = _t, u = _u, v = _v; add = false; } Op(){} }; Op op[maxn]; int fa[maxn]; int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } int E;//添加的边的次数 void Union(int x, int y, int opid) { int fx = find(x); int fy = find(y); op[opid].fu = fx, op[opid].fv = fy; op[opid].add = false; if(fx != fy) { fa[fx] = fy; op[opid].add = true; op[opid].edge = ++E; add_Edge(fx, fy, opid); add_Edge(fy, fx, opid); } } int ans[maxn]; int read() { int x = 0; char c; while(!isdigit(c = getchar())) continue; x = (x << 3) + (x << 1) + c - '0'; while(isdigit(c = getchar())) x = (x << 3) + (x << 1) + c - '0'; return x; } int sis[maxn]; int main() { //scanf("%d", &T); T = read(); int cas = 0; while(T--) { cas++; //scanf("%d %d", &n, &m); n = read(), m = read(); memset(head, -1, sizeof(head)); tot = 0; E = 0; for(int i = 1; i <= n; i++) fa[i] = i; for(int i = 1; i <= m; i++) { //scanf("%d %d %d", &op[i].type, &op[i].u, &op[i].v); op[i].type = read(), op[i].u = read(), op[i].v = read(); if(op[i].type == 1) Union(op[i].u, op[i].v, i); } idx = 0; memset(vis, 0, sizeof(vis)); cnt = 0; for(int i = 1; i <= n; i++) { int fi = find(i); if(sis[fi] != cas) { split(fi); sis[fi] = cas; } } build(1, n, 1); for(int i = m; i > 0; i--) { if(op[i].type == 2)//query { int u = op[i].u, v = op[i].v; if(find(u) != find(v))//一直就是不连通的 { ans[i] = -1; continue; } ans[i] = answer(op[i].u, op[i].v); } else { if(op[i].add == false) continue;//这个操作没有造成过添加边的影响 change(op[i].edge, 1e9); } } for(int i = 1; i <= m; i++) if(op[i].type == 2) printf("%d\n", ans[i] == 1e9 ? -1 : ans[i]); } return 0; }