zoukankan      html  css  js  c++  java
  • [NOI2014]魔法森林 LCT

    题面

    [NOI2014]魔法森林

    题解

    一条路径的代价为路径上的(max(a[i]) + max(b[i])),因为一条边同时有$a[i], b[i]$2种权值,直接处理不好同时兼顾到,所以我们考虑一个暴力的做法。

    一个暴力的做法:

    我们枚举(max(a[i])),然后强制只能选满足这个限制的边,那么此时(a[i])就已经不用管了,只需要最小化(max(b[i]))即可。
    因此我们求一下最小生成树,然后看一下这条路径的(max(b[i]))就可以了。

    一个小优化:

    考虑如果我们枚举到一些不存在的(max(a[i])),显然是没用的,因此我们只需要枚举(a[i])最大可以取到与哪条边的(a[i])相等即可。

    一个大优化:

    注意到我们每枚举一次就重建最小生成树太亏了,毕竟我们有这样一个结论:
    集合内多加一条边然后求最小生成树,这个新的最小生成树一定是在原来最小生成树基础上多加一条边构成的。
    因此我们将新加入集合的边依次拿来尝试更新原来的最小生成树。

    考虑怎么维护。
    强行加入一条x到y的边后,原来的最小生成树将会变成一个基环树,因此我们要舍弃一条边。
    我们在原来最小生成树上查询x到y路径上的最大值,看是否比当前加入的边大,如果比当前加入的边大,那么我们就断开那条最大的边,然后加入当前加入的边,否则就不做修改。

    但是查询边的最大值不太好处理,我们可以将一条边视作一个点,然后连x ---> y 就连x ---> tmp ---> y,其中tmp为边的编号,然后断开的时候也断2条边。

    #include<bits/stdc++.h>
    using namespace std;
    #define R register int
    #define AC 200000
    #define inf 100000000
    
    int n, m, ans = inf, top;
    int maxn[AC], pos[AC], rev[AC], son[AC][2], fa[AC], val[AC];
    int father[AC], q[AC];
    struct node{
    	int x, y, a, b;
    }way[AC];
    
    inline bool cmp(node a, node b) {return a.a < b.a;}
    
    inline int read()
    {
    	int x = 0;char c = getchar();
    	while(c > '9' || c < '0') c = getchar();
    	while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    	return x;
    }
    
    inline void upmax(int &a, int b) {if(b > a) a = b;}
    inline void upmin(int &a, int b) {if(b < a) a = b;}
    
    inline int find(int x)
    {
    	if(father[x] == x) return x;
    	else return father[x] = find(father[x]);
    }
    
    struct Link_Cut_Tree{
    	
    	void pushdown(int x)
    	{
    		if(!rev[x]) return ;
    		rev[son[x][0]] ^= 1, rev[son[x][1]] ^= 1;
    		swap(son[x][0], son[x][1]), rev[x] = 0;//别忘了换儿子,之前还没换的
    	}
    	
    	bool is_root(int x) {return (son[fa[x]][1] != x) && (son[fa[x]][0] != x);}
    
    	void update(int x) 
    	{
    		int tmp = (maxn[son[x][0]] > maxn[son[x][1]]) ? son[x][0] : son[x][1];
    		if(val[x] > maxn[tmp]) maxn[x] = val[x], pos[x] = x;
    		else maxn[x] = maxn[tmp], pos[x] = pos[tmp];
    	}
    	
    	void rotate(int x)
    	{
    		int y = fa[x], z = fa[y], k = son[y][1] == x; 
    		if(!is_root(y)) son[z][son[z][1] == y] = x;
    		fa[x] = z, fa[son[x][k ^ 1]] = y, son[y][k] = son[x][k ^ 1];
    		fa[y] = x, son[x][k ^ 1] = y, update(y), update(x);
    	}
    	
    	void splay(int x)
    	{
    		q[top = 1] = x;
    		for(R i = x; fa[i]; i = fa[i]) q[++ top] = fa[i];
    		while(top) pushdown(q[top]), -- top;
    		while(!is_root(x))
    		{
    			int y = fa[x], z = fa[y];
    			if(!is_root(y)) (son[y][1] == x) ^ (son[z][1] == y) ? rotate(x) : rotate(y);
    			rotate(x);
    		}
    	}
    	
    	void access(int x)
    	{for(R i = 0; x; i = x, x = fa[x]) splay(x), son[x][1] = i, update(x);}
    	
    	void make_root(int x) {access(x), splay(x), rev[x] ^= 1;}
    
    	void split(int x, int y) {make_root(x), access(y), splay(y);}
    	
    	int get_rot(int x)
    	{
    		access(x), splay(x);
    		while(son[x][0]) x = son[x][0];
    		return x;
    	}
    	
    	void link(int x, int y)
    	{
    		int xx = get_rot(x), yy = get_rot(y);
    		if(xx == yy) return ;
    		make_root(x), fa[x] = y;
    	}
    	
    	void cut(int x, int y)
    	{
    		int xx = get_rot(x), yy = get_rot(y);
    		if(xx != yy) return ;
    		split(x, y);//split后默认y在最上面,x在y左边
    		if(!son[x][1] && !son[x][0]) son[y][0] = 0, fa[x] = 0;
    	}
    	
    	void insert(int i)
    	{
    		int x = way[i].x, y = way[i].y, fx = find(x), fy = find(y);
    		if(fx != fy) 
    		{
    			link(x, n + i), link(n + i, y);
    			if(fx > fy) swap(fx, fy);
    			father[fy] = fx;
    		}
    		else
    		{
    			split(x, y);
    			if(maxn[y] <= val[n + i]) return ;
    			int tmp = pos[y];
    			//son[tmp][0] = son[tmp][1] = fa[tmp] = 0;//把这个点删掉
    			cut(way[tmp - n].x, tmp), cut(tmp, way[tmp - n].y);//应该是断开和这条边相邻的点
    			link(x, n + i), link(n + i, y);
    		}	
    		if(find(1) == find(n)) split(1, n), upmin(ans, way[i].a + maxn[n]);
    	}
    	
    }LCT;
    
    void pre()
    {
    	n = read(), m = read();
    	for(R i = 1; i <= n; i ++) father[i] = i;
    	for(R i = 1; i <= m; i ++)
    		way[i].x = read(), way[i].y = read(), way[i].a = read(), way[i].b = read();
    	sort(way + 1, way + m + 1, cmp);
    }
    
    void work()
    {
    	for(R i = 1; i <= m; i ++) val[n + i] = way[i].b, LCT.insert(i);
    	if(ans != inf) printf("%d
    ", ans);
    	else printf("-1
    ");
    }
    
    int main()
    {
    	freopen("in.in", "r", stdin);
    	pre();
    	work();
    	fclose(stdin);
    	return 0;
    }
    
  • 相关阅读:
    Hadoop杂记
    hadoop主节点(NameNode)备份策略以及恢复方法
    (转)第12章 Shell脚本编程
    Hadoop添加删除节点
    secondarynamenode异常
    (转)Memcached笔记——(一)安装&常规错误&监控
    浅(kou)谈(hu)杜教筛
    Pollard_Rho 算法
    Miller_Rabin 素数判定算法
    zoj分类(包括poj已做的)
  • 原文地址:https://www.cnblogs.com/ww3113306/p/10439199.html
Copyright © 2011-2022 走看看