UOJ #117. Euler loop,
#117. Euler Loop
Statistics
- Description
- Submit
- Custom Test
One day a soul painter drew a picture, and now you need to find out the Euler loop, that is, find a ring in the figure so that each side appears exactly on the ring.
There are two subtasks in total:
Input Format
The first line is an integer tt, indicating the subtask number. T ε {1, 2} t ε {1, 2}. If t = 1 t = 1, it indicates processing undirected graphs. If t = 2 t = 2, it indicates processing Directed Graphs.
The second row has two integers, n, mn, and m, indicating the number of knots and edges of the graph.
In the following line of mm, the two integers vi, uivi, and ui in line ii indicate the side of section ii (numbered from 11 ). Ensure that 1 is less than or equal to vi, ui is less than or equal to n1 is less than or equal to vi, and ui is less than or equal to n.
In the figure, there may be duplicate edges or auto rings.
Output Format
If one stroke is not allowed, output a line"NO".
Otherwise, output a line"YES", The next line outputs a set of solutions.
Example 1 input
13 31 22 31 3
Output
YES1 2 -3
Example 2 input
25 62 32 53 41 24 25 1
Output
YES4 1 3 5 2 6
Restrictions and conventions
1 ≤ n ≤ 1051 ≤ 0 ≤ m ≤ 2x105 ≤ n ≤ 0 ≤ m ≤ 2 x
Time Limit: 1s1s
Space limit: 256MB256MB
Download
When the graph is an undirected graph, the existence condition of the Euler loop is that the inbound degrees of all vertices are even.
When the graph is a directed graph, the existence condition of the Euler loop is that the inbound degrees of all points are equal to the outbound degrees.
Evaluate all dfs edges in Euler's loop
Save all edges during backtracking
Then flashback output
UOJ's data is really pitfall ,,,
In addition, GG () is written as GG, and QWQ is not reported. ,
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>using namespace std;const int MAXN=1e6+10;#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20+1,stdin),p1==p2)?EOF:*p1++)char buf[1<<20+1],*p1=buf,*p2=buf;inline int read(){ char c=getchar();int x=0,f=1; while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} return x*f;}int N,M,S;struct node{ int u,v,ID,nxt;}edge[MAXN];int head[MAXN],num=1;int inder[MAXN],ans[MAXN],vis[MAXN],tot=0;inline void AddEdge(int x,int y,int z){ edge[num].u=x; edge[num].v=y; edge[num].ID=z; edge[num].nxt=head[x]; head[x]=num++;}void GG(){printf("NO");exit(0);}int dfs(int now){ for(int i=head[now];i!=-1;i=edge[i].nxt) { if(!vis[abs(edge[i].ID)]) { head[now]=i; vis[abs(edge[i].ID)]=1,dfs(edge[i].v); ans[++tot]=edge[i].ID; i=head[now]; } }}int main(){ #ifdef WIN32 freopen("a.in","r",stdin); #else #endif memset(head,-1,sizeof(head)); int QWQ=read(); if(QWQ==1) { N=read();M=read(); for(int i=1;i<=M;i++) { int x=read(),y=read();S=x; AddEdge(x,y,i); AddEdge(y,x,-i); inder[x]++;inder[y]++; } for(int i=1;i<=N;i++) if(inder[i]&1) GG(); dfs(S); if(tot<M) GG(); puts("YES"); for(int i=M;i>=1;i--) printf("%d ",ans[i]); } else { N=read();M=read(); for(int i=1;i<=M;i++) { int x=read(),y=read();S=x; AddEdge(x,y,i); inder[y]--;inder[x]++; S=x; } for(int i=1;i<=N;i++) if(inder[i]!=0) GG(); dfs(S); if(tot<M) GG(); puts("YES"); for(int i=M;i>=1;i--) printf("%d ",ans[i]); } return 0;}