判断欧拉回路是否存在

欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路。现给定一个图,问是否存在欧拉回路?
Input
测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是节点数N ( 1 < N < 1000 )和边数M;随后的M行对应M条边,每行给出一对正整数,分别是该条边直接连通的两个节点的编号(节点从1到N编号)。当N为0时输入结
束。
Output
每个测试用例的输出占一行,若欧拉回路存在则输出1,否则输出0。
Sample Input
3 3
1 2
1 3
2 3
3 2
1 2
2 3
0
Sample Output
1
0判断欧拉回路是否存在_第1张图片

#include
#include
int a[1001],n,m;
int b[1001];
int par[1001];
int find(int root)
{
    if(root!=par[root])
        par[root]=find(par[root]);
    return par[root];
}
int main()
{
    while(~scanf("%d",&n)&&n!=0)
    {
        memset(a,0,sizeof(a));
        int i,t=0,flog=0,t1,t2,r1,r2;
        for(i=1; i<n; i++)
            par[i]=i;
        scanf("%d",&m);
        for(i=0; i<m; i++)
        {
            scanf("%d %d",&t1,&t2);
            if(par[t1]!=par[t2])
            {
                r1=find(par[t1]);
                r2=find(par[t2]);
                par[r1]=r2;
            }
            a[t1]++;
            a[t2]++;
        }
        for(i=1; i<=n; i++)
        {
            if(find(i)!=find(1))
            {
                flog=0;
                break;
            }
            if(a[i]%2==0)
                flog++;
        }
        if(flog==n)
            flog=1;
        else
            flog=0;
        printf("%d\n",flog);
    }
}

Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve it to open that doors. Because there is no other way to open the doors, the puzzle is very important for us.

There is a large number of magnetic plates on every door. Every plate has one word written on it. The plates must be arranged into a sequence in such a way that every word begins with the same letter as the previous word ends. For example, the word acm'' can be followed by the wordmotorola’’. Your task is to write a computer program that will read the list of words and determine whether it is possible to arrange all of the plates in a sequence (according to the given rule) and consequently to open the door.
Input
The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing a single integer number Nthat indicates the number of plates (1 <= N <= 100000). Then exactly Nlines follow, each containing a single word. Each word contains at least two and at most 1000 lowercase characters, that means only letters ‘a’ through ‘z’ will appear in the word. The same word may appear several times in the list.
Output
Your program has to determine whether it is possible to arrange all the plates in a sequence such that the first letter of each word is equal to the last letter of the previous word. All the plates from the list must be used, each exactly once. The words mentioned several times must be used that number of times.
If there exists such an ordering of plates, your program should print the sentence “Ordering is possible.”. Otherwise, output the sentence “The door cannot be opened.”.
Sample Input
3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok
Sample Output
The door cannot be opened.
Ordering is possible.
The door cannot be opened.

#include
#include
#include
char s[100101];
int in[30],out[30];
int par[100];
int find(int root)
{
    if(par[root]!=root)
        par[root]=find(par[root]);
    return par[root];
}
int main()
{
    int p;
    scanf("%d",&p);
    int i,j;
    while(p--)
    {
        for(i=1; i<=30; i++)
            par[i]=i;
        int n,ans=0,sum1=0,sum2=0;
        scanf("%d",&n);
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        while(n--)
        {
            scanf("%s",s);
            int t=strlen(s);
            int a=s[0]-'a'+1;
            int b=s[t-1]-'a'+1;
            in[a]++;
            out[b]++;
            par[find(a)]=find(b);
        }
        for(i=1; i<=26; i++)
            if((out[i])&&find(i)==i||abs(in[i]-out[i])>1)
            //判断点是否是起点且在图中 //出度与入度最大差值不能超过1
                ans++;
        if(ans>1)
        {
            printf("The door cannot be opened.\n");
            continue;
        }
        for(i=1; i<=26; i++)
        {
            if(in[i]>out[i])
                sum1++;
            if(in[i]<out[i])
                sum2++;
        }
        //要么是欧拉路起点和终点度相差为1 //或者是欧拉回路所有的的出度与入度都一样
        if(sum1==1&&sum2==1||sum1==0&&sum2==0)
            printf("Ordering is possible.\n");
        else
            printf("The door cannot be opened.\n");
    }
}

你可能感兴趣的:(判断欧拉回路是否存在)