[WC 2006]水管局长数据加强版

Description

SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一条从A至B的水管的路径,接着通过信息化的控制中心通知路径上的水管进入准备送水状态,等到路径上每一条水管都准备好了,供水公司就可以开始送水了。嘟嘟一次只能处理一项送水任务,等到当前的送水任务完成了,才能处理下一项。
在处理每项送水任务之前,路径上的水管都要进行一系列的准备操作,如清洗、消毒等等。嘟嘟在控制中心一声令下,这些水管的准备操作同时开始,但由于各条管道的长度、内径不同,进行准备操作需要的时间可能不同。供水公司总是希望嘟嘟能找到这样一条送水路径,路径上的所有管道全都准备就绪所需要的时间尽量短。嘟嘟希望你能帮助他完成这样的一个选择路径的系统,以满足供水公司的要求。另外,由于MY市的水管年代久远,一些水管会不时出现故障导致不能使用,你的程序必须考虑到这一点。
不妨将MY市的水管网络看作一幅简单无向图(即没有自环或重边):水管是图中的边,水管的连接处为图中的结点。

Input

输入文件第一行为3个整数:N, M, Q分别表示管道连接处(结点)的数目、目前水管(无向边)的数目,以及你的程序需要处理的任务数目(包括寻找一条满足要求的路径和接受某条水管坏掉的事实)。
以下M行,每行3个整数x, y和t,描述一条对应的水管。x和y表示水管两端结点的编号,t表示准备送水所需要的时间。我们不妨为结点从1至N编号,这样所有的x和y都在范围[1, N]内。
以下Q行,每行描述一项任务。其中第一个整数为k:若k=1则后跟两个整数A和B,表示你需要为供水公司寻找一条满足要求的从A到B的水管路径;若k=2,则后跟两个整数x和y,表示直接连接x和y的水管宣布报废(保证合法,即在此之前直接连接x和y尚未报废的水管一定存在)。

Output

按顺序对应输入文件中每一项k=1的任务,你需要输出一个数字和一个回车/换行符。该数字表示:你寻找到的水管路径中所有管道全都完成准备工作所需要的时间(当然要求最短)。

Sample Input

4 4 3
1 2 2
2 3 3
3 4 2
1 4 2
1 1 4
2 1 4
1 1 4

Sample Output

2
3

HINT

【原题数据范围】
N ≤ 1000
M ≤ 100000
Q ≤ 100000
测试数据中宣布报废的水管不超过5000条;且任何时候我们考虑的水管网络都是连通的,即从任一结点A必有至少一条水管路径通往任一结点B。
【加强版数据范围】
N ≤ 100000
M ≤ 1000000
Q ≤ 100000
任何时候我们考虑的水管网络都是连通的,即从任一结点A必有至少一条水管路径通往任一结点B。
【C/C++选手注意事项】
由于此题输入规模较大(最大的测试点约20MB),因此即使使用scanf读入数据也会花费较多的时间。为了节省读入耗时,建议使用以下函数读入正整数(返回值为输入文件中下一个正整数):
int getint()
{
char ch = getchar();
for ( ; ch > '9' || ch < '0'; ch = getchar());
int tmp = 0;
for ( ; '0' <= ch && ch <= '9'; ch = getchar())
tmp = tmp * 10 + int(ch) - 48;
return tmp;
}

题解

