欧拉路径理解

欧拉路径:每个边仅通过一次,也叫一笔画问题
欧拉回路:闭合的欧拉路径,即一个环
判断欧拉路或欧拉回路是否存在

首先图一定要是连通图,判断连通性问题可以用dfs或者并查集实现

对于无向连通图

若图中所有点都是偶数点则存在欧拉回路,任意点为起点或终点,若存在两个奇点,则存在欧拉路,一个是起点,一个是终点

对于有向连通图

对于有向图,计算每个点的度,入度加一,出度减一,若所有点的度都为0,则存在欧拉回路,,如果存在一个点度为1,一个点度为-1,且其他点均为0,则存在欧拉路径

Play on Words HDU - 1116
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.
将每个字母的首尾看成有向图的边,题意需要满足:1,有向图连通,2,有两个奇数点或没有奇数点,3,度数最多相差1

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define mem(a,b)  memset(a,b,sizeof(a))
typedef long long ll;
const int N=100002;
int pre[N],vis[N],degree[N];
char s[N];
void init()
{
    for(int i=0;i<26;i++)
        pre[i]=i;
    mem(vis,0);
    mem(degree,0);
}
int find(int x)
{
    int fx=pre[x];
    if(x!=pre[x])
    {
        pre[x]=find(fx);
    }
    return pre[x];
}
void join(int x,int y)
{
    int fx=find(x);
    int fy=find(y);
    if(fx!=fy)
    {
        pre[fx]=fy;
    }
}
int main()
{
    int t,x,l,i,n;
    scanf("%d",&t);
    while(t--)
    {
        init();
        int a,b;
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%s",s);
            l=strlen(s);
            a=s[0]-'a';
            b=s[l-1]-'a';
            vis[a]=1;
            vis[b]=1;
            degree[a]--;
            degree[b]++;//printf("%d %d\n",a,b);
            join(a,b);
        }
        int cnt=0,sum=0,tot=0,ok=1;
        for(int i=0;i<26;i++)
        {//printf("%d %d\n",degree[i],vis[i]);
            if(vis[i]==0)
                continue;
            if(abs(degree[i])==1)
                cnt++;
            if(pre[i]==i)
                tot++;
            if(abs(degree[i])>=2)  ok=0;
        }
       // printf("%d %d %d \n",cnt,tot,ok);
        if(ok&&tot==1&&(cnt==0||cnt==2))
            printf("Ordering is possible.\n");
        else
            printf("The door cannot be opened.\n");
    }
    return 0;

}

C - Door Man POJ - 1300
Problem Description
You are a butler in a large mansion. This mansion has so many rooms that they are merely referred to by number (room 0, 1, 2, 3, etc…). Your master is a particularly absent-minded lout and continually leaves doors open throughout a particular floor of the house. Over the years, you have mastered the art of traveling in a single path through the sloppy rooms and closing the doors behind you. Your biggest problem is determining whether it is possible to find a path through the sloppy rooms where you:

Always shut open doors behind you immediately after passing through
Never open a closed door
End up in your chambers (room 0) with all doors closed

In this problem, you are given a list of rooms and open doors between them (along with a starting room). It is not needed to determine a route, only if one is possible.

Input
Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the following description, and there will be no blank lines separating data sets.
A single data set has 3 components:

Start line - A single line, “START M N”, where M indicates the butler’s starting room, and N indicates the number of rooms in the house (1 <= N <= 20).
Room list - A series of N lines. Each line lists, for a single room, every open door that leads to a room of higher number. For example, if room 3 had open doors to rooms 1, 5, and 7, the line for room 3 would read “5 7”. The first line in the list represents room 0. The second line represents room 1, and so on until the last line, which represents room (N - 1). It is possible for lines to be empty (in particular, the last line will always be empty since it is the highest numbered room). On each line, the adjacent rooms are always listed in ascending order. It is possible for rooms to be connected by multiple doors!
End line - A single line, “END”

Following the final data set will be a single line, “ENDOFINPUT”.

Note that there will be no more than 100 doors in any single data set.

Output
For each data set, there will be exactly one line of output. If it is possible for the butler (by following the rules in the introduction) to walk into his chambers and close the final open door behind him, print a line “YES X”, where X is the number of doors he closed. Otherwise, print “NO”.

Sample Input
START 1 2
1

END
START 0 5
1 2 2 3 3 4 4

END
START 0 10
1 9
2
3
4
5
6
7
8
9

END
ENDOFINPUT

Sample Output
YES 1
NO
YES 10
题意:给出 n 个房间以及房间之间的门,再给出初始房间 m,要求从 m 开始走,能否每个房间只经过一次,而且最后到达 0 号房间,若可以则输出 YES 以及要走的房间数

求一个图中是否具有欧拉回路或欧拉通路

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int main()
{
	char str[128];
	int door[25],m,n;
	while(~scanf("%s",str))
	{
		if(str[0]=='S')
		{
			scanf("%d %d",&m,&n);
			getchar();
			for(int i=0;i<n;i++)
			door[i]=0;
			int doors=0;
			for(int i=0;i<n;i++)
			{
				gets(str);
				int k=0,j;
				while(sscanf(str+k,"%d",&j)==1)
				{
					doors++;
					door[i]++;
					door[j]++;
					if(str[k]&&str[k]==' ') k++;
					while(str[k]&&str[k]!=' ') k++;
				}

			}
			gets(str);
			int odd=0,even=0;
			for(int i=0;i<n;i++)
			{
				if(door[i]%2)
				odd++;
				else
				even++;
			}
			if(odd==0&&m==0)//欧拉回路
			printf("YES %d\n",doors);
			else if(odd==2&&door[m]%2&&door[0]%2&&m!=0)//欧拉通路
			printf("YES %d\n",doors);
			else
			printf("NO\n",doors);
		}
		else
		break;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值