题意:
思路:
Code:
#include<stdio.h> #include<string.h> bool solve(); void dfs(int n); int graph[51][51]; int du[51]; int path[1010*2]; int len; int main() { //freopen("10054.in","r",stdin); //freopen("10054.out","w",stdout); int t; scanf("%d",&t); int num=0; while(t-->0) { num++; memset(graph,0,sizeof(graph)); memset(du,0,sizeof(du)); len=0; int n; scanf("%d",&n); for(int i=0;i<n;++i) { int a,b; scanf("%d%d",&a,&b); du[a]++; du[b]++; graph[a][b]++;//增加一条无向边 //graph[b][a]=graph[a][b]; graph[b][a]++; } if(num!=1) printf("\n"); printf("Case #%d\n",num); int len=0; if(solve()==false) printf("some beads may be lost\n"); } return 0; } bool solve() { //判定顶点度数 for(int i=1;i<=50;++i) { if(du[i]%2==0) continue; else return false; }//printf("ok\n"); //判定连通性 int cnt=0;//连通块个数 for(int i=1;i<=50;i++) { if(du[i])//存在该颜色 { cnt++; dfs(i); } } if(cnt>1) return false; //print //printf("len:%d\n",len); for(int i=0;i<len-1;i=i+2) { printf("%d %d\n",path[i],path[i+1]); } //printf("%d %d\n",path[len-1],path[0]); return true; } void dfs(int n) { for(int i=1;i<=50;i++) { if(graph[n][i]) { graph[n][i]--; //graph[i][n]=graph[n][i]; graph[i][n]--; du[n]--; du[i]--; //path[len++]=n; dfs(i); path[len++]=i; path[len++]=n; } } }
时间: 2024-10-24 05:52:03