Problem B
Claw Decomposition
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 should 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: Mohammad Mahmudur Rahman
Special Thanks to: Manzurur Rahman Khan
<br/>#include<stdio.h><br/>#include<cstring><br/>#include<string><br/>#include<vector><br/>#define REP(i, b, n) for (int i = b; i < n; i++)<br/>#define rep(i,n) REP(i, 0, n)<br/>#define DBG 0<br/>using namespace std;<br/><br/>int N,color[300];<br/>vector<int>egs[300];<br/>bool trav[300];<br/>bool mark(int u,int cr){<br/> int b;<br/> if(DBG)printf("mark %d %d\n",u,cr);<br/> trav[u]=1,color[u]=cr;<br/> int nc=(cr==1?2:1);<br/> rep(i,egs[u].size()){<br/> b=egs[u][i];<br/> if(color[b]&&color[b]!=nc)return 0; //traverse and wrong color<br/> if(!trav[b])mark(b,nc);<br/> }<br/> return 1;<br/>}<br/>void ans(){<br/> bool fail=0;<br/> memset(color,0,sizeof(color));<br/> memset(trav,0,sizeof(trav));<br/> rep(i,N){<br/> if(!trav[i]){<br/> if(!mark(i,1)){fail=1;break;}<br/> }<br/> }<br/> if(!fail)printf("YES\n");<br/> else printf("NO\n");<br/>}<br/>int main(){<br/> int a,b;<br/> while(scanf("%d",&N)==1){<br/> rep(i,300)egs[i].clear();<br/> if(N==0)break;<br/> while(scanf("%d%d",&a,&b)==2){<br/> if(a+b==0)break;<br/> else a--,b--,egs[a].push_back(b),egs[b].push_back(a);<br/> }<br/> ans();<br/> }<br/>}<br/>
沒有留言:
張貼留言