BZOJ1969: [Ahoi2005]LANE 航线规划(LCT)
2018-06-17 20:44:03来源:未知 阅读 ()
Submit: 587 Solved: 259
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
1 2
1 3
3 4
4 5
4 2
1 1 5
0 4 2
1 5 1
-1
Sample Output
3
HINT
Source
时间倒流
首先把所有的操作全都读进来
然后把最终形态的树建出来,
这样删边操作就变成了加边操作
维护联通性用LCT,缩点的话用并查集维护祖先,然后暴力改祖先即可
access操作稍微有一些改动,具体看代码吧
// luogu-judger-enable-o2 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int MAXN = 1e5+10; inline int read() { char c = getchar(); int x = 0, f = 1; while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();} return x * f; } int N, M; int father[MAXN]; int find(int x) { if(father[x] == x) return father[x]; else return father[x] = find(father[x]); } int unionn(int x, int y) { int fx = find(x), fy = find(y); father[fx] = fy; } #define ls(x) T[x].ch[0] #define rs(x) T[x].ch[1] #define fa(x) T[x].f struct node { int f, ch[2], r, siz; }T[MAXN]; int ident(int x) { return T[fa(x)].ch[0] == x ? 0 : 1; } void update(int x) { T[x].siz = T[ls(x)].siz + T[rs(x)].siz + 1; } void pushdown(int x) { if(T[x].r) { T[ls(x)].r ^= 1, T[rs(x)].r ^= 1; swap(ls(x), rs(x)); T[x].r = 0; } } void connect(int x, int fa, int how) { T[x].f = fa; T[fa].ch[how] = x; } bool IsRoot(int x) { return T[fa(x)].ch[0] != x && T[fa(x)].ch[1] != x; } void rotate(int x) { int Y = fa(x), R = fa(Y), Yson = ident(x), Rson = ident(Y); int B = T[x].ch[Yson ^ 1]; T[x].f = R; if(!IsRoot(Y)) connect(x, R, Rson); connect(B, Y, Yson); connect(Y, x, Yson ^ 1); update(Y);update(x); } int st[MAXN]; void splay(int x) { int y = x, top = 0; st[++top] = y; while(!IsRoot(y)) st[++top] = y = fa(y); while(top) pushdown(st[top--]); for(int y = fa(x); !IsRoot(x); rotate(x), y = fa(x)) if(!IsRoot(y)) rotate( ident(x) == ident(y) ? y : x); } void access(int x) { for(int y = 0; x;y = x, x = fa(y) = find(fa(x)))//这里要改一下 splay(x), rs(x) = y, update(x); } void makeroot(int x) { access(x); splay(x); T[x].r ^= 1; } void link(int x, int y) { //makeroot(x); T[x].f = y; } int findroot(int x) { //makeroot(x); access(x);splay(x); pushdown(x); while(ls(x)) pushdown(x = ls(x)); splay(x); return x; } void delet(int x, int to) { if(x) father[x] = to, delet(ls(x), to), delet(rs(x), to); } void merge(int x, int y) { if(x == y) return ; makeroot(x); if(findroot(y) != x) { link(x, y); return ; } delet(rs(x), x); rs(x) = 0; update(x); } int split(int x,int y) { makeroot(x); access(y); splay(y); } struct Edge { int u,v; bool operator < (const Edge &rhs) const{ return u < rhs.u || (u == rhs.u && v < rhs.v); } }E[MAXN]; int vis[MAXN]; struct Query { int opt, x, y; }Q[MAXN]; int Qnum = 0; int ans[MAXN]; int main() { #ifdef WIN32 freopen("a.in", "r", stdin); #else //freopen("lane.in","r",stdin); //freopen("lane.out","w",stdout); #endif N = read(), M = read(); for(int i = 1; i <= N; i++) father[i] = i; for(int i = 1; i <= M; i++) { E[i].u = read(), E[i].v = read(); if(E[i].u > E[i].v) swap(E[i].u, E[i].v); } sort(E + 1, E + M + 1); while(scanf("%d", &Q[++Qnum].opt) && Q[Qnum].opt != -1) { Q[Qnum].x = read(), Q[Qnum].y = read(); if(Q[Qnum].x > Q[Qnum].y) swap(Q[Qnum].x, Q[Qnum].y); if(Q[Qnum].opt == 0) vis[lower_bound(E + 1, E + M + 1, (Edge){Q[Qnum].x, Q[Qnum].y} ) - E] = 1; } Qnum--; for(int i = 1; i <= M; i++) if(!vis[i]) merge( find(E[i].u), find(E[i].v) ); int ansnum = 0; for(int i = Qnum; i >= 1; i--) { Q[i].x = find(Q[i].x); Q[i].y = find(Q[i].y); if(Q[i].opt == 0) merge(Q[i].x, Q[i].y); else split(Q[i].x, Q[i].y), ans[++ansnum] = T[Q[i].y].siz - 1; } while(ansnum) printf("%d\n", ans[ansnum--]); return 0; }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- HDU4405 Aeroplane chess(期望dp) 2018-08-26
- BZOJ1965: [Ahoi2005]SHUFFLE 洗牌(exgcd 找规律) 2018-07-11
- P1403 [AHOI2005]约数研究 2018-06-17
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash