[破碎的状态] [-16] 100960 H
感谢@似水流年 翻译..
n个点,要求强制在线(交互):
1(C),连接两点
2(D),删除某两点之间的连边
3(T),问两点是否有边相连
4(E),结束程序
=================
特殊条件:
1,一共会调用n-1次C,这n-1次C好像会把整个图连接成一颗树
2,一共会调用n-1次D
3,n<=100000,总询问数<=300000
实际上..
这题可以直接LCT..
我就当LCT练手题了..
没特判x==y被WA了好久好难过...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | #include<set> #include<map> #include<list> #include<queue> #include<stack> #include<string> #include<time.h> #include<math.h> #include<memory> #include<vector> #include<bitset> #include<fstream> #include<stdio.h> #include<utility> #include<sstream> #include<string.h> #include<iostream> #include<stdlib.h> #include<algorithm> using namespace std; struct node { bool rev_tag; node * father; node * ch[2]; }; node * null; node * new_node() { static node a[100005]; static int top=0; node * t=&a[top++]; t->father=null; t->ch[0]=null; t->ch[1]=null; t->rev_tag= false ; return t; } void rotate(node * &x, int c) { node * y=x->ch[c]; x->ch[c]=y->ch[!c]; y->ch[!c]->father=x; y->ch[!c]=x; y->father=x->father; x->father=y; x=y; } void push_down(node * &x) { if (x->rev_tag) { swap(x->ch[0],x->ch[1]); x->ch[0]->rev_tag^=1; x->ch[1]->rev_tag^=1; x->rev_tag= false ; } } int get_up(node * x) { if (x->father->ch[0]==x) return 0; if (x->father->ch[1]==x) return 1; return -1; } void splay(node * &x) { for (;;) { push_down(x->father->father); push_down(x->father); push_down(x); int t1=get_up(x); if (t1==-1) return ; int t2=get_up(x->father); if (t2==-1) { x=x->father; rotate(x,t1); } else { int t3=get_up(x->father->father); if (t3!=-1) { if (t1==t2) { x=x->father; x=x->father; x=x->father; rotate(x->ch[t3],t2); rotate(x->ch[t3],t1); x=x->ch[t3]; } else { x=x->father; x=x->father; rotate(x->ch[t2],t1); x=x->father; rotate(x->ch[t3],t2); x=x->ch[t3]; } } else { if (t1==t2) { x=x->father; x=x->father; rotate(x,t2); rotate(x,t1); } else { x=x->father; x=x->father; rotate(x->ch[t2],t1); rotate(x,t2); } } } } } void access(node * &x) { for (;;) { splay(x); if (x->father==null) { x->ch[1]=null; return ; } splay(x->father); x->father->ch[1]=x; } } void evert(node * &x) { access(x); x->rev_tag^=1; } void link(node * x,node * y) { evert(x); x->father=y; } void cut(node * x,node * y) { evert(x); access(y); y->ch[0]->father=null; y->ch[0]=null; } node * a[100005]; int main() { #ifdef absi2011 freopen ( "input.txt" , "r" ,stdin); freopen ( "output.txt" , "w" ,stdout); #endif int n; scanf ( "%d" ,&n); int i; null=new_node(); null->father=null; null->ch[0]=null; null->ch[1]=null; for (i=0;i<n;i++) { a[i]=new_node(); } for (;;) { static char c[15]; scanf ( "%s" ,c); if (c[0]== 'C' ) { int x,y; scanf ( "%d%d" ,&x,&y); x--; y--; link(a[x],a[y]); } else if (c[0]== 'D' ) { int x,y; scanf ( "%d%d" ,&x,&y); x--; y--; cut(a[x],a[y]); } else if (c[0]== 'T' ) { int x,y; scanf ( "%d%d" ,&x,&y); x--; y--; evert(a[x]); access(a[y]); if ((a[x]->father!=null)||(x==y)) { puts ( "YES" ); } else { puts ( "NO" ); } fflush (stdout); } else { break ; } } return 0; } |