(beginer)DFS UVA 11396 Claw Decomposition
2014-02-08 09:54
218 查看
Claw
Decomposition
Input: Standard
Input
Output: Standard
Output
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.
Let’s 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
题意:给出n个节点的简单无向图,每个点的度数是3.你的任务是判断能不能把它分解成若干个爪(K1,3),在你的分解方案中,每条边必须恰好属于一个爪,但同一个节点可以出现在多个爪里。
思路:首先每条边都要属于一个爪,对于每一个点,与他相邻的点在爪中的位置一定是不同的,如果这个点是爪的中心,那么周围3个点都是爪子的3个2度的点,如果这个点是爪中2度的点,那么周围3个点都应该是3度的那个中心节点。所以就是把节点分成两类就行了。
代码:#include<iostream>#include<cstdio>#include<cstring>#include<string.h>#include<algorithm>#include<math.h>#include<queue>using namespace std;const int maxn = 2000;
struct Node { int y; Node *next;}edge[maxn] , *first[maxn];
int n , m;int color[310];
void init(){ memset(edge,0,sizeof(edge)); memset(first,0,sizeof(first)); m = 0;}
void add(int x,int y){ edge[++m].y = y; edge[m].next = first[x]; first[x] = &edge[m];}
void input(){ int x , y; while (scanf("%d%d",&x,&y),x+y) { add(x,y); add(y,x); }}
bool bipartite(int x){ Node * p = first[x]; while (p) { int y = p->y; if (color[x]==color[y]) return false; if (!color[y]) { color[y] = 3-color[x]; if (!bipartite(y)) return false; } p = p->next; } return true;}
void solve(){ memset(color,0,sizeof(color)); color[1] = 1; if (bipartite(1)) cout << "YES" << endl; else cout << "NO" << endl;}
int main(){ while (scanf("%d",&n),n) { init(); input(); solve(); }}
Decomposition
Input: Standard
Input
Output: Standard
Output
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.
Let’s 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 |
思路:首先每条边都要属于一个爪,对于每一个点,与他相邻的点在爪中的位置一定是不同的,如果这个点是爪的中心,那么周围3个点都是爪子的3个2度的点,如果这个点是爪中2度的点,那么周围3个点都应该是3度的那个中心节点。所以就是把节点分成两类就行了。
代码:#include<iostream>#include<cstdio>#include<cstring>#include<string.h>#include<algorithm>#include<math.h>#include<queue>using namespace std;const int maxn = 2000;
struct Node { int y; Node *next;}edge[maxn] , *first[maxn];
int n , m;int color[310];
void init(){ memset(edge,0,sizeof(edge)); memset(first,0,sizeof(first)); m = 0;}
void add(int x,int y){ edge[++m].y = y; edge[m].next = first[x]; first[x] = &edge[m];}
void input(){ int x , y; while (scanf("%d%d",&x,&y),x+y) { add(x,y); add(y,x); }}
bool bipartite(int x){ Node * p = first[x]; while (p) { int y = p->y; if (color[x]==color[y]) return false; if (!color[y]) { color[y] = 3-color[x]; if (!bipartite(y)) return false; } p = p->next; } return true;}
void solve(){ memset(color,0,sizeof(color)); color[1] = 1; if (bipartite(1)) cout << "YES" << endl; else cout << "NO" << endl;}
int main(){ while (scanf("%d",&n),n) { init(); input(); solve(); }}
相关文章推荐
- (beginer)DFS (双连通分量) UVA 10765 Doves and bombs
- UVa 11396 - Claw Decomposition(二分图判定,染色法,dfs)
- (beginer)DFS (2-SAT) UVA 11294 Wedding
- (beginer)DFS (dfs) UVA 10319 Manhattan
- (beginer) DFS (二分图判定) UVA 11080 Place the Guards
- uva11396 dfs应用
- (beginer)DFS (强连通)UVA 11504 Dominos
- UVA-11396 - Claw Decomposition(DFS)
- (beginer)DFS (桥...边-双连通分量) UVA 610 - Street Directions
- 紫书章七例五 Krypton Factor UVA - 129(dfs)判断一个字符串中是否包含两个相邻子串
- UVa 129 - Krypton Factor(DFS+回溯)
- UVA572油田问题(DFS求连通块)
- [POJ 2631/UVA 10308 Roads in the North] DFS求树上的最长路
- 【UVA】10317 - Equating Equations(dfs + 剪枝)
- UVa 572 油田(DFS求连通块)
- UVA 572 dfs求连通块
- (beginer)最短路 UVA 11367 Full Tank?
- uva 10318(dfs+状态压缩)
- uva585(dfs)
- uva 10344 23 out of 5 (DFS)