UVA10054The Necklace

来源:互联网 发布:s5700 telnet 默认端口 编辑:程序博客网 时间:2024/05/18 00:51

UVA-10054

题意:给出若干个珠子,每个珠子两端都有颜色,两个珠子相同颜色的一端可以接在一起,问给出的能不能串成一个项链。
解题思路:能不能串成一串,明显又是欧拉回路。所有点的度都要为偶数。是欧拉回路的条件下,DFS遍历输出。
额,要注意点,点的坐标是1-50,,不是1-n。

/*************************************************************************    > File Name: UVA-10054.cpp    > Author: Narsh    >     > Created Time: 2016年07月22日 星期五 10时24分23秒 ************************************************************************/#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>using namespace std;int map[1100][1100],d[1010],n,m,t,num=0;void dfs(int x) {    for (int i = 1; i <= 50; i++)         if (map[x][i]) {            d[x]--;            d[i]--;            map[x][i]--;            map[i][x]--;            dfs(i);            printf("%d %d\n",i,x);        }}int main() {    //freopen("xx.in","r",stdin);    scanf("%d",&t);    while (t--) {        int a,b;        scanf("%d",&n);        memset(map,0,sizeof(map));        memset(d,0,sizeof(d));        for (int i = 1; i <= n; i++) {            scanf("%d%d",&a,&b);            map[a][b]++;            map[b][a]++;            d[a]++;d[b]++;        }        int f=1;        for (int i = 1; i <= 50; i++)             if (d[i] % 2 != 0) {                f=0;                break;            }        printf("Case #%d\n",++num);        if (!f) printf("some beads may be lost\n");        else {            for (int i = 1; i <= 50; i++)                 if (d[i]) dfs(i);        }        if (t) printf("\n");    }}
0 0
原创粉丝点击