Problem B
Claw Decomposition
Input:Standard Input
Output:Standard Output
A claw is defined as a pointed curved nail on the end of each toe in birds, some reptiles, and some mammals. however, if you are a graph theory enthusiast, you may understand the following special class of graph as shown in the following figure by the word
Claw.
If you are more concerned about graph theory terminology, you may want to define claw as K1, 3.
Let's leave the definition for the moment & come to the problem. you are given a simple undirected graph in which every vertex has degree 3. you are to figure out whether the graph can be decomposed into claws or not.
Just for the sake of clarity, a decomposition of a graph is a list of subgraphs such that each edge appears in exactly one subgraph in the list.
Input
There will be several cases in the input file. each case starts with the number of vertices in the graph, V (4 <= V <= 300 ). this is followed by a list of edges. every line in the list has two integers, a & B, the endpoints of an edge (1 <= a, B <= V ). the edge list
Ends with a line with a pair of 0. The end of input is denoted by a case with V = 0. This case shocould not be processed.
Output
For every case in the input, print YES if the graph can be decomposed into claws & NO otherwise.
Sample Input Output for Sample Input
4 1 2 1 3 1 4 2 3 2 4 3 4 0 0 6 1 2 1 3 1 6 2 3 2 5 3 4 4 5 4 6 5 6 0 0 0 |
NO NO |
Problemsetter: Hammad Mahmudur Rahman
Special Thanks to: Manzurur Rahman Khan
First, let's analyze the question. We need to divide the source image into several "Claws", and each claw has three edges, because the question shows that each side can only belong to one claw, therefore, the total number of edges in the graph should be a multiple of 3, and then the degree of each vertex is 3, and m * 2 = n * 3 can be obtained. (M is the number of edges, and n is the number of points). This is the first analysis of the relationship between the edge and the number of points in the graph.
Is it true that the graph meets this condition must be YES? In this case, the detailed information of each edge provided by the question is useless. From experience, this idea is wrong.
I had to go deep into the analysis and see that the claw is marked with the point in the middle of the claw, the other three points can only be their "affiliated", and these three points can only be used as the "affiliated" of other claws ", otherwise, each side cannot meet the condition that only one claw belongs. After realizing this, there may be inspiration (this can only be explained here), and it is found that the "mark" of all the claws will not be connected by edges, in addition, all "affiliated" are not connected. Is this entire image a binary image we are familiar? After analysis, the problem is solved. Looking back, this question is very interesting for those who are studying Graph Theory for the first time, and those who answer this question independently can enjoy this pleasure!
# Include <cstdio> # include <algorithm> # include <vector> # include <cstring> using namespace std; const int maxn = 300 + 5; int color [maxn]; vector <int> G [maxn]; bool bipartite (int u) {for (int I = 0; I <G [u]. size (); I ++) {int v = G [u] [I]; if (color [v] = color [u]) return false; if (! Color [v]) {color [v] = 3-color [u]; if (! Bipartite (v) return false;} return true;} int main () {int n, m; while (scanf ("% d", & n )) {if (n = 0) break; int a, B; m = 0; memset (color, 0, sizeof (color); for (int I = 0; I <= n; I ++) G [I]. clear (); while (scanf ("% d", & a, & B) {if (a = 0 & B = 0) break; G [a]. push_back (B); G [B]. push_back (a); m ++;} color [1] = 1; // remember to set the color of the initial node to 1 if (m * 2 = n * 3 & bipartite (1) printf ("YES \ n "); else printf ("NO \ n");} return 0 ;}