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
| #include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #include<vector> using namespace std; #define MAXN 201116 #define pb push_back #define pii pair<int,int> #define mp make_pair int n , m; vector<int> G[MAXN]; int dep[MAXN] , par[MAXN] , dir[MAXN]; void dfs( int u , int fa ) { dep[u] = dep[fa] + 1 , par[u] = fa; for( int i = 0 ; i < G[u].size( ) ; ++ i ) { int v = G[u][i]; if( v == fa ) continue; if( !dep[v] ) dfs( v , u ) , dir[u] += dir[v]; else if( dep[v] < dep[u] ) ++ dir[u] , -- dir[ v ]; } } int l , r , L , R; int s; void dfs2( int u , int fa ) { for( int i = 0 ; i < G[u].size() ; ++ i ) { int v = G[u][i]; if( dep[v] == dep[u] + 1 ) dfs2( v , u ); else if( dep[v] < dep[s] && v != fa ) { if( l ) L = u , R = v; else l = u , r = v; } if( L ) return; } } vector<int> ans , tmp; void doit( int fr , int to ) { if( dep[fr] < dep[to] ) { tmp.clear(); to = par[to]; while( to != fr ) tmp.pb( to ) , to = par[to]; tmp.pb( fr ); for( auto it = tmp.rbegin() ; it != tmp.rend( ) ; ++ it ) ans.pb( *it ); return; } while( fr != to ) ans.pb( fr ) , fr = par[fr]; } int main() { cin >> n >> m; for( int i = 1 , u , v ; i <= m ; ++ i ) { scanf("%d%d",&u,&v); G[u].pb( v ) , G[v].pb( u ); } for( int i = 1 ; i <= n ; ++ i ) if( !dep[i] ) dfs( i , 0 ); for( int i = 1 ; i <= n ; ++ i ) if( dir[i] > 1 ) { s = i; break; } if( !s ) return puts("NO") , 0; puts("YES"); dfs2( s , par[s] ); if( dep[R] < dep[r] ) swap( r , R ) , swap( l , L ); int x = l , y = L; while( x != y ) dep[x] > dep[y] ? x = par[x] : y = par[y]; doit( x , l ) , ans.pb( l ) , doit( r , R ) , ans.pb( R ); printf("%d ",ans.size()); for( auto& x : ans ) printf("%d ",x); puts(""); ans.clear(); doit( x , L ) , ans.pb( L ) , ans.pb( R ); printf("%d ",ans.size()); for( auto& x : ans ) printf("%d ",x); puts(""); ans.clear(); doit( x , R ) , ans.pb( R ); printf("%d ",ans.size()); for( auto& x : ans ) printf("%d ",x); puts(""); }
|