图论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 ...
随机推荐
- FPGA的嵌入式RAM
FPGA中的嵌入式RAM分为两种:专用的BRAM和分布是RAM(用LUT实现的).这两种RAM又可以配置成单端口和双端口的RAM和ROM.双端口RAM又可以根据读写地址是否在同一块分为Double P ...
- OPENFIRE 使用Hazelcast插件进行集群
参考资料:http://www.linuxidc.com/Linux/2014-01/94850.htm https://www.igniterealtime.org/projects/openf ...
- MFC技术积累——基于MFC对话框类的那些事儿
1. 创建对话框类 (1)打开VC++6.0环境,点击:文件→新建: (2)在弹出的新建对话框中选择:工程→MFC AppWizard (exe)→输入工程名称(例如:功能调试)→工程保存路径名→确定 ...
- 新建maven的pom.xml第一行出错的解决思路
前言:博主在想要用maven创建项目的时候,忘记之前已经安装过maven了,所以再安装了另一个版本的maven,导致在pom.xml的第一行总是显示某一个jar的zip文件读取不出来. 在网上找了很多 ...
- 国家气象局提供的天气预报接口(完整Json接口)
国家气象局提供的天气预报接口主要有三个,分别是:http://www.weather.com.cn/data/sk/101010100.htmlhttp://www.weather.com.cn/da ...
- Maven归纳
一.常用功能 1.Maven的中央仓库 https://mvnrepository.com/ 2.添加jar包依赖 1.首先点击pom.xml,然后点击弹出页面中的Dependencies选项,接 ...
- win10搭建Java环境
一.下载地址 jdk和jre官方网址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 根据你的系统选择你需要 ...
- 爬虫学习之pdf读取和存储
在py3中如需进行pdf文件操作需要加载PDFMiner3K库文件,可通过pip方式或者可以下载源文件方式安装 python3 -m pip install pdfminer3k 下载源文件方式: 1 ...
- Python可变与不可变类型及垃圾回收机制
1. 可变与不可变类型 1.1 可变类型 在id不变的情况下,value可以改变,则称之为可变类型.列表.字典与集合是可变的. l1 = [,,,,] print(id(l1)) l1[] = #改 ...
- CSS3-transform3D
CSS3 3D位移 在CSS3中3D位移主要包括两种函数translateZ()和translate3d().translate3d()函数使一个元素在三维空间移动.这种变形的特点是,使用三维向量的坐 ...