CF555E Case of Computer Network

Source: Internet
Author: User

Portal Test Instructions

Exercises

Easy to find, in the same side of the double inside the point we do not care about it, it is sure to.

So let's pinch and turn the graph into a tree. This way, the path of $s $ to $t $ can be $\mathrm{lca}$.

We put the $s $ to $\mathrm{lca}$ the path up the marker, put the $\mathrm{lca}$ to the $t $ path to hit the downward marker.

As long as there are no paths with two kinds of markers, it is possible.

Marking uses a differential implementation on the tree. Time complexity: $\mathcal o\left (n+m+q\mathrm{lg}n\right) $.

Attached code:

#pragmaGCC optimize (3)#include<bits/stdc++.h>using namespacestd;#defineDown (x, y) x = min (x, y)intN, M, q;structEdge {int  from, to, NXT, id;} edges[400005];structEdgE {intto, NXT;} edges[400005];intE0, E =1, e2 =1, hed[200005], hed[200005], Tim =0, fa[200005], dfn[200005], low[200005], scc[200005], dis[200005], lca[ -][200005];intflg[200005];BOOLroot[200005];BOOLgoon[200005];intleaves[200005], top=0;Const intClean_flag =0x1;Const intUp_flag =0x2;Const intDown_flag =0x4; Stack<int>S;inlineBOOLHasflag (intXintattr) {    returnFLG[X] &attr;} InlineBOOLSetflag (intXintattr) {    if(attr = = Up_flag && hasflag (x, Down_flag))return false; if(attr = = Down_flag && hasflag (x, Up_flag))return false; FLG[X]|=attr; return true;} InlinevoidAddedge (intXinty) {edges[e]=(Edge) {x, Y, hed[x], E2}; HED[X]= e++; Edges[e]= (edge) {y, X, hed[y], e2++}; Hed[y]= e++;} InlinevoidAddedge (intXinty) {edges[e]=(EdgE) {y, hed[x]}; HED[X]= e++;} Inlinevoid_tarjan (intx) {dfn[x]= Low[x] = + +Tim;    S.push (x);  for(intE=HED[X]; E E=edges[e].nxt) {        inty =edges[e].to; if(Edges[e].id = = Fa[x])Continue; if(!Dfn[y]) {Fa[y]=edges[e].id;            _tarjan (y);        Down (Low[x], low[y]); } ElseDown (Low[x], dfn[y]); }    if(Dfn[x] = =Low[x]) {Scc[x]= ++E2;  while(S.top ()! =x) {scc[s.top ()]=E2;        S.pop ();    } s.pop (); }}inlinevoidTarjan () { for(intI=1; i<=n; i++)if(!Dfn[i]) _tarjan (i);  for(inti =0; i < E0; i++) {        intx = Edges[i]. from, y =edges[i].to; if(Scc[x] = = Scc[y])Continue;    Addedge (Scc[x], scc[y]); }}inlinevoidLca_pre () { for(intI=1; i< -; i++)         for(intj=1; j<=n; J + +) Lca[i][j]= lca[i-1][lca[i-1][j]];} InlineintLCA (intXinty) {if(Dis[x] >Dis[y]) swap (x, y); intU = dis[x], V =Dis[y]; intX=x, y=y;  for(intDet = v-u, i=0; Det det>>=1, ++i)if(Det &1* Z =Lca[i][y]; if(X = = Y)returnX;  for(intI= -; i>=0; i--) {        if(Lca[i][x] = = Lca[i][y])Continue; X= Lca[i][x]; Y =Lca[i][y]; }    returnlca[0][x];}intgroup[200005], Current;inlinevoidDfsintXintFA) {    BOOLIsLeaf =true; GROUP[X] =Current ;  for(intE=HED[X]; E E=edges[e].nxt) {        inty =edges[e].to; if(Y! =FA) {IsLeaf=false; lca[0][y] =x; Dis[y]= Dis[x] +1;        DFS (y, x); }    }    if(isleaf) leaves[top++] =x;} InlineBOOLAkmachine (intXintflag) {     while(!Root[x]) {        if(Goon[x])return true; GOON[X]=true; if(Hasflag (x, Up_flag) &&!hasflag (x, Clean_flag) && (FLAG &Down_flag)|| Hasflag (x, Down_flag) &&!hasflag (x, Clean_flag) && (FLAG &Up_flag)) return false; if(Hasflag (x, clean_flag)) FLAG =0; Flag= Flg[x] & (Up_flag |Down_flag); X= lca[0][x]; }    return true;} InlineBOOLCheck () { for(intI=0; i<top; i++)if(!akmachine (Leaves[i),0))return false; return true;} InlineBOOLsolve () { for(intI=1; i<=n; i++)        if(!Dis[i]) { Current= i; lca[0][i] = i; Root[i] =1; DFS (i,0);    } lca_pre ();  while(q--) {        intS, t; CIN >> S >>T; S= Scc[s]; t =Scc[t]; if(Group[s]! = Group[t])return false; if(s = = t)Continue; intp =LCA (S, t); if(P! = s)if(!setflag (S, Up_flag))return false; if(P! = t)if(!setflag (t, Down_flag))return false;    Setflag (P, Clean_flag); }    returncheck ();}intMain () {iOS:: Sync_with_stdio (false); Cin.tie (0); CIN>> n >> M >>Q;  for(intI=1; i<=m; i++) {        intx, y; CIN >> x >>y;    Addedge (x, y); } E0= e; E =1; E2 =0; Tarjan (); cout<< (Solve)?"Yes":"No") <<Endl; return 0;}

CF555E Case of Computer Network

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.