/*
欧拉回路的应用;刘汝佳p112页
总感觉数据不是很全,有些情况不通过。
还有,漏了去判断图是否连通。并查集,详细看10129题
题意:一项链,每个小球有两种颜色,颜色相同的可连在一起,不小心项链断了,问是否还可以围成一个项链。并输出结果
*/
#include <cstdio>
#include <cstring>
const int nMax=60;
int graph[nMax][nMax],p[nMax];
int N;
void dfs(int u)
{
for(int i=1;i<=50;i++)
{
if(graph[u][i])
{
graph[u][i]--;
graph[i][u]--;
//printf("%d %d\n",u,i);
//为什么上面这种情况就不可以呢?
dfs(i);
printf("%d %d\n",i,u);
}
}
}
int main()
{
int T;
scanf("%d",&T);
for(int cas=1;cas<=T;cas++)
{
memset(graph,0,sizeof(graph));
memset(p,0,sizeof(p));
if(cas!=1)
printf("\n");
printf("Case #%d\n",cas);
scanf("%d",&N);
for(int i=0;i<N;i++)
{
int a,b;
scanf("%d %d",&a,&b);
graph[a][b]++;
graph[b][a]++;
p[a]++;
p[b]++;
}
int ok=1;
for(int i=0;i<=50;i++)
if(p[i]%2!=0)
{
ok=0;
break;
}
if(ok)
{
int u;
for(u=1;u<=50 && !p[u];u++);
dfs(u);
}
else
printf("some beads may be lost\n");
}
return 0;
}
10054 - The Necklace(***)
最新推荐文章于 2019-07-10 00:35:13 发布