上海交大-“Code人心弦”9月网络赛-OddGraph

1098. OddGraph

Description

Pangzi looks somehow odd recently. He started to love odd things, like odd numbers and odd graphs. He says, an odd graph is a non-empty graph (that is the set of vertices is non-empty) that every vertex has an odd degree (that is, connected to an odd number of edges). If a graph is not odd, he will try to find a subgraph that is odd. In Pangzi's mind, a subgraph of a graph G=(V, E) is composed by some vertices in G and all edges between these vertices from G. Mathematically, G'=(V', E') where V' is a subset of V and E'={(u, v): u, v in V' and (u, v) in E}.

Now Pangzi will give you a graph, please tell him if this graph is odd. If it is not odd, try to find an odd subgraph.

Pay attention. As Pangzi is odd now, when he talks about graphs, he is actually talking about undirected graphs.

Input Format

The first line of the input is an integer T (T <= 100), indicating the number of test cases.

Then, T test cases follow. For every test case, the first line is 2 integers n, m (1 <= n <= 100, 0 <= m <= 1000). Then m lines follow, every line contains 2 integers u, v (1<=u, v<=n) indicating an edge in G.

There is no self-loop or parallel edge.

Output Format

Output the answer for each test case.

  • If the graph is an odd graph, output "ODD GRAPH".
  • If the graph is not odd and contains an odd subgraph, output the subgraph in the following format. First output a number K, the vertices in this subgraph, and then output K integers in increasing order representing the vertices. These numbers should be printed in one line and separated by one space. No extra spaces. If there are multiple solutions, first minimize K, and if there are still multiple solutions, output the lexicographically smallest sequence of vertices.
  • If the graph is not odd and contains no odd subgraph, output "NO ODD SUBGRAPH"

For two sequences a[1], a[2], ..., a[K] and b[1], b[2], ..., b[k], a is lexicographically smaller than b if and only if for the smallest i such that a[i] != b[i], a[i] < b[i].

See the sample output for clarifications.

Sample Input

3
1 0
4 4
1 2
2 3
3 4
4 1
2 1
1 2

Sample Output

NO ODD SUBGRAPH
2 1 2
ODD GRAPH

Case Limits

Time limit: 500 msec

Memory limit: 64 MB

题意:给你一个无向图,如果所有顶点的度都为奇数,则输出ODD GRAPH,否则找该图的子图是否存在满足条件的,存在则输出最少顶点数和对应顶点的序号,否则输出NO ODD SUBGRAPH。

思路:用x[i]记录i顶点的度,map[i][j]记录顶点i和j之间有几条边,遍历x数组判断是否为ODD GRAPH,是则输出,否则继续判断子图,若存在,最小子图的顶点数必然为2,因此从顶点1往下找即可。

#include<stdio.h>
#include<string.h>
int map[122][122];
int x[122];
int main()
{	
	int n,m,t,i,j,a,b;	
	while(scanf("%d",&t)!=EOF)	
	{		
		while(t--)		
		{			
			scanf("%d%d",&n,&m);			
			if(m==0)			
			{				
				printf("NO ODD SUBGRAPH\n");				
				continue;		
			}
			memset(x,0,sizeof(x));		
			memset(map,0,sizeof(map));			
			for(i=0;i<m;i++)			
			{				
				scanf("%d%d",&a,&b);				
				x[a]++;				
				x[b]++;				
				map[a][b]++;				
				map[b][a]++;			
			}		
			int flag=0;			
			for(i=1;i<=n;i++)	
			{			
				if(x[i]%2==0)		
				{			
					flag=1;		
					break;		
				}		
			}		
			int flag1,xx,yy;		
			if(flag==1)	
			{			
				flag1=0;	
				for(i=1;i<=n;i++)	
				{			    
					for(j=i+1;j<=n;j++)		
					{					
						if(map[i][j]%2==1)	
						{					
							flag1=1;		
							xx=i;			
							yy=j;	
							break;		
						}				
					}				
					if(flag1==1)			
						break;			
				}		
			}		
			else		
				printf("ODD GRAPH\n");		
			if(flag1==1)		
			{			
				printf("2 %d %d\n",xx,yy);		
				flag1=2;			
			}			
			if(flag==0&&flag1==0)				
				printf("NO ODD SUBGRAPH\n");	
		}
	}
	return 0;
}


 


 

你可能感兴趣的:(网络,Integer,Graph,output,parallel,Numbers)