All day a ghost animal problem, feel the pill ...
This problem in the Xjoi simulation problem, too God ...
The core of this problem is to divide the cograph and try to find out the synthesis process of cograph.
We take the edges of this picture as black edges, and the edges that appear in the complement of this graph are white edges, and the black and white edges form a complete picture.
1. If the current black edge of this chart is not connected, then you can check all the black edge of the block is not cograph, if all are cograph, then the original is Cograph
2. If the current picture of the white edge is not connected, then you can check all the white edge of the connecting block is not cograph, if all is cograph, then the original image is Cograph
3. If the white edge of the current picture is connected, return directly to the graph is not cograph
This is obviously correct, but the complexity of the time is too high, the complexity of each division is \ (o (m) \), because the white edge has \ (o (n^2) \) bar, so this procedure is also \ (o (n^2) \)
It's obviously going to explode ...
Consider optimizing this practice, we do not need to enumerate all white edges, when a white edge is found, it is connected to the two white Unicom block merge, the number of merges is \ (O (n) \)
Therefore, we need a data structure that supports fast merging of unicom blocks, querying one unicom block to all sides of another unicom block ...
Here we use the linked list to maintain the Unicom block ... (Why not use and check the set?) Because I want to access all the points of the Unicom block
This ensures that the edge of each query must not be in the same current block,
The query between the two points is either a black edge or a white edge, only the black edge of the \ (O (m) \), because only \ (o (n) \) Merge, so sweep to the white edge only \ (o (n) \), the time complexity is \ (o (n + m) \)
So for a problem, you just need \ (O (n + m) \) to turn it into a number of small original problems.
Because the cograph of each layer has at least \ (O (n) \) bars connected to the black edge between the white Unicom blocks is deleted, so the number of layers divided is \o (min (m/n, n) \)
Total time Complexity \o ((n + m) \sqrt (n) \)
Run a little bit slow ah ~
#include <bits/stdc++.h>#defineN 300000using namespacestd; Vector<int>Bi[n], bn[n];intT, N, M;intAi[n];intNx[n], ne[n], nl[n], tot;intVis[n], td[n], tt[n], col[n];inttmp;voidDFS1 (intTintc) {Vis[t]=C; for(inti =0; I < bi[t].size (); ++i)if(!Vis[bi[t][i]]) DFS1 (Bi[t][i], c);}intSolve2 (intt);intSolve1 (intt) { intNW = tmp +1; for(intp = t; P p = nx[p]) vis[p] =0; for(intp = t; P p =Nx[p])if(!vis[p]) DFS1 (p, vis[p]= ++tmp); for(intp = t; P p =Nx[p]) { if(!tt[vis[p]]) tt[vis[p] [td[vis[p]] =p; Else{nx[td[vis[p] ]=p; TD[VIS[P]]=p; } } for(inti = NW; I <= tmp; ++i) {Nx[td[i]]=0; if(!solve2 (Tt[i]))return 0; } return 1;}Set<int>S[n];intTestintAintb) { returnS[a].count (b);}intSolve2 (intt) { if(Nx[t] = =0)return 1; for(intp = t; P p = nx[p]) ne[p] = Nx[p], nl[p] =p; for(intp = t; P p = ne[p]) nx[p] =0; for(intp = t; P p =Ne[p]) { for(intA = p; A A =Nx[a]) { for(intQ = ne[p], c =p; q;) { intBo =0; for(intb = q; b b =Nx[b])if(!Test (A, B)) {Bo=1; Gotohaha; } haha:if(bo) {nx[nl[p]]=Q; NL[P]=Nl[q]; NL[Q]=0; NE[C]=Ne[q]; NE[Q]=0; Q=Ne[c]; } Else{C=Ne[c]; Q=Ne[q]; } } } } if(Ne[t] = =0)return 0; for(intp = t; P p =Ne[p]) for(intQ = p; Q Q =Nx[q]) col[q]=p, bn[q].clear (); for(intp = t; P p =Ne[p]) for(intQ = p; Q Q =Nx[q]) for(intA =0; A < Bi[q].size (); ++a)if(Col[bi[q][a]] = =Col[q]) bn[q].push_back (Bi[q][a]); for(intp = t; P p =Ne[p]) for(intQ = p; Q Q =Nx[q]) bi[q]=Bn[q]; Vector<int>NLS; for(intp = t; P p =ne[p]) Nls.push_back (p); for(intp =0; P < nls.size (); ++p)if(!solve1 (Nls[p]))return 0; return 1;}intMain () {//freopen ("c.in", "R", stdin);scanf"%d", &T); while(T--) {scanf ("%d%d", &n, &m); for(inti =1; I <= m; ++i) {intA, B; scanf ("%d%d", &a, &b); Bi[a].push_back (b); S[a].insert (b); Bi[b].push_back (a); S[b].insert (a); } for(inti =1; I < n; + + i) nx[i] = i +1, ne[i] =0; Nx[n]=0; if(Solve1 (1)) puts ("TAK");ElsePuts"NIE"); for(inti =1; I <= N; ++i) bi[i].clear (), s[i].clear (); }}
Bzoj 2075: [Poi2004]kag