UVa10054 The Necklace,无向图求欧拉回路

2015-07-20 17:40:51 · 作者: · 浏览: 3

无向图求欧拉回路:

1、图连通

2、所有顶点的度数位偶数


随便从一个点开始递归遍历即可求出路径


#include 
  
   
#include 
   
     #include 
    
      using namespace std; const int maxcolor = 50; int n, G[maxcolor+1][maxcolor+1], deg[maxcolor+1]; struct Edge{ int from, to; Edge(int from, int to):from(from), to(to){ } }; vector
     
       ans; void euler(int u){ for(int v=1; v<=maxcolor; v++) if(G[u][v]){ G[u][v]--; G[v][u]--; euler(v); ans.push_back(Edge(u, v)); } } int main(){ int T; scanf("%d", &T); for(int cas=1; cas<=T; ++cas){ scanf("%d", &n); memset(G, 0, sizeof G ); memset(deg, 0, sizeof deg ); int start = -1; for(int i=0; i
      
       =0; i--) printf("%d %d\n", ans[i].from, ans[i].to); if(cas