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 ;
}

图论trainning-part-2 B. Claw Decomposition的更多相关文章

  1. UVA 11396 Claw Decomposition(二分图)

    以“爪”形为单元,问所给出的无向图中能否被完全分割成一个个单元. 分析图的性质,由于已知每个点的度是3,所以“爪”之间是相互交错的,即把一个“爪”分为中心点和边缘点,中心点被完全占据,而边缘点被三个“ ...

  2. UVA - 11396 Claw Decomposition(二分图染色)

    题目大意:给你一张无向图,每一个点的度数都是3. 你的任务是推断是否能把它分解成若干个爪(每条边仅仅能属于一个爪) 解题思路:二分图染色裸题.能够得出:爪的中心点和旁边的三个点的颜色是不一样的 #in ...

  3. 【交叉染色法判断二分图】Claw Decomposition UVA - 11396

    题目链接:https://cn.vjudge.net/contest/209473#problem/C 先谈一下二分图相关: 一个图是二分图的充分必要条件: 该图对应无向图的所有回路必定是偶环(构成该 ...

  4. UVA-11396 Claw Decomposition (二分图判定)

    题目大意:给一张无向图,能否把它分成若干个“爪”,即,一个点有三个子节点. 题目分析:每个点的度数3是已知的,只需判断一下是不是二分图即可. 代码如下: # include<iostream&g ...

  5. UVA 11396 Claw Decomposition 染色

    原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  6. 基于模糊聚类和最小割的层次化网格分割算法(Hierarchical Mesh Decomposition)

    网格分割算法是三维几何处理算法中的重要算法,具有许多实际应用.[Katz et al. 2003]提出了一种新型的层次化网格分割算法,该算法能够将几何模型沿着凹形区域分割成不同的几何部分,并且可以避免 ...

  7. 基于模糊聚类和最小割的层次化三维网格分割算法(Hierarchical Mesh Decomposition)

    网格分割算法是三维几何处理算法中的重要算法,具有许多实际应用.[Katz et al. 2003]提出了一种新型的层次化网格分割算法,该算法能够将几何模型沿着凹形区域分割成不同的几何部分,并且可以避免 ...

  8. [leetcode] 题型整理之图论

    图论的常见题目有两类,一类是求两点间最短距离,另一类是拓扑排序,两种写起来都很烦. 求最短路径: 127. Word Ladder Given two words (beginWord and end ...

  9. Matrix QR Decomposition using OpenCV

    Matrix QR decomposition is very useful in least square fitting model. But there is no function avail ...

随机推荐

  1. Objective-C Data Encapsulation

    All Objective-C programs are composed of the following two fundamental elements: Program statements ...

  2. thinkphp写的登录注册的小demo

    和asp.net类似,一个FormAction对应Form文件夹 demo结构: ‘ 对于项目结构有疑问的: http://www.thinkphp.cn/document/60.html login ...

  3. Idea注释参数报错,控制台乱码问题解决方法

    idea虽然工具非常好用,但是他的一些解决方法网上非常的少,有些压根没有,解决这些问题非常浪费时间 1.最近在工作中发现一个问题,使用ant打包后,控制台总是报错,提示信息还是乱码的,吓得我赶紧用回了 ...

  4. Selenium私房菜系列9 -- 我遇到的问题及解决问题的方法

    Selenium私房菜系列10 -- 我遇到的问题及解决问题的方法

  5. Codeforces Round #290 (Div. 2) _B找矩形环的三种写法

    http://codeforces.com/contest/510/status/B 题目大意 给一个n*m  找有没有相同字母连起来的矩形串 第一种并查集 瞎搞一下 第一次的时候把val开成字符串了 ...

  6. selenium+chrome浏览器驱动-爬取百度图片

    百度图片网页中中,当页面滚动到底部,页面会加载新的内容. 我们通过selenium和谷歌浏览器驱动,执行js,是浏览器不断加载页面,通过抓取页面的图片路径来下载图片. from selenium im ...

  7. blog.yiz96.com

    欢迎访问我的新博客 blog.yiz96.com

  8. 关于POST的请求的问题的汇总

    1)404 解决方式:检查路径,路由问题 2)500 解决方式:1)首先检查代码 2)检查是否是参数未接收到 3)检查是否Content-Type类型导致的参数未收到 4)区分body-raw跟bod ...

  9. linux设置http/https proxy及忽略proxy的方法

    msys2设置网络代理 在文件 .bashrc 中添加 export http_proxy="proxy IP:port" 如 export http_proxy="19 ...

  10. [HDU5360]:Gorgeous Sequence(小清新线段树)

    题目传送门 题目描述: (原题英文) 操作0:输入l,r,t,线段树区间与t取min. 操作1:输入l,r,区间取最大值. 操作2:输入l,r,区间求和. 输入格式: 第一行一个整数T,表示数据组数: ...