• 题意
    • 输入一张无向图,保证存在一条路径从1开始经过所有的边的正反方向各一次,最后返回1,输出任意一种路径方案
  • 题解
    • 将欧拉回路的vis去掉即可
  • 代码
    • #include<algorithm>
      #include <iostream>
      #include  <fstream>
      #include  <cstdlib>
      #include  <cstring>
      #include  <cassert>
      #include   <cstdio>
      #include   <vector>
      #include   <string>
      #include    <cmath>
      #include    <queue>
      #include    <stack>
      #include      <set>
      #include      <map>
      using namespace std;
      #define P(a,b,c) make_pair(a,make_pair(b,c))
      #define rep(i,a,n) for (int i=a;i<=n;i++)
      #define per(i,a,n) for (int i=n;i>=a;i--)
      #define CLR(vis) memset(vis,0,sizeof(vis))
      #define MST(vis,pos) memset(vis,pos,sizeof(vis))
      #define pb push_back
      //#define mp make_pair
      #define all(x) (x).begin(),(x).end()
      #define fi first
      #define se second
      #define SZ(x) ((int)(x).size())
      typedef pair<int,pair<int,int> >pii;
      typedef long long ll;
      typedef unsigned long long ull;
      const ll mod = 1000000007;
      const int INF = 0x3f3f3f3f;
      ll gcd(ll a, ll b) {
      	return b ? gcd(b, a%b) : a;
      }
      template<class T>inline void gmax(T &A, T B) {
      	(A<B)&&(A=B);//if(B>A)A=B;
      }
      template<class T>inline void gmin(T &A, T B) {
      	(A>B)&&(A=B);//if(B<A)A=B;
      }
      template <class T>
      inline bool scan(T &ret) {
      	char c;
      	int sgn;
      	if(c=getchar(),c==EOF) return 0; //EOF
      	while(c!='-'&&(c<'0'||c>'9')) c=getchar();
      	sgn=(c=='-')?-1:1;
      	ret=(c=='-')?0:(c-'0');
      	while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
      	ret*=sgn;
      	return 1;
      }
      inline void outt(int x) {
      	if(x>9) outt(x/10);
      	putchar(x%10+'0');
      }
      const int N = 100010,M = 1000010;
      int head[N], ver[M], Next[M], tot;
      int STA[M], ans[M];
      int n,m,top,t;
      
      
      void add(int x,int y){
      	ver[++tot] = y;
      	Next[tot] = head[x];
      	head[x] = tot;
      }
      
      
      void euler(){
      	STA[++top] = 1;
      	while(top>0){
      		int x = STA[top], i = head[x];
      		if(i){
      			STA[++top] = ver[i];
      			head[x] = Next[i];
      		}else{
      			--top;
      			ans[++t] = x;
      		}
      	}
      }
      
      int main(){
      	cin>>n>>m;
      	tot = 1;
      	top = 0;
      	memset(head, 0 ,sizeof(head));
      	rep(i,1,m){
      		int x,y;
      		scanf("%d%d", &x, &y);
      		add(x,y), add(y,x); 
      	}
      	euler();
      	per(i,1,t){
      		printf("%d\n", ans[i]);
      	}
      	return 0; 
      }

发表评论

邮箱地址不会被公开。 必填项已用*标注