One stroke and one stroke

Source: Internet
Author: User

One stroke and one stroke
L example input: n, m in the first line, which has n vertices and m edges. The following m lines describe the two points connected by each edge. L 5 5l 1 2l 2 3l 3 4l 4 5l 5 1l sample output: Euler or Euler Loop l 1 5 4 3 2 1

 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 int map[1001][1001]; 5 int tj[10001]; 6 int vis[1001]; 7 int n,m; 8 int ans[1001]; 9 int now=1;10 int flag=1;11 void dfs(int i) {12     //cout<<p<<" ";13     for(int j=1; j<=n; j++) 14     {15         if(map[i][j]==1) 16         {17             map[j][i]=map[i][j]=0;18             dfs(j);19         }20     }21     ans[now]=i;22     now++;23 }24 int main() 25 {26 27     scanf("%d%d",&n,&m);28     for(int i=1; i<=m; i++) 29     {30         int x,y;31         scanf("%d%d",&x,&y);32         map[y][x]=map[x][y]=1;33         tj[x]++;34         tj[y]++;35     }36     flag=1;37     for(int i=1; i<=n; i++) 38         if(tj[i]%2==1) 39         {40             flag=i;41         }42     dfs(flag);43     for(int i=1; i<=now-1; i++)44     cout<<ans[i]<<" ";45     return 0;46 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.