博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
图论trainning-part-2 B. Claw Decomposition
阅读量:5007 次
发布时间:2019-06-12

本文共 3659 字,大约阅读时间需要 12 分钟。

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解法

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #define LL long long13 #define INF 0x3f3f3f3f14 using namespace std;15 const int maxv = 310;16 struct arc{17 int v;18 };19 vector
g[maxv];20 int n;21 bool color[maxv];22 bool dfs(int u){23 for(int i = 0; i < g[u].size(); i++){24 int j = g[u][i].v;25 if(!color[j]){26 color[j] = !color[u];27 if(!dfs(j)) return false;28 }else if(color[j] == color[u]) return false;29 }30 return true;31 }32 int main() {33 int i,u,v;34 while(scanf("%d",&n),n){35 if(n == 1) {puts("NO");continue;}36 for(i = 1; i <= n; i++)37 g[i].clear();38 while(scanf("%d%d",&u,&v),u||v){39 g[u].push_back((arc){v});40 g[v].push_back((arc){u});41 }42 memset(color,false,sizeof(color));43 dfs(1)?puts("YES"):puts("NO");44 }45 return 0;46 }
View Code

 

 BFS解法:

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #define LL long long13 #define INF 0x3f3f3f3f14 using namespace std;15 const int maxn = 310;16 vector
g[maxn];17 queue
qu;18 int n,color[maxn];19 bool bfs(int src){20 while(!qu.empty()) qu.pop();21 qu.push(src);22 color[src] = 1;23 while(!qu.empty()){24 int u = qu.front(),v;25 qu.pop();26 for(int i = 0; i < g[u].size(); i++){27 v = g[u][i];28 if(color[v] == -1){29 color[v] = !color[u];30 qu.push(v);31 }else if(color[v] == color[u]) return false;32 }33 }34 return true;35 }36 int main(){37 int u,v;38 while(scanf("%d",&n),n){39 memset(color,-1,sizeof(color));40 for(int i = 1; i <= n; i++)41 g[i].clear();42 while(scanf("%d%d",&u,&v),u||v){43 g[u].push_back(v);44 g[v].push_back(u);45 }46 bfs(1)?puts("YES"):puts("NO");47 }48 return 0;49 }
View Code

 

转载于:https://www.cnblogs.com/crackpotisback/p/3862861.html

你可能感兴趣的文章
阅读之https及加密原理
查看>>
HDOJ4550 卡片游戏 随便销毁内存的代价就是wa//string类的一些用法
查看>>
css文本样式text、字体样式font
查看>>
洛谷 P1020 导弹拦截(LIS)
查看>>
python判断图片是否损坏
查看>>
MySQL服务启动:某些服务在未由其他服务或程序使用时将自动停止
查看>>
软件工程第四周作业 - 单元测试
查看>>
KNN与SVM对比&SVM与逻辑回归的对比
查看>>
php 现在拓展地址
查看>>
【Java并发编程】之十六:深入Java内存模型——happen-before规则及其对DCL的分析(含代码)...
查看>>
团队个人冲刺第三天
查看>>
unit
查看>>
2017-10-17 NOIP模拟赛2
查看>>
How to install ia32-libs in Ubuntu 14.04 LTS (Trusty Tahr)
查看>>
ACM/ICPC 之 模拟 (HNUOJ 13391-换瓶模拟)
查看>>
JavaWeb学习——JSP基础
查看>>
Eclipse tomcat server 无法添加项目
查看>>
黑寡妇黄飞鸿
查看>>
leetcode 217 Contains Duplicate 数组中是否有重复的数字
查看>>
The Ctrl & CapsLock `problem'
查看>>