danlao @ 2024-04-30 17:49:53
记录
#include <bits/stdc++.h>
using namespace std;
#define hh putchar('\n')
#define kg putchar(' ')
#define debug puts("debug")
//#define int long long
//#define int __int128
namespace quickread{
inline int read(){
int x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x*f;
}
inline void write(int x){
if(x<0){putchar('-');x=-x;}
if(x>9)write(x/10);
putchar(x%10+'0');
}
inline string readstr(){
char ch=getchar();string str="";
while(!(ch>='0'&&ch<='9')&&!(ch>='a'&&ch<='z')&&!(ch>='A'&&ch<='Z')) ch=getchar();
while((ch>='0'&&ch<='9')||(ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')){str+=ch;ch=getchar();}
return str;
}
inline char readchar(){
char ch=getchar();
while(!(ch>='0'&&ch<='9')&&!(ch>='a'&&ch<='z')&&!(ch>='A'&&ch<='Z')) ch=getchar();
return ch;
}
}
using namespace quickread;
int n,m,bg,top,st[100010];
vector<int> p[510];
vector<int>::iterator it[100010];
bool vis[510][510];
void hierholzers(){
for(int i=1;i<=n;i++){
int d=p[i].size();
if(d%2==1) {bg=i;break;}
}
for(int i=1;i<=n;i++){
sort(p[i].begin(),p[i].end());
it[i]=p[i].begin();
}
if(!bg){
bg=510;
for(int i=1;i<=n;i++)
if(p[i].size()&&bg>i) bg=i;
}
}
void dfs(int u){
while(it[u]!=p[u].end()){
int v=*it[u];it[u]++;
if(vis[u][v]) continue;
vis[u][v]=vis[v][u]=1;
dfs(v);
}
st[++top]=u;
}
signed main(){
// ios::sync_with_stdio(0);
// cin.tie(0);cout.tie(0);
m=read();
for(int i=1;i<=m;i++){
int u=read(),v=read();
p[u].push_back(v);
p[v].push_back(u);
n=max(n,max(u,v));
}
hierholzers();
dfs(bg);
for(int i=top;i>=1;i--) write(st[i]),hh;;
return 0;
}
by fengzhaoyu @ 2024-05-01 15:09:47
考虑重边
#include<bits/stdc++.h>
using namespace std ;
int g[10005][10005],du[100004];
int m,n;
int ans[100005],tot;
void dfs(int u)
{
for(int i=1;i<=n;i++)
{
if(g[u][i]>0)
{
g[u][i]--;
g[i][u]--;
dfs(i);
}
}
ans[++tot]=u;
}
int main()
{
cin>>m;
for(int i=1;i<=m;i++)
{
int u,v;
cin>>v>>u;
g[v][u]++;g[u][v]++;
du[v]++;
du[u]++;
n=max({v,u,n});
}
int st=1;
for(int i=1;i<=n;i++)
{
if(du[i]%2==1)
{
st=i;break;
}
}
dfs(st);
for(int i=tot;i>=1;i--)
{
cout<<ans[i]<<endl;
}
return 0;
}