欧拉回路(通路):通过图(无向图或有向图)中所有边一次且仅一次行遍图中所有顶点的
欧拉图和半欧拉图:具有欧拉回路的图称为欧拉图,具有欧拉通路而无欧拉回路的
无向图:
无向图G是欧拉图当且仅当G是连通图,且G中没有奇度顶点。(可以从任一点出发,最终一定会回到该点)
无向图G是半欧拉图当且仅当G是连通图,且G中恰有两个奇度顶点。(从其中一个奇点出发,另一个奇点终止)
有向图:
有向图D是欧拉图当且仅当D是强连通的且每个顶点的入度都等于出度。(可以从任一点出发,最终一定会回到该点)
有向图D是半欧拉图当且仅当D是单向连通的,且D中恰有两个奇度顶点,其中一个的出度比入度大1(作为起点),另一个入度比出度大1(作为终点),而其余顶点的入度都等于出度。
一笔画问题
时间限制:
3000 ms | 内存限制:
65535 KB
难度:
4
-
描述
-
zyc从小就比较喜欢玩一些小游戏,其中就包括画一笔画,他想请你帮他写一个程序,判断一个图是否能够用一笔画下来。
规定,所有的边都只能画一次,不能重复画。
-
输入
-
第一行只有一个正整数N(N<=10)表示测试数据的组数。
每组测试数据的第一行有两个正整数P,Q(P<=1000,Q<=2000),分别表示这个画中有多少个顶点和多少条连线。(点的编号从1到P)
随后的Q行,每行有两个正整数A,B(0<A,B<P),表示编号为A和B的两点之间有连线。
输出
-
如果存在符合条件的连线,则输出"Yes",
如果不存在符合条件的连线,输出"No"。
样例输入
-
2 4 3 1 2 1 3 1 4 4 5 1 2 2 3 1 3 1 4 3 4
样例输出
-
No Yes
根据无向图的定义先用并查集或dfs判断图是否联通,再判断奇点个数 -
第一行只有一个正整数N(N<=10)表示测试数据的组数。
#include<stdio.h>
#include<string.h>
#define MAX 1005
int du[MAX],set[MAX];
int find(int x)
{
if(x!=set[x])
set[x]=find(set[x]);
return set[x];
}
void merge(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
set[fy]=fx;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int p,q,need=0,flag=1,u,v;
memset(du,0,sizeof(du));
scanf("%d%d",&p,&q);
for(int i=1; i<=p; i++)
{
set[i]=i;
}
while(q--)
{
scanf("%d%d",&u,&v);
merge(u,v);
du[u]++;//无向图两端度都记录
du[v]++;
}
for(int i=1; i<=p; i++)
{
if(set[i]==i)
{
need++;
}
if(need>1)//判断是否为连通图
{
flag=0;
break;
}
}
if(flag)
{
int k=0;
for(int i=1; i<=p; i++)
{
if(du[i]%2==1)
k++;
if(k>2)//判断奇点个数
{
flag=0;
break;
}
}
}
if(flag)
printf("Yes\n");
else
printf("No\n");
}
}
poj1386 有向图的判定
Play on Words
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 12079 | Accepted: 4119 |
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.
题意:输入n (1 <= n <= 100000)个单词,判断是否能把这些单词排成一个序列,使每个单词的第一个字母和上一个单词的最后一个字母相同
分析:把首尾字母看成节点,单词看成有向边,判断是否是欧拉路径。
输入较大用cin会超时。
#include<stdio.h> #include<iostream> #include<string.h> using namespace std; int set[30],in[30],out[30]; char s[1005]; int find(int x) { if(x!=set[x]) set[x]=find(set[x]); return set[x]; } void merge(int x,int y) { int fx=find(x); int fy=find(y); if(fx!=fy) { set[fy]=fx; } } int main() { int T; scanf("%d",&T); while(T--) { int n; scanf("%d",&n); memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); for(int i=0; i<26; i++) { set[i]=i; } for(int i=0; i<n; i++) { scanf("%s",s); int x,y; x=s[0]-'a'; y=s[strlen(s)-1]-'a'; in[x]++; out[y]++; merge(x,y); } int need=0,flag=1; for(int i=0; i<26; i++) { if((in[i]||out[i])&&set[i]==i) { need++; } if(need>1) //判断是否连通 { flag=0; break; } } int k=0,k1=0,k2=0; if(flag) { for(int i=0; i<26; i++) { if(in[i]==out[i]) { k++; } else if(in[i]-out[i]==1) { k1++; } else if(out[i]-in[i]==1) { k2++; } } } if(flag&&((k==24&&k1==1&&k2==1)||k==26)) { printf("Ordering is possible.\n"); } else { printf("The door cannot be opened.\n"); } } }