还是动态维护瓶颈路...但是正着做不好做,考虑倒叙操作,每次当“报废一条水管”就相当于新添一条水管,这样就好用 $lct$ 维护了...

  1 //It is made by Awson on 2018.1.11
  2 #include <set>
  3 #include <map>
  4 #include <cmath>
  5 #include <ctime>
  6 #include <queue>
  7 #include <stack>
  8 #include <cstdio>
  9 #include <string>
 10 #include <vector>
 11 #include <cstdlib>
 12 #include <cstring>
 13 #include <iostream>
 14 #include <algorithm>
 15 #define LL long long
 16 #define Max(a, b) ((a) > (b) ? (a) : (b))
 17 #define Min(a, b) ((a) < (b) ? (a) : (b))
 18 #define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
 19 using namespace std;
 20 const int N = 200000;
 21 const int M = 1000000;
 22 void read(int &x) {
 23     char ch; bool flag = 0;
 24     for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
 25     for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
 26     x *= 1-2*flag;
 27 }
 28 void write(int x) {
 29     if (x > 9) write(x/10);
 30     putchar(x%10+48);
 31 }
 32 
 33 int n, m, q, w[N+5], a[N+5], b[N+5], u, v, c, ans[N+5];
 34 struct tt {
 35     int u, v, c, tag;
 36     tt() {}
 37     tt(int _u, int _v, int _c, int _tag) {u = _u, v = _v, c = _c, tag = _tag; }
 38     bool operator < (const tt &tmp) const {return c < tmp.c; }
 39 }e[M+5], que[N+5];
 40 bool comp(const tt &a, const tt &b) {return a.u == b.u ? a.v < b.v : a.u < b.u; }
 41 struct Link_Cut_Tree {
 42     int ch[N+5][2], pre[N+5], maxi[N+5], rev[N+5], isrt[N+5], pos;
 43     Link_Cut_Tree() {for (int i = 1; i <= N; i++) isrt[i] = 1; }
 44     void pushup(int o) {
 45     maxi[o] = o;
 46     if (w[maxi[ch[o][0]]] > w[maxi[o]]) maxi[o] = maxi[ch[o][0]];
 47     if (w[maxi[ch[o][1]]] > w[maxi[o]]) maxi[o] = maxi[ch[o][1]];
 48     }
 49     void pushdown(int o) {
 50     if (!rev[o]) return;
 51     int ls = ch[o][0], rs = ch[o][1];
 52     Swap(ch[ls][0], ch[ls][1]), Swap(ch[rs][0], ch[rs][1]);
 53     rev[ls] ^= 1, rev[rs] ^= 1, rev[o] = 0;
 54     }
 55     void push(int o) {
 56     if (!isrt[o]) push(pre[o]);
 57     pushdown(o);              
 58     }
 59     void rotate(int o, int kind) {
 60     int p = pre[o];
 61     ch[p][!kind] = ch[o][kind], pre[ch[o][kind]] = p;
 62     if (isrt[p]) isrt[o] = 1, isrt[p] = 0;
 63     else ch[pre[p]][ch[pre[p]][1] == p] = o;
 64     pre[o] = pre[p];
 65     ch[o][kind] = p, pre[p] = o;
 66     pushup(p), pushup(o);
 67     }
 68     void splay(int o) {
 69     push(o);
 70     while (!isrt[o]) {
 71         if (isrt[pre[o]]) rotate(o, ch[pre[o]][0] == o);
 72         else {
 73         int p = pre[o], kind = ch[pre[p]][0] == p;
 74         if (ch[p][kind] == o) rotate(o, !kind), rotate(o, kind);
 75         else rotate(p, kind), rotate(o, kind);
 76         }
 77     }
 78     }
 79     void access(int o) {
 80     int y = 0;
 81     while (o) {
 82         splay(o);
 83         isrt[ch[o][1]] = 1, isrt[ch[o][1] = y] = 0;
 84         pushup(o); o = pre[y = o];
 85     }
 86     }
 87     void makeroot(int o) {access(o), splay(o); rev[o] ^= 1, Swap(ch[o][0], ch[o][1]); }
 88     void link(int x, int y) {makeroot(x); pre[x] = y; }
 89     void cut(int x, int y) {makeroot(x); access(y); splay(y); ch[y][0] = pre[x] = 0, isrt[x] = 1; pushup(y); }
 90     void update(int x, int y, int c) {
 91     makeroot(x), access(y), splay(y); int last = maxi[y];
 92     if (w[last] <= c) return;
 93     cut(last, a[last]), cut(last, b[last]);
 94     w[last] = c, link(last, a[last] = x), link(last, b[last] = y);
 95     }
 96     int query(int x, int y) {makeroot(x), access(y), splay(y); return w[maxi[y]]; }
 97 }T;
 98 
 99 int fa[N+5];
100 int find(int x) {return fa[x] ? fa[x] = find(fa[x]) : x; }
101 void Kruskal() {
102     sort(e+1, e+1+m); int cnt = 0;
103     for (int i = 1; i <= m; i++) {
104     if (e[i].tag) continue;
105     if (find(e[i].u)^find(e[i].v)) {
106         fa[find(e[i].u)] = find(e[i].v);
107         w[++T.pos] = e[i].c, T.link(T.pos, a[T.pos] = e[i].u), T.link(T.pos, b[T.pos] = e[i].v);
108         ++cnt;
109         if (cnt == n-1) break;
110     }
111     }
112 }
113 int dsrch(int u, int v) {
114     int L = 1, R = m;
115     while (L <= R) {
116     int mid = (L+R)>>1;
117     if (e[mid].u == u && e[mid].v == v) return mid;
118     if (e[mid].u < u || (e[mid].u == u && e[mid].v < v)) L = mid+1;
119     else R = mid-1;
120     }
121     return 2333;
122 }
123 void work() {
124     read(n), read(m), read(q); T.pos = n;
125     for (int i = 1; i <= m; i++) {
126     read(u), read(v), read(c); if (u > v) Swap(u, v);
127     e[i] = tt(u, v, c, 0);
128     }
129     sort(e+1, e+1+m, comp);
130     for (int i = 1; i <= q; i++) {
131     read(c), read(u), read(v); if (u > v) Swap(u, v);
132     que[i] = tt(u, v, c, 0);
133     if (c == 2) {int t = dsrch(u, v); e[t].tag = 1; }
134     }
135     Kruskal(); sort(e+1, e+1+m, comp);
136     for (int i = q; i; i--) {
137     if (que[i].c == 2) {int t = dsrch(que[i].u, que[i].v); T.update(e[t].u, e[t].v, e[t].c); }
138     else ans[i] = T.query(que[i].u, que[i].v);
139     }
140     for (int i = 1; i <= q; i++) if (ans[i]) write(ans[i]), putchar('
');
141 }
142 int main() {
143     work();
144     return 0;
145 }
原文地址:https://www.cnblogs.com/NaVi-Awson/p/8267806.html