Recursive Euler Road template. Current arc optimization.

#include <bits / STDC ++ H.>
 #define F. (I, A, B) for (int I = A; I <= B; I ++)
 #define LL Long Long
 #define PF (A) the printf ( "% D ", A)
 #define PHN the puts (" ")
 the using  namespace STD;
 int Read ();
 / * . * arc current optimization, otherwise the card into m ^ 2 when no fixed starting point can be selected easily, but there must sides he connected, not directly 1. * / 
#define N 400010 
 int T, n-, m;
 int Chu [N], Ru [N], du [N], to [N << . 1 ], FIR [N << . 1 ], ID [N << . 1 ], head [N], CNT;
 void the Add ( int X, int Y, int ID){to[++cnt]=y;fir[cnt]=head[x];head[x]=cnt;id[cnt]=ID;}
int sta[N<<1],vis[N<<1],top;
void dfs1(int x){
    for(int i=head[x];i;){
        while(i&&vis[i])i=fir[i];
        if(i){
            vis[i]=1;head[x]=fir[i];
            dfs1(to[i]);
            sta[++top]=id[i];
            i=head[x];
        }
    }
}    
void dfs2(int x){
    for(int i=head[x];i;){
        while(i&&vis[i]&&vis[i^1])i=fir[i];
        if(i){
            vis[i]=vis[i^1]=1;head[x]=fir[i];
            dfs2(to[i]);
            sta[++top]=id[i];
            i=head[x];
        }
    }
}
void WORK1 () { // do not exchange, the unidirectional 
    CNT = 0 ;
     for ( int I = . 1 , U, V; I <= m; ++ I) {
        u=read();v=read();
        add (u, v, i) ; chu ++ [u]; ++ ru [v];
    }
    int s=to[2],x=0,y=0;
    F(i,1,n){
        if(chu[i]-ru[i]==1)++x,s=i;
        else if(ru[i]-chu[i]==1)++y;
        else if(chu[i]!=ru[i]){
            puts("NO");exit(0);
        }
    }
    if((!x&&!y)||(x==1&&y==1));
    else {puts("NO"),exit(0);}
    dfs1(s);
    F(i,1,m){
        if(!vis[i]){puts("NO"),exit(0);}
    }
    puts("YES");
    for(int i=top;i>0;--i)pf(sta[i]);phn;
/*    memset(vis,0,sizeof(vis));
    for(int i=top;i>0;--i){
        s = to [i]; vis [i] = 1;
    }
    F(i,1,m)if(!vis[i])puts("WA");
    puts("Nicer");*/
}    
void WORK2 () { // exchange, bidirectional 
    CNT = . 1 ;
     for ( int I = . 1 , U, V; I <= m; ++ I) {
        u=read();v=read();
        add(u,v,i);add(v,u,-i);
        ++du[u];++du[v];
    }
    int s=to[2],x=0;
    F(i,1,n){
        if(du[i]&1)++x,s=i;
    }
    if(x==2||!x);
    else {puts("NO"),exit(0);}
    dfs2(s);
    F(i,1,m){
        if(!vis[i<<1]||!vis[i<<1|1]){puts("NO"),exit(0);}
    }
    puts("YES");
    for(int i=top;i>0;--i)pf(sta[i]);phn;    
}
int main () {
    freopen("merge.in","r",stdin);freopen("merge.out","w",stdout);
/** *///    freopen("ex_merge4.in","r",stdin);//freopen("merge.out","w",stdout);
    T=read();n=read();m=read();
    if(T==2)work1();
    else if(T==1)work2();
}
int read(){
    int s=0,f=0;char ch=getchar();
    while(!isdigit(ch))f=ch=='-',ch=getchar();
    while(isdigit(ch))s=s*10+(ch^48),ch=getchar();
    return f?-s:s;
}
/*
g++ dp.cpp
./a.out
g = c ++ ++ 11 merge.cpp -std 
./a.out
2 5 5
2 3
2 5
3 4
1 2
4 2
*/
Test 100T1

For this question: optimizing the current arc, or get stuck into m ^ 2 when there is no fixed starting point can pick, but there must be connected to the side of him, you can not directly use 1.

After the arc current is added n + m, can be snapped into or m ^ 2. (Two points directly connected edges m)

Guess you like

Origin www.cnblogs.com/seamtn/p/11794166.html
arc