UVa 11396 爪分解(二分图判定)

https://vjudge.net/problem/UVA-11396

题意:

给出n个结点的简单无向图,每个点的度数均为3。你的任务是判断能否把它分解成若干爪。每条边必须属于一个爪,但同一个点可以出现在多个爪里。

思路:

一个鸡爪当中,有一个中心点,即度为3的点,还有3个边缘点。

每条边都连接了一个中心点和一个边缘点,于是就是二分图判定。

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<vector>
 6 #include<stack>
 7 #include<queue>
 8 #include<cmath>
 9 #include<map>
10 using namespace std;
11
12 const int maxn=300+5;
13
14 vector<int> G[maxn];
15 int color[maxn];
16
17 bool bipartite(int u)
18 {
19     for(int i=0;i<G[u].size();i++)
20     {
21         int v=G[u][i];
22         if(color[v]==color[u])  return false;
23         if(color[v]==0)
24         {
25             color[v]=3-color[u];
26             if(!bipartite(v))   return false;
27         }
28     }
29     return true;
30 }
31
32 int n;
33
34 int main()
35 {
36     //freopen("D:\\input.txt","r",stdin);
37     while(~scanf("%d",&n) && n)
38     {
39         for(int i=1;i<=n;i++)   G[i].clear();
40         int u,v;
41         while(true)
42         {
43             scanf("%d%d",&u,&v);
44             if(u==0 && v==0)  break;
45             G[u].push_back(v);
46             G[v].push_back(u);
47         }
48         memset(color,0,sizeof(color));
49         color[1]=1;
50         bool flag = bipartite(1);
51         if(flag)  puts("YES");
52         else puts("NO");
53     }
54     return 0;
55 }
时间: 2024-08-03 15:00:49

UVa 11396 爪分解(二分图判定)的相关文章

uva 11396Claw Decomposotion(二分图判定)

 题目大意:给出一个简单无向图,每一个点的度为3.推断是否能将此图分解成若干爪的形式.使得每条边都仅仅出如今唯一的爪中. (点能够多次出如今爪中) 这道题实质上就是问这个图是否为二分图,dfs判定就可以 #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm> #includ

UVA 11080 - Place the Guards(二分图判定)

UVA 11080 - Place the Guards 题目链接 题意:一些城市.之间有道路相连,如今要安放警卫,警卫能看守到当前点周围的边,一条边仅仅能有一个警卫看守,问是否有方案,假设有最少放几个警卫 思路:二分图判定,判定过程记录下白点和黑点个数,小的就是要安放的个数,注意假设是0,那么应该是加1 代码: #include <cstdio> #include <cstring> #include <vector> using namespace std; con

poj2942 Knights of the Round Table,无向图点双联通,二分图判定

点击打开链接 无向图点双联通,二分图判定 <span style="font-size:18px;">#include <cstdio> #include <stack> #include <vector> #include <algorithm> #include <cstring> using namespace std; struct Edge{ int u, v; }; const int maxn = 1

hdu 5285 wyh2000 and pupil(二分图判定)

对每两个不认识的人连一条边,则此题可转化为二分图判定(二分图可有多个). 如果有一部分图判定为不是二分图,则输出“Poor wyh”. 否则,分别累加每个二分图的最多的颜色数. #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <string> #include <stack> #include <cmat

二分图判定 nyoj1015(模板)

题目:点击打开链接nyoj1015 分析:题意很清楚,就是让判断一个图是不是二分图,思路当然就是染色法,首先给一个顶点然色,然后与它相邻的顶点全部染相反的颜色,如果过程中发现要染的点已经染色了,而且是和现在点相同的颜色的话,那么就说明不是一个二分图. 其实就是广搜模板 #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <vector&g

二分图判定【图的搜索】

二分图判定 给定一个图,要给图上每个顶点染色,并且要使得相邻的顶点颜色不同.问是否能最多用2种颜色进行染色? 参考如下: Source Code: #include <iostream> #include <vector> using namespace std; vector<int> Edge[1010]; int colorArr[1010]; void initColorArr(int length){ for(int i=0;i<=length;++i)

HDU 2444 The Accomodation of Students(二分图判定+最大匹配)

Problem Description There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other. Now you are given all pairs o

编程算法 - 二分图判定 代码(C)

二分图判定 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 给定一个具有n个顶点的图. 要给图上每个顶点染色, 并且要使相邻的顶点颜色不同.  是否能最多用2种颜色进行染色. 没有重边和闭环. 即二分图问题. 使用深度优先搜索(dfs), 把顶点染成c, 然后相邻边染成-c. 如果相邻边被染色过, 且相同, 则图不是二分图; 如果所有边都被染色, 并且互不相同, 则是二分图. 进行多次搜索, 避免非连通图. 代码: /* * CppPrim

HDU 2444 The Accomodation of Students 二分图判定+最大匹配

题目来源:HDU 2444 The Accomodation of Students 题意:n个人是否可以分成2组 每组的人不能相互认识 就是二分图判定 可以分成2组 每组选一个2个人认识可以去一个双人间 最多可以有几组 思路:二分图判定+最大匹配 #include <cstdio> #include <cstring> #include <vector> using namespace std; const int maxn = 550; int vis[maxn];