【POJ - 2942】Knights of the Round Table(点双连通分量,二分图判断奇环奇圈)

题干:

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 5
1 4
1 5
2 5
3 4
4 5
0 0

Sample Output

2

Hint

Huge input file, 'scanf' recommended to avoid TLE. 

题目大意:

亚瑟王要在圆桌上召开骑士会议,为了不引发骑士之间的冲突,并且能够让会议的议题有令人满意的结果,每次开会前都必须对出席会议的骑士有如下要求: 1、  相互憎恨的两个骑士不能坐在直接相邻的2个位置; 2、  出席会议的骑士数必须是奇数,这是为了让投票表决议题时都能有结果。   如果出现有某些骑士无法出席所有会议(例如这个骑士憎恨所有的其他骑士),则亚瑟王为了世界和平会强制把他剔除出骑士团。        现在给定准备去开会的骑士数n,再给出m对憎恨对(表示某2个骑士之间使互相憎恨的),问亚瑟王至少要剔除多少个骑士才能顺利召开会议?

一句话题意:

一些骑士,他们有些人之间有矛盾,现在要求选出一些骑士围成一圈,圈要满足如下条件:1.人数大于1。2.总人数为奇数。3.有仇恨的骑士不能挨着坐。问有几个骑士不能和任何人形成任何的圆圈。

解题报告:

注意这里的col数组不是给tarjan用的,而是给二分图染色用的。

