zoukankan      html  css  js  c++  java
  • 【bzoj4025】二分图 LCT

    题目描述

    神犇有一个n个节点的图。因为神犇是神犇,所以在T时间内一些边会出现后消失。神犇要求出每一时间段内这个图是否是二分图。这么简单的问题神犇当然会做了,于是他想考考你。

    输入

    输入数据的第一行是三个整数n,m,T。
    第2行到第m+1行,每行4个整数u,v,start,end。第i+1行的四个整数表示第i条边连接u,v两个点,这条边在start时刻出现,在第end时刻消失。

    输出

    输出包含T行。在第i行中,如果第i时间段内这个图是二分图,那么输出“Yes”,否则输出“No”,不含引号。

    样例输入

    3 3 3
    1 2 0 2
    2 3 0 3
    1 3 1 2

    样例输出

    Yes
    No
    Yes


    题解

    LCT

    考虑二分图的判断方法:没有奇环

    那么我们可以按照每条边的出现时间从小到大排序。对于每条边,如果它形成了奇环,那么这个环上所有边的消失时间的最小值之后这个奇环就不复存在。

    所以可以使用LCT维护以消失时间为关键字的最大生成树,每次加边时更新这棵生成树,同时如果产生了奇环,就把这个环上的最小消失时间加到桶中。每个时间把该时间的边弹掉,看是否还有其它形成奇环的边,如果有则不是二分图,否则是二分图。

    判断是否是奇环可以通过维护size来实现,对于最大生成树,由于使用的是边权,所以需要想办法转化为点权。我们可以在要连接的两个点之间添加虚点,存着这条边的边权,这样边权就转化为了点权。记录一下消失时间最小的边(虚点)是哪个,判断与新边的大小关系,并决定删除哪个即可。

    另外,本题卡常= = (其实本机开O2测才11s,不知道bz上怎么就20s++)

    需要使用fread读入优化及puts输出才能勉强通过(还好时间没有在最后一页= =)

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #define N 100010
    using namespace std;
    struct data
    {
    	int x , y , t1 , t2;
    }a[N << 1];
    int n , fa[N << 2] , c[2][N << 2] , si[N << 2] , w[N << 2] , mp[N << 2] , rev[N << 2] , num[N] , sum , now;
    inline char nc()
    {
    	static char buf[100000] , *p1 , *p2;
    	return p1 == p2 && (p2 = (p1 = buf) + fread(buf , 1 , 100000 , stdin) , p1 == p2) ? EOF : *p1 ++ ;
    }
    inline int read()
    {
    	int ret = 0; char ch = nc();
    	while(ch < '0' || ch > '9') ch = nc();
    	while(ch >= '0' && ch <= '9') ret = (ret << 3) + (ret << 1) + ch - 48 , ch = nc();
    	return ret;
    }
    bool cmp(data a , data b)
    {
    	return a.t1 < b.t1;
    }
    void pushup(int x)
    {
    	si[x] = si[c[0][x]] + si[c[1][x]] + 1;
    	mp[x] = x;
    	if(w[mp[c[0][x]]] < w[mp[x]]) mp[x] = mp[c[0][x]];
    	if(w[mp[c[1][x]]] < w[mp[x]]) mp[x] = mp[c[1][x]];
    }
    void pushdown(int x)
    {
    	if(rev[x])
    	{
    		int l = c[0][x] , r = c[1][x];
    		swap(c[0][l] , c[1][l]) , swap(c[0][r] , c[1][r]);
    		rev[l] ^= 1 , rev[r] ^= 1 , rev[x] = 0;
    	}
    }
    bool isroot(int x)
    {
    	return c[0][fa[x]] != x && c[1][fa[x]] != x;
    }
    void update(int x)
    {
    	if(!isroot(x)) update(fa[x]);
    	pushdown(x);
    }
    void rotate(int x)
    {
    	int y = fa[x] , z = fa[y] , l = (c[1][y] == x) , r = l ^ 1;
    	if(!isroot(y)) c[c[1][z] == y][z] = x;
    	fa[x] = z , fa[y] = x , fa[c[r][x]] = y , c[l][y] = c[r][x] , c[r][x] = y;
    	pushup(y) , pushup(x);
    }
    void splay(int x)
    {
    	update(x);
    	while(!isroot(x))
    	{
    		int y = fa[x] , z = fa[y];
    		if(!isroot(y))
    		{
    			if((c[0][y] == x) ^ (c[0][z] == y)) rotate(x);
    			else rotate(y);
    		}
    		rotate(x);
    	}
    }
    void access(int x)
    {
    	int t = 0;
    	while(x) splay(x) , c[1][x] = t , pushup(x) , t = x , x = fa[x];
    }
    int find(int x)
    {
    	access(x) , splay(x);
    	while(c[0][x]) pushdown(x) , x = c[0][x];
    	return x;
    }
    void makeroot(int x)
    {
    	access(x) , splay(x);
    	swap(c[0][x] , c[1][x]) , rev[x] ^= 1;
    }
    void link(int x , int y)
    {
    	makeroot(x) , fa[x] = y;
    }
    void cut(int x , int y)
    {
    	makeroot(x) , access(y) , splay(y) , fa[x] = c[0][y] = 0 , pushup(y);
    }
    void split(int x , int y)
    {
    	makeroot(x) , access(y) , splay(y);
    }
    void add(int p)
    {
    	int tx = a[p].x , ty = a[p].y , tmp , flag = 0;
    	if(tx == ty && a[p].t2 > now) num[a[p].t2] ++ , sum ++ ;
    	else
    	{
    		if(find(tx) != find(ty)) link(p + n , tx) , link(p + n , ty);
    		else
    		{
    			split(tx , ty);
    			if(!((si[ty] >> 1) & 1)) flag = 1;
    			if(w[mp[ty]] >= a[p].t2) tmp = p;
    			else tmp = mp[ty] - n , cut(tmp + n , a[tmp].x) , cut(tmp + n , a[tmp].y) , link(p + n , tx) , link(p + n , ty);
    			if(flag && a[tmp].t2 > now) num[a[tmp].t2] ++ , sum ++ ;
    		}
    	}
    }
    int main()
    {
    	int m , t , i , p = 1;
    	n = read() , m = read() , t = read();
    	for(i = 1 ; i <= m ; i ++ ) a[i].x = read() , a[i].y = read() , a[i].t1 = read() , a[i].t2 = read();
    	sort(a + 1 , a + m + 1 , cmp);
    	for(i = 1 ; i <= n + m ; i ++ ) si[i] = 1 , mp[i] = i;
    	for(i = 0 ; i <= n ; i ++ ) w[i] = 1 << 30;
    	for(i = 1 ; i <= m ; i ++ ) w[i + n] = a[i].t2;
    	for(i = 0 ; i < t ; i ++ )
    	{
    		now = i;
    		while(p <= m && a[p].t1 <= i) add(p ++ );
    		sum -= num[i];
    		if(sum) puts("No");
    		else puts("Yes");
    	}
    	return 0;
    }
    

     

  • 相关阅读:
    mysql中bigint、int、mediumint、smallint 和 tinyint的取值范围
    centos6.5下安装samba服务器与配置
    centos 6.5 安装图形界面【转】
    Linux 下添加用户,修改权限
    Linux下自动调整时间和时区与Internet时间同步
    C#下利用封包、拆包原理解决Socket粘包、半包问题(新手篇)
    Unity脚步之NetworkBehaviour下前进、后退、左右转向的简单移动
    Token 在 Ajax 请求头中,服务端过滤器跨域问题
    【游戏】【暗黑2】重置属性点和技能点
    ASCII
  • 原文地址:https://www.cnblogs.com/GXZlegend/p/7148985.html
Copyright © 2011-2022 走看看