图论trainning-part-2 B. Claw Decomposition
B. Claw Decomposition
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的更多相关文章
- UVA 11396 Claw Decomposition(二分图)
以“爪”形为单元,问所给出的无向图中能否被完全分割成一个个单元. 分析图的性质,由于已知每个点的度是3,所以“爪”之间是相互交错的,即把一个“爪”分为中心点和边缘点,中心点被完全占据,而边缘点被三个“ ...
- UVA - 11396 Claw Decomposition(二分图染色)
题目大意:给你一张无向图,每一个点的度数都是3. 你的任务是推断是否能把它分解成若干个爪(每条边仅仅能属于一个爪) 解题思路:二分图染色裸题.能够得出:爪的中心点和旁边的三个点的颜色是不一样的 #in ...
- 【交叉染色法判断二分图】Claw Decomposition UVA - 11396
题目链接:https://cn.vjudge.net/contest/209473#problem/C 先谈一下二分图相关: 一个图是二分图的充分必要条件: 该图对应无向图的所有回路必定是偶环(构成该 ...
- UVA-11396 Claw Decomposition (二分图判定)
题目大意:给一张无向图,能否把它分成若干个“爪”,即,一个点有三个子节点. 题目分析:每个点的度数3是已知的,只需判断一下是不是二分图即可. 代码如下: # include<iostream&g ...
- UVA 11396 Claw Decomposition 染色
原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- 基于模糊聚类和最小割的层次化网格分割算法(Hierarchical Mesh Decomposition)
网格分割算法是三维几何处理算法中的重要算法,具有许多实际应用.[Katz et al. 2003]提出了一种新型的层次化网格分割算法,该算法能够将几何模型沿着凹形区域分割成不同的几何部分,并且可以避免 ...
- 基于模糊聚类和最小割的层次化三维网格分割算法(Hierarchical Mesh Decomposition)
网格分割算法是三维几何处理算法中的重要算法,具有许多实际应用.[Katz et al. 2003]提出了一种新型的层次化网格分割算法,该算法能够将几何模型沿着凹形区域分割成不同的几何部分,并且可以避免 ...
- [leetcode] 题型整理之图论
图论的常见题目有两类,一类是求两点间最短距离,另一类是拓扑排序,两种写起来都很烦. 求最短路径: 127. Word Ladder Given two words (beginWord and end ...
- Matrix QR Decomposition using OpenCV
Matrix QR decomposition is very useful in least square fitting model. But there is no function avail ...
随机推荐
- SQL问题:未启用当前数据库的 SQL Server Service Broker
数据库分离后,附加回到数据库,然后在程序中打开调用数据库的页面,出现如下问题:“未启用当前数据库的 SQL Server Service Broker,因此查询通知不受支持.如果希望使用通知,请为此数 ...
- COGS 2211. [BZOJ3653]谈笑风生
★★★★ 输入文件:laugh.in 输出文件:laugh.out 简单对比时间限制:3 s 内存限制:512 MB [问题描述] 设T 为一棵有根树,我们做如下的定义: • 设a和b ...
- codevs 1390 回文平方数 USACO
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 青铜 Bronze 题目描述 Description 回文数是指从左向右念和从右像做念都一样的数.如12321就是一个典型的回文数 ...
- JS常用操作节点的方法
js常见的创建dom节点的方法有 createElement() 创建一个元素节点 => 接收参数为string类型的nodename createTextNode() 创建一个文本节点 =&g ...
- python基础一 day14 生成器函数进阶
def generator(): print(123) content = yield 1 print('=======',content) print(456) arg = yield 2 '''' ...
- 2.add two number
在初始化的时候:ListNode* result;这样就会报runtime error
- 引入了junit为什么还是用不了@Test注解
pom文件明明引入了unit,为什么还是用不了@Test? 配置如下: <dependency> <groupId>junit</groupId> <arti ...
- ASIHTTPRequest简单学习
ASIHTTPRequest框架是优秀的第三方Objective-C的HTTP框架,支持Mac OS X和iOS下的HTTP开发. 一.ASIHTTPRequest框架的安装和配置 (1)首先要在项目 ...
- Bzoj 1083: [SCOI2005]繁忙的都市 (最小生成树)
Bzoj 1083: [SCOI2005]繁忙的都市 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1083 此题是最小瓶颈生成树的裸题. ...
- OpenWrt 路由器如何让 lan 口主机获得 ipv6 网络访问 -- 知乎
本文转自知乎: OpenWrt 路由器如何让 lan 口主机获得 ipv6 网络访问? - mistforest的回答 - 知乎https://www.zhihu.com/question/29667 ...