欧拉回路以及欧拉路径

来源:互联网 发布:cf宏鼠标一键瞬狙数据 编辑:程序博客网 时间:2024/04/28 12:43

欧拉回路是指在一个图中存在一个回路使得从一点出发又回到该点,并且不重复地走完图中所有的边。

解决方法:

1,运用并查集判断所有点是否构成一个图

2,判断点的度(如果是有向图,所有点的入度等于出度,如果是无向图,所有点的度都为偶数)

原理:如果要实现欧拉回路,那么对于有向图来说,通过一条边进入某点,肯定存在一条没走过边离开该点,所以入度自然等于出度,对于无向图,每通过一条边进入某点也必然存在一条没走过的边出去,因为无向图的边是完全相同的,所以每个点的度肯定是偶数。

欧拉路径是指在一个图中存在一条路径使得从一点出发到达另一点,并且不重复地走完图中所有的边。

解决方法:

1,同样使用并查集判断所有点是否构成一个图

2,判断点的度(如果是有向图,只有一个点出度比入度大1,也只有一个点入度比出度大1,其余的点入度等于出度,如果是无向图,只有两个点的度是奇数,其余均为偶数)

原理:如果要实现欧拉路径,只存在一个起点和一个终点,对于起点,出去的边比进来的边多1,终点进来的边比出去的边多1,对于有向图来说,起点就是出度比入度大1的点,而终点就是入度比出度大1的点,对于无向图,起点和终点的度为基数

理解了欧拉路径后就不难发现,终点和起点是成对出现的,那么通过图中没点的度就可以判断该图最少可以分成几个欧拉路径的组合,例如:无向图中存在4个奇数度的点,那么最少分成两个欧拉路径(思考一下,无论怎样都不会出现奇数个奇数度的点);有向图中出度比入度大1的点有4个,那么最少分成4个欧拉路径(入度比出度大1的点也一定是4个)


在欧拉路径题目中,有时会包含欧拉回路的情况,要想清楚。


在欧拉路径,欧拉回路的题目中还要注意以什么为边,以什么为点

上一道题:

Description

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 word ``motorola''. 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

32acmibm3acmmalformmouse2okok
 

Sample Output

The door cannot be opened.Ordering is possible.The door cannot be opened.
题目大意是判断能否实现词语接龙,在该题中,应该把26个字母看成点,每个单词看成边,而且是从首字母指向尾字母的有向边

具体代码:

#include<stdio.h>#include<string.h>int father[26],r[26];int inde[26],outde[26];//记录出入度char s[1005];void make_set(){    memset(inde,0,sizeof(inde));    memset(outde,0,sizeof(outde));    for(int i=0;i<26;i++)    {        father[i]=i;        r[i]=1;    }}int find_set(int a){    if(a!=father[a])        father[a]=find_set(father[a]);    return father[a];}void Union(int a,int b){    a=find_set(a);    b=find_set(b);    if(a==b)        return ;    if(r[a]>r[b])    {        father[b]=a;        r[a]+=r[b];    }    else    {        father[a]=b;        r[b]+=r[a];    }    return ;}int f;int main(){    int n,t;    scanf("%d",&t);    while(t--)    {        make_set();        scanf("%d",&n);        for(int i=0;i<n;i++)        {            scanf("%s",s);            inde[s[0]-'a']++;            outde[s[strlen(s)-1]-'a']++;            Union((int)(s[0]-'a'),(int)(s[strlen(s)-1]-'a'));//建立并查集        }        int in=0,out=0;        bool flag=true;        int count=0;        for(int i=0;i<26;i++)//判断是否在同一集合        {            if(count==0&&(inde[i]||outde[i]))            {                f=find_set(i);                count=1;            }            if(count!=0&&(inde[i]||outde[i])&&find_set(i)!=f)            {                flag=false;                break;            }        }        if(!flag)        {            printf("The door cannot be opened.\n");            continue;        }        for(int i=0;i<26;i++)//出入度判断        {            if(inde[i]-outde[i]==1)                in++;            else if(outde[i]-inde[i]==1)                out++;            else if(inde[i]-outde[i]>1||outde[i]-inde[i]>1)            {                flag=false;                break;            }            if(in>1||out>1)            {                flag=false;                break;            }        }        if(flag)            printf("Ordering is possible.\n");        else            printf("The door cannot be opened.\n");    }    return 0;}





0 0