欧拉图
来源:互联网 发布:谁绑架刘嘉玲 知乎 编辑:程序博客网 时间:2024/05/04 15:02
点击打开链接
Eular Graph
TimeLimit: 1 Second MemoryLimit: 32 Megabyte
Totalsubmit: 53 Accepted: 38
Description
Eular graph is a graph that you can start from one vertex X and go through all the edges only once and go back to vertex X, your task is to judge whether a given graph is a Eular graph or not.
A graph will be given like this:
Give n vertexes identified from 1 to n,and a set of edges described as i,j which means that there is a edge from vertex i to vertex j. Edges are directional.
Input
Input contains serveral test cases.First line of the input file is a integer t(1<=t<=100) presents the number of test cases.Each test case contains two parts. Part one contains two integer n and m. n is the number of vertexes and m is the number of edges. Part two contains m lines with two integers(i and j)1<=i,j<=100.
Output
For each test case,print yes if the graph is a Eular graph and no if not.
Sample Input
2
5 5
1 2
2 3
3 4
4 5
5 1
4 3
1 2
2 3
3 4
Sample Output
yes
no
#include<stdio.h>#include<string.h>#include<stdlib.h>int map[110][110],used[110][110],visit[110],vretex[110][110],n;void trace(int k){ int i; for(i=1;i<=n;i++){ if(i!=k && map[k][i] == 1 && visit[i] ==0){ visit[i] = 1; trace(i); } }}int main(){ int t,m,a,b,i,j; scanf("%d",&t); while(t--){ memset(map,0,sizeof(map)); memset(used,0,sizeof(used)); memset(vretex,0,sizeof(vretex)); memset(visit,0,sizeof(visit)); scanf("%d%d",&n,&m); for(i=0;i<m;i++){ scanf("%d%d",&a,&b); map[a][b] = 1; vretex[a][0] ++ ; vretex[b][1] ++; } visit[1] = 1; trace(1); for(i=1;i<=n;i++) if(visit[i] == 0) break; if(i>n){ for(j=1;j<=n;j++){ if(vretex[j][0] != vretex[j][1]) break; } if(j>n){ printf("yes\n"); continue; } } printf("no\n"); } return 0;}