zoukankan      html  css  js  c++  java
  • P4172 [WC2006]水管局长 LCT维护最小生成树

    (color{#0066ff}{ 题目描述 })

    SC 省 MY 市有着庞大的地下水管网络,嘟嘟是 MY 市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从 xx 处送往 yy 处,嘟嘟需要为供水公司找到一条从 AA 至 BB 的水管的路径,接着通过信息化的控制中心通知路径上的水管进入准备送水状态,等到路径上每一条水管都准备好了,供水公司就可以开始送水了。嘟嘟一次只能处理一项送水任务,等到当前的送水任务完成了,才能处理下一项。

    在处理每项送水任务之前,路径上的水管都要进行一系列的准备操作,如清洗、消毒等等。嘟嘟在控制中心一声令下,这些水管的准备操作同时开始,但由于各条管道的长度、内径不同,进行准备操作需要的时间可能不同。供水公司总是希望嘟嘟能找到这样一条送水路径,路径上的所有管道全都准备就绪所需要的时间尽量短。嘟嘟希望你能帮助他完成这样的一个选择路径的系统,以满足供水公司的要求。另外,由于 MY 市的水管年代久远,一些水管会不时出现故障导致不能使用,你的程序必须考虑到这一点。

    不妨将 MY 市的水管网络看作一幅简单无向图(即没有自环或重边):水管是图中的边,水管的连接处为图中的结点。

    (color{#0066ff}{输入格式})

    输入文件第一行为 (3) 个整数:(N), (M), (Q) 分别表示管道连接处(结点)的数目、目前水管(无向边)的数目,以及你的程序需要处理的任务数目(包括寻找一条满足要求的路径和接受某条水管坏掉的事实)。

    以下 (M) 行,每行 (3) 个整数 (x), (y)(t)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) 尚未报废的水管一定存在)。

    (color{#0066ff}{输出格式})

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

    (color{#0066ff}{输入样例})

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

    (color{#0066ff}{输出样例})

    2
    3
    

    (color{#0066ff}{数据范围与提示})

    (N leq 1000)

    (M leq 100000)

    (Q leq 100000)

    测试数据中宣布报废的水管不超过 (5000) 条;且任何时候我们考虑的水管网络都是连通的,即从任一结点 (A) 必有至少一条水管路径通往任一结点 (B)

    (color{#0066ff}{ 题解 })

    正难则反,考虑倒着来,这样删边就成了加边

    我们只需维护最小生成树即可

    每次加边,找到链上最长边,跟当前比一下,这个可以暴力找,然后判断是否断掉

    显然可以用LCT维护

    但是发现这是边权,要弄到点上

    如果移到点上,一 splay就乱了,那怎么办呢

    假设我们有(x----y),我们考虑开一个新节点o,点权为这条边的边权,然后(x--o--y)这样连

    这样怎么弄边权都不会有影响, 就好维护了

    #include<bits/stdc++.h>
    #define LL long long
    LL in() {
    	char ch; LL x = 0, f = 1;
    	while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
    	for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
    	return x * f;
    }
    const int maxn = 2e6 + 10;
    struct node {
    	node *ch[2], *fa;
    	int val, max, rev;
    	node(int val = 0, int max = 0, int rev = 0): val(val), max(max), rev(rev) {}
    	bool ntr() { return fa && (fa->ch[1] == this || fa->ch[0] == this); }
    	bool isr() { return fa->ch[1] == this; }
    	void trn() { std::swap(ch[0], ch[1]), rev ^= 1; }
    	void upd() {
    		max = val;
    		if(ch[0]) max = std::max(max, ch[0]->max);
    		if(ch[1]) max = std::max(max, ch[1]->max);
    	}
    	void dwn() {
    		if(!rev) return;
    		if(ch[0]) ch[0]->trn();
    		if(ch[1]) ch[1]->trn();
    		rev = 0;
    	}
    	void clr() {
    		if(ch[0]) ch[0]->fa = NULL;
    		if(ch[1]) ch[1]->fa = NULL;
    		ch[0] = ch[1] = NULL;
    	}
    };
    node pool[maxn];
    struct E {
    	int from, to, dis;
    	E(int from = 0, int to = 0, int dis = 0): from(from), to(to), dis(dis) {}
    	friend bool operator < (const E &a, const E &b) {
    		return a.from == b.from? a.to < b.to : a.from < b.from;
    	}
    }e[maxn], q[maxn];
    std::map<E, int> mp;
    std::set<E> v;
    int n, m, Q, ans[maxn];
    void rot(node *x) {
    	node *y = x->fa, *z = y->fa;
    	int k = x->isr(); node *w = x->ch[!k];
    	if(y->ntr()) z->ch[y->isr()] = x;
    	x->ch[!k] = y, y->ch[k] = w;
    	y->fa = x, x->fa = z;
    	if(w) w->fa = y;
    	y->upd(), x->upd();
    }
    void splay(node *o) {
    	static node *st[maxn];
    	int top;
    	st[top = 1] = o;
    	while(st[top]->ntr()) st[top + 1] = st[top]->fa, top++;
    	while(top) st[top--]->dwn();
    	while(o->ntr()) {
    		if(o->fa->ntr()) rot(o->isr() ^ o->fa->isr()? o : o->fa);
    		rot(o);
    	}
    }
    void access(node *x) {
    	for(node *y = NULL; x; x = (y = x)->fa)
    		splay(x), x->ch[1] = y, x->upd();
    }
    void makeroot(node *x) { access(x), splay(x), x->trn(); }
    node *findroot(node *x) {
    	access(x), splay(x);
    	while(x->dwn(), x->ch[0]) x = x->ch[0];
    	return splay(x), x;
    }
    node *findmax(node *o) {
    	while(o->dwn(), o->val != o->max) {
    		if(o->ch[0] && o->ch[0]->max == o->max) o = o->ch[0];
    		else o = o->ch[1];
    	}
    	return o;
    }
    void link(node *x, node *y) { makeroot(x), x->fa = y; }
    void add(int id) {
    	node *o = pool + n + id, *from = pool + e[id].from, *to = pool + e[id].to;
    	if(findroot(from) == findroot(to)) {
    		makeroot(from), access(to), splay(to);
    		if(to->max <= o->val) return;
    		node *pos = findmax(to);
    		splay(pos), pos->clr(), pos->upd();
    	}
    	link(from, o), link(o, to);
    }
    int query(int x, int y) {
    	node *from = pool + x, *to = pool + y;
    	makeroot(from), access(to), splay(to);
    	return to->max;
    }
    int main() {
    	n = in(), m = in(), Q = in();
    	for(int i = 1; i <= m; i++) {
    		e[i].from = in(), e[i].to = in(), e[i].dis = in();
    		if(e[i].from > e[i].to) std::swap(e[i].from, e[i].to);
    		mp[e[i]] = i;
    		pool[n + i].val = e[i].dis, pool[n + 1].upd();
    	}
    	for(int i = 1; i <= Q; i++) {
    		q[i].dis = in(), q[i].from = in(), q[i].to = in();
    		if(q[i].from > q[i].to) std::swap(q[i].from, q[i].to);
    		if(q[i].dis == 2) v.insert(E(q[i].from, q[i].to, 233));
    	}
    	for(int i = 1; i <= m; i++) if(!v.count(e[i])) add(i);
    	int num = 0;
    	for(int i = Q; i >= 1; i--) {
    		if(q[i].dis == 1) ans[++num] = query(q[i].from, q[i].to);
    		else add(mp[E(q[i].from, q[i].to)]);
    	}
    	for(int i = num; i >= 1; i--) printf("%d
    ", ans[i]);
    	return 0;
    }
    
  • 相关阅读:
    C++11学习笔记
    孙鑫MFC学习笔记20:Hook编程
    孙鑫MFC学习笔记19:动态链接库
    孙鑫MFC学习笔记18:ActiveX
    孙鑫MFC学习笔记17:进程间通信
    孙鑫MFC学习笔记16:异步套接字
    孙鑫MFC学习笔记:15多线程
    ionic2 使用slides制作滑动效果的类型选择栏
    JavaScript简明教程之Node.js
    ionic2实战-使用Chart.js
  • 原文地址:https://www.cnblogs.com/olinr/p/10369699.html
Copyright © 2011-2022 走看看