图论trainning-part-2 B. Claw Decomposition

时间:2022-08-25 04:11:48

B. Claw Decomposition

Time Limit: 1000ms
Memory Limit: 131072KB

64-bit integer IO format: %lld      Java class name: Main

 

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.

Lets 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

解题:二分图的判断,使用染色法!如果相邻顶点颜色相同,即不是二分图。

DFS解法

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxv = ;
struct arc{
int v;
};
vector<arc>g[maxv];
int n;
bool color[maxv];
bool dfs(int u){
for(int i = ; i < g[u].size(); i++){
int j = g[u][i].v;
if(!color[j]){
color[j] = !color[u];
if(!dfs(j)) return false;
}else if(color[j] == color[u]) return false;
}
return true;
}
int main() {
int i,u,v;
while(scanf("%d",&n),n){
if(n == ) {puts("NO");continue;}
for(i = ; i <= n; i++)
g[i].clear();
while(scanf("%d%d",&u,&v),u||v){
g[u].push_back((arc){v});
g[v].push_back((arc){u});
}
memset(color,false,sizeof(color));
dfs()?puts("YES"):puts("NO");
}
return ;
}

 BFS解法:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
vector<int>g[maxn];
queue<int>qu;
int n,color[maxn];
bool bfs(int src){
while(!qu.empty()) qu.pop();
qu.push(src);
color[src] = ;
while(!qu.empty()){
int u = qu.front(),v;
qu.pop();
for(int i = ; i < g[u].size(); i++){
v = g[u][i];
if(color[v] == -){
color[v] = !color[u];
qu.push(v);
}else if(color[v] == color[u]) return false;
}
}
return true;
}
int main(){
int u,v;
while(scanf("%d",&n),n){
memset(color,-,sizeof(color));
for(int i = ; i <= n; i++)
g[i].clear();
while(scanf("%d%d",&u,&v),u||v){
g[u].push_back(v);
g[v].push_back(u);
}
bfs()?puts("YES"):puts("NO");
}
return ;
}