判断欧拉回路是否存在

欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路。现给定一个图,问是否存在欧拉回路?
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在这里插入图片描述

#include<stdio.h>
#include<string.h>
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<stdio.h>
#include<string.h>
char s[100101];
int par[10010];
int in[1001],out[1001];
int book[1001],c[100];
int find(int root)
{
    
    
    if(root!=par[root])
        par[root]=find(par[root]);
    return par[root];
}
int merge(int t1,int t2)
{
    
    
    int r1,r2;
    r1=find(t1);
    r2=find(t2);
    if(r1!=r2)
        par[r1]=r2;
}
int main()
{
    
    
    int p;
    scanf("%d",&p);
    while(p--)
    {
    
    
        memset(book,0,sizeof(book));
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        memset(c,0,sizeof(c));
        int n,i,t,a,b,sum=0,k=0;
        for(i=0;i<26;i++)
            par[i]=i;
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
    
    
            scanf("%s",s);
            t=strlen(s);
            a=s[0]-'a';
            b=s[t-1]-'a';
            book[a]=1;
            book[b]=1;
            in[a]++;
            out[b]++;
            merge(a,b);
        }
        for(i=0;i<26;i++)
        {
    
    
            if(par[i]==i&&book[i]==1)
                sum++;
        }
        if(sum>1)
        {
    
    
            printf("The door cannot be opened.\n");
            continue;
        }
        for(i=0;i<26;i++)
        {
    
    
            if((in[i]!=out[i])&&book[i]==1)
            {
    
    
                c[k++]=i;
            }
        }
        if(k==0)
        {
    
    
            printf("Ordering is possible.\n");
            continue;
        }
        if(k==2&&((out[c[0]]-in[c[0]]==1&&in[c[1]]-out[c[1]]==1)||(out[c[1]]-in[c[1]]==1&&in[c[0]]-out[c[0]]==1)))
            printf("Ordering is possible.\n");
        else
            printf("The door cannot be opened.\n");
    }
}

猜你喜欢

转载自blog.csdn.net/m0_46312382/article/details/108137283