标签:tac \n eof vector names print clu space sort
80分,暂时没找出20分的Bug
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<algorithm> #include<queue> #include<map> #include<stack> using namespace std; int n,m; const int maxn=1e4+2; const int maxm=1e5+2; vector<int> g[maxn]; bool vis[maxn][maxn]; int fa[maxn]; stack<int> path; void init(){ for(int i=1;i<=n;i++){ g[i].clear(); } while(!path.empty()) path.pop(); memset(vis,false,sizeof(vis)); } int find(int x){ return x==fa[x]?x:fa[x]=find(fa[x]); } void merge(int u,int v){ int fu=find(u); int fv=find(v); if(fu!=fv){ fa[fu]=fv; } } bool judge(){ for(int i=1;i<=n;i++) fa[i]=i; for(int u=1;u<=n;u++){ int sz=g[u].size(); for(int i=0;i<sz;i++){ int v=g[u][i]; merge(u,v); } } int cnt=0; for(int i=1;i<=n;i++){ if(fa[i]==i) cnt++; } if(cnt==1) return true; return false; } void dfs(int u){ int sz=g[u].size(); for(int i=0;i<sz;i++){ int v=g[u][i]; if(vis[u][v]) continue; vis[u][v]=vis[v][u]=true; dfs(v); } path.push(u); } void eular(){ dfs(1); printf("%d",path.top()); path.pop(); while(!path.empty()){ printf(" %d",path.top()); path.pop(); } puts(""); } int main(){ while(~scanf("%d%d",&n,&m)){ init(); int u,v; for(int i=1;i<=m;i++){ scanf("%d%d",&u,&v); g[u].push_back(v); g[v].push_back(u); } if(!judge()){ printf("-1\n"); continue; } int cnt=0; for(int i=1;i<=n;i++){ int sz=g[i].size(); if(sz%2){ cnt++; } } if(cnt!=0&&cnt!=2){ printf("-1\n"); continue; } if(cnt==2){ int sz=g[1].size(); if(sz%2==0){ printf("-1\n"); continue; } } for(int i=1;i<=n;i++){ sort(g[i].begin(),g[i].end()); } eular(); } return 0; }
标签:tac \n eof vector names print clu space sort
原文地址:https://www.cnblogs.com/itcsl/p/9192951.html