【bzoj4025】二分图 LCT

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

Yes
No
Yes

LCT

#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 ++ ;
}
{
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);
}
{
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;
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;
}


|转载请注明 [原文链接][作者] ，谢谢！

posted @ 2017-07-11 09:33  GXZlegend  阅读(445)  评论(0编辑  收藏  举报