BZOJ 2594: [Wc2006]水管局长数据加强版(kruskal + LCT)

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的任务,你需要输出一个数字和一个回车/换行符。该数字表示:你寻找到的水管路径中所有管道全都完成准备工作所需要的时间(当然要求最短)。
 
题目大意:给一张无向图,每条边有一个权值。有Q个操作,或删掉一条已存在边,或给出两个点,要求找出一条路径使得路径上的最大边最小,求这条边的权。
思路:首先,考虑没有修改的情况,要求最大边最小,可以贪心地从小到大往图里加边,这个就类似于kruskal的操作,实际上就是kruskal求MST。
求出MST后,用link-cut tree维护这颗MST即可。
然后因为要删边,在这题里,删掉一条边之后,会形成两个集合,为了保证MST连通,须要再找一条最小边把两颗MST连起来,但是这并不是一件容易的事情。
考虑离线,从后往前做。先把可能会被删掉的边全都删掉,然后求MST。
此时,遇到删边操作,就把A到B之间的最大边删去,再把本来要删的边加到MST中(如果最大边大于本来要删掉的边,否则什么都不用做)。
然后主要就是LCT的事情了。
 
代码(14796 ms):
  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 typedef long long LL;
  4  
  5 const int MAXV = 100010;
  6 const int MAXE = 1000010;
  7  
  8 #define foreach(iter, v) for(__typeof(v.begin()) iter = v.begin(); iter != v.end(); ++iter)
  9 #define FOR(i, n) for(int i = 0; i < n; ++i)
 10  
 11 inline int readint() {
 12     char c = getchar();
 13     while(!isdigit(c)) c = getchar();
 14     int res = 0;
 15     while(isdigit(c)) res = res * 10 + c - '0', c = getchar();
 16     return res;
 17 }
 18  
 19 struct DSU {
 20     int fa[MAXV];
 21  
 22     void init(int n) {
 23         for(int i = 1; i <= n; ++i)
 24             fa[i] = i;
 25     }
 26  
 27     int find_set(int x) {
 28         return fa[x] == x ? x : fa[x] = find_set(fa[x]);
 29     }
 30  
 31     void merge(int x, int y) {
 32         fa[find_set(x)] = find_set(y);
 33     }
 34 } dsu;
 35  
 36 struct Edge {
 37     int u, v, cost;
 38     bool del, use;
 39  
 40     Edge() {}
 41     Edge(int u, int v):
 42         u(u), v(v) {}
 43  
 44     int getv(int x) {
 45         return x != u ? u : v;
 46     }
 47  
 48     void read() {
 49         u = readint(), v = readint(), cost = readint();
 50         if(u > v) swap(u, v);
 51         del = use = false;
 52     }
 53  
 54     bool operator < (const Edge &rhs) const {
 55         return cost < rhs.cost;
 56     }
 57 } edge[MAXE];
 58  
 59 bool cmp_uv(const Edge &a, const Edge &b) {
 60     if(a.u != b.u) return a.u < b.u;
 61     return a.v < b.v;
 62 }
 63  
 64 struct Operator {
 65     int k, a, b, mid;
 66     void read() {
 67         k = readint(), a = readint(), b = readint();
 68         if(a > b) swap(a, b);
 69     }
 70 } oper[MAXV];
 71  
 72 void kruskal(int n, int m) {
 73     sort(edge, edge + m);
 74     dsu.init(n);
 75     for(int i = 0; i < m; ++i) if(!edge[i].del) {
 76         if(dsu.find_set(edge[i].u) != dsu.find_set(edge[i].v)) {
 77             edge[i].use = true;
 78             dsu.merge(edge[i].u, edge[i].v);
 79         }
 80     }
 81 }
 82  
 83 struct Node {
 84     Node *ch[2], *fa;
 85     int id, ans;
 86     bool rev, rt;
 87 } statePool[MAXV + MAXE], *nil;
 88 int ncnt;
 89  
 90 vector<int> adj[MAXV];
 91 Node *ptr[MAXV];
 92  
 93 void init() {
 94     ptr[0] = nil = statePool;
 95     nil->ans = -1;
 96     ncnt = 1;
 97 }
 98  
 99 Node* new_node(int i, Node *f) {
100     Node *x = statePool + ncnt++;
101     x->ch[0] = x->ch[1] = nil; x->fa = f;
102     x->id = x->ans = i;
103     x->rev = false; x->rt = true;
104     return x;
105 }
106  
107 void dfs(int u, int fa, Node *f) {
108     ptr[u] = new_node(-1, f);
109     foreach(it, adj[u]) {
110         int v = edge[*it].getv(u);
111         if(v == fa) continue;
112         dfs(v, u, new_node(*it, ptr[u]));
113     }
114 }
115  
116 int max_id(int a, int b) {
117     if(a > b) swap(a, b);
118     if(a == -1) return b;
119     return edge[a].cost > edge[b].cost ? a : b;
120 }
121  
122 void update(Node *x) {
123     x->ans = x->id;
124     FOR(k, 2) x->ans = max_id(x->ans, x->ch[k]->ans);
125 }
126  
127 void rotate(Node *x) {
128     Node *y = x->fa;
129     int t = (y->ch[1] == x);
130  
131     if(y->rt) y->rt = false, x->rt = true;
132     else y->fa->ch[y->fa->ch[1] == y] = x;
133     x->fa = y->fa;
134  
135     (y->ch[t] = x->ch[t ^ 1])->fa = y;
136     (x->ch[t ^ 1] = y)->fa = x;
137     update(y);
138 }
139  
140 void modify_rev(Node *x) {
141     if(x == nil) return ;
142     x->rev = !x->rev;
143     swap(x->ch[0], x->ch[1]);
144 }
145  
146 void pushdown(Node *x) {
147     if(x->rev) {
148         FOR(k, 2) modify_rev(x->ch[k]);
149         x->rev = false;
150     }
151 }
152  
153 void push(Node *x) {
154     if(!x->rt) push(x->fa);
155     pushdown(x);
156 }
157  
158 void splay(Node *x) {
159     push(x);
160     while(!x->rt) {
161         Node *f = x->fa, *ff = f->fa;
162         if(!f->rt) rotate((ff->ch[1] == f) == (f->ch[1] == x) ? f : x);
163         rotate(x);
164     }
165     update(x);
166 }
167  
168 Node *access(Node *x) {
169     Node *y = nil;
170     while(x != nil) {
171         splay(x);
172         x->ch[1]->rt = true;
173         (x->ch[1] = y)->rt = false;
174         update(x);
175         y = x; x = x->fa;
176     }
177     return y;
178 }
179  
180 void be_root(Node *x) {
181     access(x);
182     splay(x);
183     modify_rev(x);
184 }
185  
186 void link(Node *x, Node *y) {
187     be_root(x);
188     x->fa = y;
189 }
190  
191 void cut(Node *x, Node *y) {
192     be_root(x);
193     splay(y);
194     y->ch[0]->fa = y->fa;
195     y->ch[0]->rt = true;
196     y->fa = y->ch[0] = nil;
197     update(y);
198 }
199  
200 int query(Node *x, Node *y) {
201     be_root(x);
202     return access(y)->ans;
203 }
204  
205 int n, m, q;
206  
207 int main() {
208     n = readint(), m = readint(), q = readint();
209     for(int i = 0; i < m; ++i) edge[i].read();
210     for(int i = 0; i < q; ++i) oper[i].read();
211  
212     sort(edge, edge + m, cmp_uv);
213     for(int i = 0; i < q; ++i) if(oper[i].k == 2) {
214         oper[i].mid = lower_bound(edge, edge + m, Edge(oper[i].a, oper[i].b), cmp_uv) - edge;
215         edge[oper[i].mid].del = true;
216     }
217     kruskal(n, m);
218     sort(edge, edge + m, cmp_uv);
219  
220     init();
221     for(int i = 0; i < m; ++i) if(edge[i].use)
222         adj[edge[i].u].push_back(i), adj[edge[i].v].push_back(i);
223     dfs(1, 0, nil);
224  
225     vector<int> ans;
226     for(int i = q - 1; i >= 0; --i) {
227         int t = query(ptr[oper[i].a], ptr[oper[i].b]);
228         if(oper[i].k == 1) {
229             ans.push_back(edge[t].cost);
230         } else if(edge[t].cost > edge[oper[i].mid].cost) {
231             cut(ptr[edge[t].u], ptr[edge[t].v]);
232             Node *x = new_node(oper[i].mid, nil);
233             link(x, ptr[oper[i].a]);
234             link(x, ptr[oper[i].b]);
235         }
236     }
237  
238     for(vector<int>::reverse_iterator it = ans.rbegin(); it != ans.rend(); ++it)
239         printf("%d\n", *it);
240 }
View Code

 

posted @ 2015-02-17 16:41  Oyking  阅读(257)  评论(0编辑  收藏  举报