Codechef January Challenge 2014 Sereja and Graph
Sereja and GraphProblem code: SEAGRP |
All submissions for this problem are available.
Read problems statements in Mandarin Chinese and Russian.
Sereja has undirected graph, which consists of n vertexes and m edges. Sereja can delete edges from the graph. Now Sereja is interested in one question : is it possible to delete edges in the graph so that the degree of each vertex in the graph will be equal 1?
Please, help Sereja.
Input
First line contains integer T. T testcases follow. The first line of each testcase contains integers n and m. Following m lines contain descriptions of the edges of the graph, each line contains two numbers — indexes of the vertexes, which are connected with edge. There can be multiple edges in the graph, but can not be any loops.
Output
For each test case print "YES", if you can remove the edges so that the degrees of all vertexes will be equal to 1, and "NO" otherwise. Print the words without quotation marks.
Constraints
- 1 <= T <= 10
- 1 <= n, m <= 100
Example
Input: 3 2 2 1 2 1 2 3 2 1 2 2 3 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output: YES NO YES
1 #pragma comment(linker,"/STACK:102400000,102400000") 2 #include <cstdio> 3 #include <vector> 4 #include <cmath> 5 #include <stack> 6 #include <queue> 7 #include <cstring> 8 #include <iostream> 9 #include <algorithm> 10 using namespace std; 11 #define INF 0x7fffffff 12 #define maxn 105 13 int n, m ,t, a ,b, x, y, mi, k ,flag; 14 int mp[maxn][maxn]; 15 int v[maxn], vis[maxn]; 16 int main(){ 17 scanf("%d", &t); 18 while (t--){ 19 flag = 0, k = 0; 20 scanf("%d%d", &n, &m); 21 memset(v, 0, sizeof v); 22 memset(mp, 0, sizeof mp); 23 memset(vis, 0, sizeof vis); 24 for (int i = 0; i < m; i++){ 25 scanf("%d%d", &a, &b); 26 mp[a][b] = mp[b][a] = 1; 27 } 28 if (n & 1){ printf("NO\n"); continue; } 29 for (int i = 1; i <= n; i++) 30 for (int j = 1; j <= n; j++) 31 v[i] += mp[i][j]; 32 while (k < n){ 33 mi = INF; 34 for (int i = 1; i <= n; i++) 35 if (!vis[i] && v[i] < mi){ x = i; mi = v[i]; } 36 vis[x] = 1;k++; 37 mi = INF; flag = 0; 38 for (int i = 1; i <= n;i++) 39 if (!vis[i] && mp[x][i] && v[i] < mi){ y = i; mi = v[i]; flag = 1; } 40 vis[y] = 1;k++; 41 if (flag == 0)break; 42 for (int i = 1; i <= n; i++){ 43 if (mp[x][i]){ mp[x][i] = 0; v[i]--; } 44 if (mp[y][i]){ mp[y][i] = 0; v[i]--; } 45 } 46 } 47 printf(flag ? "YES\n" : "NO\n"); 48 } 49 return 0; 50 }
浙公网安备 33010602011771号