UVa 10054 (打印欧拉回路) The Necklace

时间:2022-12-25 16:19:05

将每个颜色看成一个顶点,对于每个珠子在两个颜色之间连一条无向边,然后求欧拉回路。

 #include <cstdio>
#include <cstring> const int maxn = + ;
int G[maxn][maxn], deg[maxn]; void Euler(int u)
{
for(int v = ; v <= ; v++) if(G[u][v])
{
G[u][v]--; G[v][u]--;
Euler(v);
printf("%d %d\n", v, u);
}
} int main()
{
//freopen("in.txt", "r", stdin); int T; scanf("%d", &T);
for(int kase = ; kase <= T; kase++)
{
memset(G, , sizeof(G));
memset(deg, , sizeof(deg)); if(kase > ) puts("");
printf("Case #%d\n", kase); int n, a, b, st;
scanf("%d", &n);
for(int i = ; i < n; i++)
{
scanf("%d%d", &a, &b);
st = a;
deg[a]++; deg[b]++;
G[a][b]++; G[b][a]++;
} bool ok = true;
for(int i = ; i <= ; i++) if(deg[i] % ) { ok = false; break; }
if(!ok) { puts("some beads may be lost"); continue; } Euler(st); } return ;
}

代码君