通过图中所有边1次且仅1次行遍所有顶点的通路称作欧拉通路。
通过图中所有边1次且仅1次行遍所有顶点的回路称作欧拉回路。
具有欧拉回路的图称为欧拉图。
具有欧拉通路而无欧拉回路的图成为半欧拉图。
无向图:
G是欧拉图当且仅当G是是连通图且没有奇度顶点
G是半欧拉图当且仅当G是连通图恰有2个奇度顶点
有向图:(求有向图是否连通只需要把有向图看作无向图来处理连通性即可)
D是欧拉图当且仅当D是连通且每个顶点的入度等于出度。
D是半欧拉图当且仅当D是连通的且恰有2个奇度顶点,其中一个顶点的入度比出度大1,另一个顶点的出度比入度大1,其余顶点的入度等于出度。
欧拉回路判断模板:
vector<int> g[1010];
bool visited[1010];
int n;
void dfs(int u)
{
visited[u]=1;
for(int i=0;i<g[u].size();i++)
if(!visited[g[u][i]])
dfs(g[u][i]);
}
int main()
{
while(cin>>n)
{
int m;
cin>>m;
memset(visited,0,sizeof(visited));
for(int i=0;i<=1000;i++)
g[i].clear();
for(int i=0;i<=m-1;i++)
{
int a,b;
scanf("%d%d",&a,&b);
g[a].push_back(b);
g[b].push_back(a);
}
int cnt=0;
for(int i=1;i<=n;i++)
{
if(!visited[i])
{
cnt++;
if(cnt>1)
break;
dfs(i);
}
}
bool flag=0;
for(int i=1;i<=n;i++)
if(g[i].size()%2==1)
{
flag=1;
break;
}
if(cnt==1&&flag==0)//
cout<<1<<endl;
else
cout<<0<<endl;
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.
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.".
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<iostream>
#include<cstring>
#include<vector>
#include<cstdio>
#include<cstring>
#include<string>
#include<stack>
using namespace std;
const int MAX=35;
int in[MAX],out[MAX],inout[MAX];
bool visited[MAX];
vector<int> g[MAX];
bool num[MAX];
int n;
void dfs(int u)
{
visited[u]=1;
for(int i=0;i<g[u].size();i++)
if(!visited[g[u][i]])
dfs(g[u][i]);
}
int main()
{
int T;
cin>>T;
while(T--)
{
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
memset(visited,0,sizeof(visited));
memset(num,0,sizeof(num));
for(int i=0;i<=30;i++)
g[i].clear();
cin>>n;
string str;
for(int i=0;i<=n-1;i++)
{
cin>>str;
int l=str.length();
int a=str[0]-'a';
int b=str[l-1]-'a';
num[a]=1;num[b]=1;
// cout<<a<<" "<<b<<endl;
g[a].push_back(b);
g[b].push_back(a);
out[a]++;
in[b]++;
}
int cnt=0;
for(int i=0;i<=25;i++)
if(num[i]&&!visited[i])
{
cnt++;
if(cnt>1)
break;
dfs(i);
}
if(cnt>1)
{
cout<<"The door cannot be opened."<<endl;
continue;
}
for(int i=0;i<=25;i++)
inout[i]=in[i]-out[i];
bool flag=0;
int cnt0=0,cnt1=0,cntx1=0;
for(int i=0;i<=25;i++)
if(!inout[i])
cnt0++;
else if(inout[i]==1)
cnt1++;
else if(inout[i]==-1)
cntx1++;
if(cnt0==26||cnt0==24&&cnt1==1&&cntx1==1)
cout<<"Ordering is possible."<<endl;
else
cout<<"The door cannot be opened."<<endl;
}
return 0;
}