UVA 10054 The Necklace 欧拉回路

来源:互联网 发布:深圳奇迹智慧网络 编辑:程序博客网 时间:2024/05/04 01:01

The input contains T test cases. The first line of the input contains the integer T.

The first line of each test case contains an integer N ( $5 \leN \le 1000$) giving the number of beads my sister was able to collect. Each of the next N lines contains two integers describing the colors of a bead. Colors are represented by integers ranging from 1 to 50.

Output 

For each test case in the input first output the test case number as shown in the sample output. Then if you apprehend that some beads may be lost just print the sentence ``some beads may be lost" on a line by itself. Otherwise, print N lines with a single bead description on each line. Each bead description consists of two integers giving the colors of its two ends. For $1 \le i \le N ­ 1$, the second integer on line i must be the same as the first integer on line i + 1. Additionally, the second integer on line N must be equal to the first integer on line 1. Since there are many solutions, any one of them is acceptable.

Print a blank line between two successive test cases.

Sample Input 

251 22 33 44 55 652 12 23 43 12 4

Sample Output 

Case #1some beads may be lost Case #22 11 33 44 22 2

卡了我20多发,一直以为是方法错了,中间还有几次RE,卧槽,不能再爱了,最后发现maxn开到55,最大为54。。
还有就是没好好读题,输出有两个要求,就要递归输出。由于是环,可以随便找个点做起点,但是输出有要求的。
最后才发现。

有人总结的好:点击打开链接

判断欧拉路是否存在的方法

有向图:图连通,有一个顶点出度大入度1,有一个顶点入度大出度1,其余都是出度=入度。

无向图:图连通,只有两个顶点是奇数度,其余都是偶数度的。

判断欧拉回路是否存在的方法

有向图:图连通,所有的顶点出度=入度。

无向图:图连通,所有顶点都是偶数度。

对于此题无向图的欧拉回路来说就是判断所有的点都是偶数度,但是这还不够,要用并查集判断一下连通性。

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<limits.h>using namespace std;const int maxn=55;int d[maxn],mp[maxn][maxn];int pre[maxn];int n;void init(){    memset(mp,0,sizeof(mp));    memset(d,0,sizeof(d));    for(int i=0;i<maxn;i++)        pre[i]=i;}int find_root(int x){    if(x!=pre[x])        pre[x]=find_root(pre[x]);    return pre[x];}bool ok(){    int x=-1;    for(int i=0;i<maxn;i++)    {        if(d[i]&1)            return false;;        if(d[i])        {            if(x==-1)                x=find_root(i);            else            {                if(x!=find_root(i))                    return false;            }        }    }    return true;}void dfs(int x)//dfs搜索下路径输出{    for(int i=1;i<maxn;i++)    {        if(mp[x][i])        {            mp[x][i]--;//特别注意可能不止多条            mp[i][x]--;            dfs(i);            printf("%d %d\n",i,x);        }    }}int main(){    int x,y;    int l=0;    int ss,t;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        init();        for(int i=0;i<n;i++)        {            scanf("%d%d",&x,&y);            ss=x;            d[x]++;            d[y]++;            mp[x][y]++;            mp[y][x]++;            pre[find_root(x)]=find_root(y);        }        if(l)            printf("\n");        printf("Case #%d\n",++l);        if(!ok())            printf("some beads may be lost\n");        else            dfs(ss);    }    return 0;}



1 0
原创粉丝点击