codeforce 521E

Posted 殇雪

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了codeforce 521E相关的知识,希望对你有一定的参考价值。

题链

SOL:我们发现充要条件是有相交环,dfs即可。

#include<bits/stdc++.h>
#define sight(c) (‘0‘<=c&&c<=‘9‘)
#define getchar nc
#define N 200005
#define eho(i,x) for(int i=head[x];i;i=net[i])
using namespace std;
void write(int x){if (x<10) {putchar(0+x); return;} write(x/10); putchar(0+x%10);}
inline void writeln(int x){ if (x<0) putchar(-),x*=-1; write(x); putchar(\n); }
inline void writel(int x){ if (x<0) putchar(-),x*=-1; write(x); putchar( ); }
inline char nc(){
static char buf[1000000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline void read(int &x){
    static char c;
    for (c=getchar();!sight(c);c=getchar());
    for (x=0;sight(c);c=getchar())x=x*10+c-48;
}
int head[N],tot=1,fall[N<<1],net[N<<1];
inline void add(int x,int y) {
    fall[++tot]=y; net[tot]=head[x]; head[x]=tot;
}
int n,m,au,av,flag,dep[N],f[N],tag[N],sta[N];
#define V fall[p]
void dfs(int u,int fa) {
    sta[++*sta]=u;
    eho(p,u) if (p^fa^1) 
            if (!dep[V]) f[V]=u,dep[V]=dep[u]+1,dfs(V,p),tag[u]+=tag[V];
            else if (dep[V]<dep[u]) tag[u]++,tag[V]--;
    if (tag[u]>=2) flag=1,au=fall[fa^1],av=fall[fa];
}
int inn[N],cir1,cir2,lst1[N],lst2[N],in1[N],in2[N];
inline void find(int u,int fa) {
    inn[u]=1;
    eho(p,u)  if (p^fa^1) 
            if (!dep[V]) dep[V]=dep[u]+1,find(V,p);
            else if (dep[V]<dep[u]) 
                if (inn[au] && inn[av] && dep[V]<=dep[au] && dep[V]<=dep[av]) {
                    if (!cir1) {
                        cir1=p; int t=u; while (dep[t]>=dep[V]) in1[t]=1,lst1[++*lst1]=t,t=f[t];
                    } else if (!cir2) {
                        cir2=p; int t=u; while (dep[t]>=dep[V]) in2[t]=1,lst2[++*lst2]=t,t=f[t];
                    }
            }
    inn[u]=0;
}
int pnt,lst[N];
inline void Print() { writel(pnt);for (int i=1; i<=pnt; i++) writel(lst[i]);   putchar(\n);}
int x,y,u1,v1,u2,v2;
int main() {
    freopen("racing.in","r",stdin);
    freopen("racing.out","w",stdout);
    read(n); read(m);
    for (int i=1; i<=m; i++) read(x),read(y),add(x,y),add(y,x);
    for (int r=1; r<=n; r++)
        if (!dep[r]) {
            *sta=0;*lst1=0;*lst2=0;cir1=0;cir2=0;flag=0;dep[r]=1;
            dfs(r,0);
            if (!flag) continue;
            for (int i=1; i<=*sta; i++) dep[sta[i]]=0;
            dep[r]=1;find(r,0);au=0,av=0;
            for (int i=1; i<=*sta; i++)
                if (in1[sta[i]]&&in2[sta[i]]) {
                    if (!au||dep[au]>dep[sta[i]]) au=sta[i];
                    if (!av||dep[av]<dep[sta[i]]) av=sta[i];
                }  
            puts("YES"); pnt=0;
            for (int i=av;dep[i]>=dep[au];i=f[i]) lst[++pnt]=i;
            Print();
            int pos1,pos2;
            for (int i=1; i<=*lst1; i++)
                if (lst1[i]==au) pos1=i;
                else if (lst1[i]==av) pos2=i; pnt=0;
            for (int i=pos2; i!=pos1; i=i==1?*lst1:i-1) lst[++pnt]=lst1[i];
            lst[++pnt]=lst1[pos1]; Print();
            for (int i=1; i<=*lst2; i++) if (lst2[i]==au) pos1=i; else if (lst2[i]==av) pos2=i;
            pnt=0;
            for (int i=pos2; i!=pos1; i=i==1?*lst2:i-1) lst[++pnt]=lst2[i];
            lst[++pnt]=lst2[pos1];
            Print();return 0;
        }puts("NO");
    return 0;
}

 

以上是关于codeforce 521E的主要内容,如果未能解决你的问题,请参考以下文章

[Codeforces Round #522 (Div. 2, based on Technocup 2019 Elimination Round 3)][C. Playing Piano](代码片段

c_cpp Codeforces片段

Codeforces 86C Genetic engineering(AC自动机+DP)

CodeForces 1005D Polycarp and Div 3(思维贪心dp)

(Incomplete) Codeforces 394 (Div 2 only)

CodeForces 931F Teodor is not a liar!