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. 2017 清北学堂 Day 6终极考试报告

    预计分数: 100+70+70 = 240 实际假分数 : 40+80+70= 190  in cena(好吧不得不承认这个分数,,,,,,=.=) 实际真分数 : 100+80+100 = 280 ...

  2. vue从入门到开发--2-基本结构

    1.App.vue 是根文件,所有的其他组件的执行均需要在此文件内导入并调用才能实现. import (导入其他组件) Test (其他组件的名字) from ‘./components/test’( ...

  3. uvm_verision——告诉我你几岁了?

    uvm_version 定义了UVM相关的版本信息,而具体的uvm_revision则是通过在src/macros/uvm_version_defines.svh实现的. uvm_revision_s ...

  4. SQL 视图、事务

    假设看多个不同的表 select *from student ,score,course,teacher 有重复的    改为select student.Sno,sname,ssex,sbirthd ...

  5. POJ 3140 Contestants Division (树形DP,简单)

    题意: 有n个城市,构成一棵树,每个城市有v个人,要求断开树上的一条边,使得两个连通分量中的人数之差最小.问差的绝对值.(注意本题的M是没有用的,因为所给的必定是一棵树,边数M必定是n-1) 思路: ...

  6. (十)mybatis之配置(mybatis-config.xml)

    配置  可以从前篇的文章中知道(https://www.cnblogs.com/NYfor2018/p/9093472.html ),要使用mybatis需要以下配置: 1.  mybatis-con ...

  7. Servlet和JSP之标签文件学习

    在上一篇文章中介绍了自定义标签的用法,接下来介绍标签文件的用法啦. tag file指令 tag file简介 用tag file的方式,无需编写标签处理类和标签库描述文件,也可以自定义标签.tag ...

  8. Linux下文件以及文件名编码转换

    1.查看文件编码方式--file 文件名(但不是很准确) yang@mint-linux ~ $ file baidu.html baidu.html: HTML document, UTF-8 Un ...

  9. 动手使用ABAP Channel开发一些小工具,提升日常工作效率

    今天的故事要从ABAP小游戏说起. 中国的ABAP从业者们手头或多或少都搜集了一些ABAP小游戏,比如下面这些. 消灭星星: 扫雷: 来自我的朋友刘梦,公众号"SAP干货铺"里的俄 ...

  10. codeforces Gym 100338F Spam Filter 垃圾邮件过滤器(模拟,实现)

    阅读题, 概要:给出垃圾邮件和非垃圾邮件的集合,然后按照题目给出的贝叶斯公式计算概率一封邮件是垃圾邮件的概率. 逐个单词判断,将公式化简一下就是在垃圾邮件中出现的次数和在总次数的比值,大于二分之一就算 ...