【bzoj4025】二分图 LCT
生活随笔
收集整理的這篇文章主要介紹了
【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; }?
?
轉載于:https://www.cnblogs.com/GXZlegend/p/7148985.html
總結
以上是生活随笔為你收集整理的【bzoj4025】二分图 LCT的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jmeter 插件 监视器 图形界面使
- 下一篇: 20170505