博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
点的双联通+二分图的判定(poj2942)
阅读量:5321 次
发布时间:2019-06-14

本文共 7564 字,大约阅读时间需要 25 分钟。

Knights of the Round Table
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 10804   Accepted: 3553

Description

Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress, and drinking with the other knights are fun things to do. Therefore, it is not very surprising that in recent years the kingdom of King Arthur has experienced an unprecedented increase in the number of knights. There are so many knights now, that it is very rare that every Knight of the Round Table can come at the same time to Camelot and sit around the round table; usually only a small group of the knights isthere, while the rest are busy doing heroic deeds around the country. 
Knights can easily get over-excited during discussions-especially after a couple of drinks. After some unfortunate accidents, King Arthur asked the famous wizard Merlin to make sure that in the future no fights break out between the knights. After studying the problem carefully, Merlin realized that the fights can only be prevented if the knights are seated according to the following two rules:
  • The knights should be seated such that two knights who hate each other should not be neighbors at the table. (Merlin has a list that says who hates whom.) The knights are sitting around a roundtable, thus every knight has exactly two neighbors.
  • An odd number of knights should sit around the table. This ensures that if the knights cannot agree on something, then they can settle the issue by voting. (If the number of knights is even, then itcan happen that ``yes" and ``no" have the same number of votes, and the argument goes on.)
Merlin will let the knights sit down only if these two rules are satisfied, otherwise he cancels the meeting. (If only one knight shows up, then the meeting is canceled as well, as one person cannot sit around a table.) Merlin realized that this means that there can be knights who cannot be part of any seating arrangements that respect these rules, and these knights will never be able to sit at the Round Table (one such case is if a knight hates every other knight, but there are many other possible reasons). If a knight cannot sit at the Round Table, then he cannot be a member of the Knights of the Round Table and must be expelled from the order. These knights have to be transferred to a less-prestigious order, such as the Knights of the Square Table, the Knights of the Octagonal Table, or the Knights of the Banana-Shaped Table. To help Merlin, you have to write a program that will determine the number of knights that must be expelled. 

Input

The input contains several blocks of test cases. Each case begins with a line containing two integers 1 ≤ n ≤ 1000 and 1 ≤ m ≤ 1000000 . The number n is the number of knights. The next m lines describe which knight hates which knight. Each of these m lines contains two integers k1 and k2 , which means that knight number k1 and knight number k2 hate each other (the numbers k1 and k2 are between 1 and n ). 
The input is terminated by a block with n = m = 0 . 

Output

For each test case you have to output a single integer on a separate line: the number of knights that have to be expelled. 

Sample Input

5 51 41 52 53 44 50 0

Sample Output