注意割点会被flag[]=1多次,所以不能在过程中纪录ans,而是要标记can[]=1,最后遍历can数组看有多少等于1的。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAXN = 1000 + 5;
const int MAXM = 2e6 + 5;
bool maze[MAXN][MAXN],can[MAXN];
struct Edge {int u,v;int ne;
} e[MAXM];
int dfn[MAXN],low[MAXN],stk[MAXN],clk,tot,index,bcc;
int flag[MAXN],tmp[MAXN],col[MAXN],head[MAXN];
int n,m;
void init() {for(int i = 1; i<=n; i++) {dfn[i]=low[i]=can[i]=0;//如果不初始化fa???head[i] = -1;}tot=0;clk=bcc=index=0;
}
void add(int u,int v) {e[++tot].u = u;e[tot].v = v;e[tot].ne = head[u];head[u] = tot;
}
bool bfs(int st) {queue<int> q;q.push(st);col[st] = 0;while(!q.empty()) {int cur = q.front();q.pop();for(int i = head[cur]; ~i; i = e[i].ne) {int v = e[i].v;if(flag[v] == 0) continue;if(col[v] != -1) {if(col[v] == col[cur]) return 1;}else {col[v] = !col[cur];q.push(v);}}}return 0;
}
void tarjan(int x,int fa) {dfn[x] = low[x] = ++clk;stk[++index] = x;for(int i = head[x]; ~i; i = e[i].ne) {int v = e[i].v;if(v == fa) continue;if(dfn[v] == 0) {tarjan(v,x);low[x] = min(low[x],low[v]);if(low[v] >= dfn[x])  {bcc++;//这里点双也用bcc表示一下。。for(int i = 1; i<=n; i++) flag[i] = 0,col[i]=-1; int cnt = 0;tmp[++cnt] = x; while(1) {int tmpp = stk[index];index--;tmp[++cnt] = tmpp;
//					col[tmp] = bcc; //这里不需要记录颜色,col数组用来二分图染色比较好。 					if(tmpp == v) break;//注意这里不是tmp==x,因为x还要在栈内,做其他点双联通分量的成员。而当前bcc,将x单独处理已下架就好。 } //别忘处理割点:for(int j = 1; j<=cnt; j++) flag[tmp[j]] = 1;if(bfs(x) == 1) {for(int j = 1; j<=cnt; j++) can[tmp[j]]=1;}} }else low[x] = min(low[x],dfn[v]);}
}
int main()
{while(~scanf("%d%d",&n,&m)) {if(n == 0 && m == 0) break;memset(maze,1,sizeof maze);init();// 每次都是 写好了init函数,忘了加到主函数中。。 for(int u,v,i = 1; i<=m; i++) {scanf("%d%d",&u,&v);maze[u][v] = maze[v][u] = 0;}for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {if(i!=j && maze[i][j]) add(i,j);}}for(int i = 1; i<=n; i++) {if(dfn[i] == 0) tarjan(i,-1);}int ans = 0;for(int i = 1; i<=n; i++) {ans += can[i];}printf("%d\n",n - ans);}return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440126.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

zookeeper单节点部署

hadoop 安装 在/install-package目录下查看zookeeper的安装包 本文中安装的是zookeeper-3.4.12.tar.gz 下方为百度云链接 链接&#xff1a;https://pan.baidu.com/s/1bzq4ILH41owtS__3tBCcRQ 提取码&#xff1a;6q4r 把下载好的zookeeper-3.4.12.tar.gz 放到/install-packa…

机器学习笔记(五):逻辑回归

目录 1&#xff09;Classification 2&#xff09;Hypothesis Representation 3&#xff09;Decision boundary 4&#xff09;Cost function 5&#xff09;Simplified cost function and gradient descent 6&#xff09;Multi-class classification:One-vs-all 7&#xf…

xrdp完美实现Windows远程访问Ubuntu 16.04

前言&#xff1a; 在很多场景下&#xff0c;我们需要远程连接到Linux服务器(本文是Ubuntu)&#xff0c;传统的连接主要分为两种。 第一种&#xff1a;通过SSH服务&#xff08;使用xshell等工具&#xff09;来远程访问&#xff0c;编写终端命令&#xff0c;不过这个是无界面的&a…

【HDU - 6203】ping ping ping(lca+贪心思想,对lca排序,树状数组差分)

题干&#xff1a; 给出一个n1个点的树&#xff0c;以及p个点对&#xff0c;需要断开一些点&#xff0c;使得这p个点对路径不连通。输出应该断开的最少点数。 解题报告&#xff1a; 从那p个点对入手的话&#xff1a;首先考虑只有一对点的话&#xff0c;肯定是这条路径上的随便…

机器学习笔记(六):正则化

目录 1&#xff09;The problem of overfitting 2&#xff09;Cost function 3&#xff09;Regularized linear regression 4&#xff09;Regularized logistic regression 我们已经学习了线性回归和逻辑回归算法&#xff0c;已经可以有效解决很多问题&#xff0c;但是在实…

Hbase单节点安装

zookeeper单节点部署 实验环境 操作系统&#xff1a;Ubuntu 16.04 Hadoop&#xff1a;Hadoop 2.7.5 Zookeeper&#xff1a;zookeeper 3.4.12 Java&#xff1a;java version 1.8.0 到/install-package目录下查看hbase安装包 #>ls /install-package本文中用的是hbase-1…

ROS 常用命令字典

版权声明&#xff1a;本文为博主原创文章&#xff0c;转载请标明出处: http://www.cnblogs.com/liu-fa/p/5761448.html 该博文适合已经具备一定的ROS编程基础的人&#xff0c;快速查看ROS相关指令。 本文持续更新中&#xff0c;望关注收藏&#xff0c;一起改进... 创建 ROS 工作…

【HDU - 3966】Aragorn's Story(树链剖分,模板题)

题干&#xff1a; Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who want to invade his kingdom. As Aragorn knows, the enemy has N camps out of his kingdom and M edges connect t…

机器学习笔记(七):神经网络:表示

目录 1&#xff09;Non-linear hypotheses 2&#xff09;Model representation 1 3&#xff09;Model representation 2 4&#xff09;Examples and intuitions 1 5&#xff09;Examples and intuitions 2 6&#xff09;Multi-class classification 1&#xff09;Non-lin…

ROS入门_1.10 理解ROS服务和参数

目录 ROS Services使用rosservice rosservice listrosservice typerosservice call Using rosparam rosparam listrosparam set and rosparam getrosparam dump and rosparam load 本教程假设从前一教程启动的turtlesim_node仍在运行&#xff0c;现在我们来看看turtlesim提供了…

1.Introduction and Evaluation

感谢七月在线罗老师和吴同学&#xff01; 最近报了七月在线的《推荐系统实战》班&#xff0c;根据上课资料和思维导图整理了这篇笔记&#xff01; 1&#xff09;推荐系统介绍 思维导图如下&#xff0c;其中需要掌握的是推荐系统存在的前提&#xff1a;信息过载和用户需求不明…

【ZOJ - 2968 】Difference Game (贪心,思维模拟)

题干&#xff1a; Now you are going to play an interesting game. In this game, you are given two groups of distinct integers and C coins. The two groups, named Ga and Gbrespectively, are not empty and contain the same number of integers at first. Each time…

使用 rqt_console 和 roslaunch

Description:本教程介绍如何使用 rqt_console 和 rqt_logger_level 进行调试&#xff0c;以及如何使用 roslaunch 同时运行多个节点。早期版本中的 rqt 工具并不完善&#xff0c;因此&#xff0c;如果你使用的是“ROS fuerte”或更早期的版本&#xff0c;请同时参考 这个页面 学…

机器学习必备宝典-《统计学习方法》的python代码实现、电子书及课件

本文转自微信公众号&#xff1a;机器学习初学者 原创&#xff1a; 机器学习初学者 机器学习初学者 6天前 《统计学习方法》可以说是机器学习的入门宝典&#xff0c;许多机器学习培训班、互联网企业的面试、笔试题目&#xff0c;很多都参考这本书。本站根据网上资料用python复现…

【2019牛客暑期多校训练营(第一场) - H】XOR(线性基,期望的线性性)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/881/H 来源&#xff1a;牛客网 Bobo has a set A of n integers a1,a2,…,ana1,a2,…,an. He wants to know the sum of sizes for all subsets of A whose xor sum is zero modulo (1097)(1097). F…

机器学习入门必备的13张“小抄”(附下载)

目录 1&#xff09;TensorFlow 2&#xff09;Keras 3&#xff09;Neural Networks 4&#xff09;Numpy 5&#xff09;Scipy 6&#xff09;Pandas 7&#xff09;Scikit-learn 8&#xff09;Matplotlib 9&#xff09;PythonForDataScience 最近在github上发现了很有用的…

ROS launch文档介绍

本文章转自&#xff1a;https://charlyhuangrostutorial.wordpress.com/2015/08/12/20/ 前面已经提过关于launch 档的角色&#xff0c;很类似bash 档&#xff0c;基本上就是把所有为了执行某个特定功能所需要的指令都写在一张纸上&#xff0c;交给ROS 一次执行开来。举例来说&a…

【2019牛客暑期多校训练营(第一场) - A】Equivalent Prefixes(单调栈,tricks)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/881/A 来源&#xff1a;牛客网 Two arrays u and v each with m distinct elements are called equivalent if and only if RMQ(u,l,r)RMQ(v,l,r) for all 1≤l≤r≤m where RMQ(w,l,r) denotes th…

吴恩达机器学习作业(1):线性回归

目录 1&#xff09;导入相关库和数据 2&#xff09;代价函数 3&#xff09;批量梯度下降 4&#xff09;绘制线性模型 前阵子在网易云课堂学习了吴恩达老师的机器学习课程&#xff0c;今天结合网上资料&#xff0c;用Python实现了线性回归作业&#xff0c;共勉。建议大家使…

【HDU - 5886】Tower Defence(树的直径,思维,dp)

题干&#xff1a; There was a civil war between two factions in Skyrim, a province of the Empire on the continent of Tamriel. The Stormcloaks, led by Ulfric Stormcloak, are made up of Skyrims native Nord race. Their goal is an independent Skyrim free from …