2 题意:一群骑士要召开圆桌会议,坐在圆桌上的人都有两个邻居 满足两个要求:每个人与身边的邻居都不能有矛盾,且围坐的人数必须是奇数 题目给出m对矛盾关系,问有多少人一定是不能参加会议的 分析: 1.首先做补图,把题中给出的边舍去,把没有给出的边建图 2.然后在图中找奇数环,求点的双连通块 3.在双连通块中找奇数点的环,如果一个联通块中存在一个奇数环,那么该连通块中的每个人都有可能成为一个奇数环中的一员,此时把可能成为一员的人标记 4.找奇数环实际上就是判断该联通块是否是二分图,即用黑白交叉染色法判断,如果某条边的两个端点有相同的颜色,就存在了奇数环 程序:
1 #include"stdio.h"  2 #include"string.h"  3 #include"stdlib.h"  4 #include"algorithm"  5 #include"queue"  6 #include"math.h"  7 #include"iostream"  8 #include"vector"  9 #define M 1009 10 #define inf 0x3f3f3f3f 11 #define eps 1e-9 12 #define PI acos(-1.0) 13 #include"map" 14 #include"vector" 15 #include"set" 16 #include"string" 17 #include"stack" 18 #define LL __int64 19 using namespace std; 20 struct node 21 { 22     int u,v,next; 23 }edge[M*M*2]; 24 int t,head[M],dfn[M],low[M],cut[M],belong[M*M*2],use[M],color[M],exist[M]; 25 int indx,num; 26 int G[M][M]; 27 stack
q; 28 vector
bian[M]; 29 void init() 30 { 31 t=0; 32 memset(head,-1,sizeof(head)); 33 } 34 void add(int u,int v) 35 { 36 edge[t].u=u; 37 edge[t].v=v; 38 edge[t].next=head[u]; 39 head[u]=t++; 40 } 41 int dfs(int u)//交叉染色判断二分图 42 { 43 for(int i=0;i<(int)bian[u].size();i++) 44 { 45 int v=bian[u][i]; 46 if(color[v]==-1) 47 { 48 color[v]=color[u]^1; 49 int tt=dfs(v); 50 if(tt) 51 return 1; 52 } 53 else if(color[v]==color[u]) 54 return 1; 55 } 56 return 0; 57 } 58 void tarjan(int u,int fa,int id) 59 { 60 dfn[u]=low[u]=++indx; 61 for(int i=head[u];~i;i=edge[i].next) 62 { 63 int v=edge[i].v; 64 if(i==(id^1))continue; 65 if(!dfn[v]) 66 { 67 q.push(i);//把边入栈 68 tarjan(v,u,i); 69 low[u]=min(low[u],low[v]); 70 if(dfn[u]<=low[v])//求割点 71 { 72 cut[u]++; 73 int x; 74 num++; 75 int cnt=0; 76 map
mp; 77 do//当搜到一个割点u的时候把栈中边出栈,所有出栈的边都属于一个点双连通块 78 { 79 x=q.top(); 80 q.pop(); 81 belong[x]=belong[x^1]=num; 82 bian[edge[x].u].push_back(edge[x].v);//建立连通块联通图 83 bian[edge[x].v].push_back(edge[x].u); 84 if(!mp[edge[x].u])//离散化点 85 { 86 mp[edge[x].u]=1; 87 use[cnt++]=edge[x].u; 88 } 89 if(!mp[edge[x].v]) 90 { 91 mp[edge[x].v]=1; 92 use[cnt++]=edge[x].v; 93 } 94 95 }while(i!=x); 96 color[use[0]]=1; 97 if(dfs(use[0]))//如果存在奇数环,把点标记 98 { 99 for(int i=0;i
dfn[v])110 {111 low[u]=dfn[v];112 q.push(i);//把边入栈113 }114 }115 if(fa<0)116 cut[u]--;117 }118 int slove(int n)119 {120 for(int i=0;i<=n;i++)121 bian[i].clear();122 indx=num=0;123 memset(cut,0,sizeof(cut));124 memset(dfn,0,sizeof(dfn));125 memset(use,0,sizeof(use));126 memset(color,-1,sizeof(color));127 memset(exist,0,sizeof(exist));128 for(int i=1;i<=n;i++)129 {130 if(!dfn[i])131 tarjan(i,-1,-1);132 }133 int ans=0;134 for(int i=1;i<=n;i++)135 {136 if(!exist[i])137 ans++;138 }139 return ans;140 }141 int main()142 {143 int n,m;144 while(scanf("%d%d",&n,&m),m||n)145 {146 init();147 memset(G,0,sizeof(G));148 for(int i=1;i<=m;i++)149 {150 int a,b;151 scanf("%d%d",&a,&b);152 G[a][b]=G[b][a]=1;153 }154 for(int i=1;i<=n;i++)155 {156 for(int j=i+1;j<=n;j++)157 {158 if(!G[i][j])159 {160 add(i,j);161 add(j,i);162 }163 }164 }165 int ans=slove(n);166 printf("%d\n",ans);167 }168 return 0;169 }
View Code

 

 

转载于:https://www.cnblogs.com/mypsq/p/4713807.html

你可能感兴趣的文章
java中volatile关键字_Java面试官最爱问的volatile关键字
查看>>
魂斗罗归来大觉机器人_魂斗罗归来Boss模式详解 Boss血量分析
查看>>
java代码抛出异常_Java中编写代码出现异常,如何抛出异常,如何捕获异常
查看>>
macbook搭建java环境_在mac上搭建了Java 环境,谨以此文写给自己
查看>>
java 序列化协议_JAVA RPC(二)序列化协议杂谈
查看>>
java flexpaper_解决flexpaper读取本地文件问题,JAVA+TOMCAT
查看>>
java license框架_LICENSE · wanggn/Java微信开发框架封装版 - Gitee.com
查看>>
java格式化输出文件_浅谈Java中格式化输出
查看>>
java动画 线程 优缺点_Java创建线程三种方式的优缺点
查看>>
null object java_java1.8--Null Object模式
查看>>
java多维数组给一维数组_java一维数组和多维数组的使用
查看>>
appium java环境_Windows平台配置Appium+Java环境
查看>>
JAVA ik es_Elasticsearch es常用数据类型
查看>>
java web框架源码_轻量级java web实践-7(框架源码-5)
查看>>
java for update 无效_java – 事务性保存而不调用update方法
查看>>
java固定编程模式_深入解析Java的设计模式编程中的模板方法模式
查看>>
用java分别输入学生姓名时_Java集合框架上机练习题:用户分别从两个文本框输入学生的姓名和分数.............
查看>>
JAVA读取stream_java – 我们如何读取或使用outputstream的内容
查看>>
java如何传入DATE参数_如何在Java中传递Date作为SQL参数而不会丢失时间部分?
查看>>
ad单点登录java代码_AD 单点登录相关核心代码
查看>>