UVALive - 3523 - Knights of the Round Table
Time Limit: 4500 mSec
Problem Description

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
Sample Input
Sample Output
2
题解:双连通分量+二分图性质及判断,首先抽象出本题的模型,可以相邻而坐的骑士之间连一条边,对于简单奇环中的每一个骑士都可以参加会议,而不被任意一个简单奇环包含的骑士不能参加会议,因此此题就是统计不在任意一个简单奇环中的节点个数,做出这个题首先需要知道两个知识点
一、二分图中必定没有奇环
二、点双连通分量是满足如下性质:任意两条边都在同一个简单环中
通常来说如果要求任意两条边在同一个简单环中,那么就是求点-双连通,因此此题就相当于求完点双连通之后判断一下每个双连通分量是否是二分图,其实还需要证明一个东西就是如果该双连通分量不是二分图,那么其中每一个节点都会存在于某个奇环内,证明很简单不赘述。有了这些理论基础之后其实就是两个模板放在一起即可。
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 1; i <= (n); i++)
#define sqr(x) ((x) * (x)) const int maxn = + ;
const int maxm = + ;
const int maxs = + ; typedef long long LL;
typedef pair<int, int> pii;
typedef pair<double, double> pdd; const LL unit = 1LL;
const int INF = 0x3f3f3f3f;
const LL mod = ;
const double eps = 1e-;
const double inf = 1e15;
const double pi = acos(-1.0); struct Edge
{
int u, v;
}; int gra[maxn][maxn];
int n, m;
int dfs_clock, bcc_cnt, bccno[maxn], pre[maxn], is_cut[maxn];
int color[maxn], odd[maxn];
vector<int> G[maxn], bcc[maxn]; stack<Edge> S; int dfs(int u, int fa)
{
int lowu = pre[u] = ++dfs_clock;
int child = ;
for (auto v : G[u])
{
Edge e = (Edge){u, v};
if (!pre[v])
{
S.push(e);
child++;
int lowv = dfs(v, u);
lowu = min(lowu, lowv);
if (lowv >= pre[u])
{
is_cut[u] = ;
bcc_cnt++;
bcc[bcc_cnt].clear();
for (;;)
{
Edge x = S.top();
S.pop();
if (bccno[x.u] != bcc_cnt)
{
bcc[bcc_cnt].push_back(x.u);
bccno[x.u] = bcc_cnt;
}
if (bccno[x.v] != bcc_cnt)
{
bcc[bcc_cnt].push_back(x.v);
bccno[x.v] = bcc_cnt;
}
if (x.u == u && x.v == v)
{
break;
}
}
}
}
else if (pre[v] < pre[u] && v != fa)
{
S.push(e);
lowu = min(lowu, pre[v]);
}
}
if (fa < && child == )
is_cut[u] = ;
return lowu;
} void find_bcc()
{
memset(pre, , sizeof(pre));
memset(is_cut, , sizeof(is_cut));
memset(bccno, , sizeof(bccno));
dfs_clock = bcc_cnt = ;
for (int i = ; i < n; i++)
{
if (!pre[i])
{
dfs(i, -);
}
}
} bool bipartite(int u, int cnt)
{
for (auto v : G[u])
{
if (bccno[v] != cnt)
continue;
if (color[v] == color[u])
{
return false;
}
if (!color[v])
{
color[v] = - color[u];
if (!bipartite(v, cnt))
{
return false;
}
}
}
return true;
} int main()
{
ios::sync_with_stdio(false);
cin.tie();
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
while (cin >> n >> m && (n || m))
{
for (int i = ; i < n; i++)
{
bcc[i].clear();
G[i].clear();
}
memset(gra, , sizeof(gra));
int u, v;
for (int i = ; i < m; i++)
{
cin >> u >> v;
u--, v--;
gra[u][v] = gra[v][u] = ;
}
for (int i = ; i < n; i++)
{
for (int j = i + ; j < n; j++)
{
if (!gra[i][j])
{
G[i].push_back(j);
G[j].push_back(i);
}
}
}
find_bcc();
memset(odd, , sizeof(odd));
for (int i = ; i <= bcc_cnt; i++)
{
for (auto v : bcc[i])
{
bccno[v] = i;
}
memset(color, , sizeof(color));
int u = bcc[i][];
color[u] = ;
if (!bipartite(u, i))
{
for (auto v : bcc[i])
{
odd[v] = ;
}
}
}
int ans = n;
for (int i = ; i < n; i++)
{
if (odd[i])
ans--;
}
cout << ans << endl;
}
return ;
}
UVALive - 3523 - Knights of the Round Table的更多相关文章
- uvalive 3523 Knights of the Round Table 圆桌骑士(强连通+二分图)
题目真心分析不出来.看了白书才明白,不过有点绕脑. 容易想到,把题目给的不相邻的关系,利用矩阵,反过来建图.既然是全部可行的关系,那么就应该能画出含奇数个点的环.求环即是求双连通分量:找出所有的双连通 ...
- UVALive 3523 Knights of the Round Table 圆桌骑士 (无向图点双连通分量)
由于互相憎恨的骑士不能相邻,把可以相邻的骑士连上无向边,会议要求是奇数,问题就是求不在任意一个简单奇圈上的结点个数. 如果不是二分图,一定存在一个奇圈,同一个双连通分量中其它点一定可以加入奇圈.很明显 ...
- UVALive 3523 : Knights of the Round Table (二分图+BCC)
题目链接 题意及题解参见lrj训练指南 #include<bits/stdc++.h> using namespace std; ; int n,m; int dfn[maxn],low[ ...
- uva 3523 Knights of the Round Table
题意:给你n,m n为有多少人,m为有多少组关系,每组关系代表两人相互憎恨,问有多少个骑士不能参加任何一个会议. 白书算法指南 对于每个双联通分量,若不是二分图,就把里面的节点标记 #include ...
- 【LA3523】 Knights of the Round Table (点双连通分量+染色问题?)
Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress ...
- POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 12439 Acce ...
- POJ 2942 Knights of the Round Table
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 10911 Acce ...
- poj 2942 Knights of the Round Table 圆桌骑士(双连通分量模板题)
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 9169 Accep ...
- POJ 2942 Knights of the Round Table - from lanshui_Yang
Description Being a knight is a very attractive career: searching for the Holy Grail, saving damsels ...
随机推荐
- Java基础2:基本数据类型与常量池
本文会结合虚拟机对引用和对象的不同处理来介绍三大特性的原理. 三大特性:继承 封装 多态 继承 Java中的继承只能单继承,但是可以通过内部类继承其他类来实现多继承. public class Son ...
- Python 中包/模块的 `import` 操作
版权声明:博客为作者原创,允许转载,但必须注明原文地址: https://www.cnblogs.com/byronxie/p/10745292.html 用实例来说明 import 的作用吧. 创建 ...
- linux下(fdisk,gdisk,parted)三种分区工具比较
1 2种分区结构简介 MBR分区 硬盘主引导记录MBR由4个部分组成 主引导程序(偏移地址0000H--0088H),它负责从活动分区中装载,并运行系统引导程序. 出错信息数据区,偏移地址0089H- ...
- iBinary C++STL模板库关联容器之map/multimap
目录 一丶关联容器map/multimap 容器 二丶代码例子 1.map的三种插入数据的方法 3.map集合的遍历 4.验证map集合数据是否插入成功 5.map数据的查找 6.Map集合删除元素以 ...
- 克拉克拉(KilaKila):大规模实时计算平台架构实战
克拉克拉(KilaKila):大规模实时计算平台架构实战 一.产品背景:克拉克拉(KilaKila)是国内专注二次元.主打年轻用户的娱乐互动内容社区软件.KilaKila推出互动语音直播.短视频配音. ...
- Linux之安装常用软件
Linux下安装软件的方法: 1,rpm(不推荐使用) 2,yum安装(使用快捷方便) 3,编译安装 一.安装python3(这里使用的是编译安装) 1,下载python3源码包 在centos下,第 ...
- 大前端的自动化工厂(2)—— SB Family
原文链接:https://bbs.huaweicloud.com/blogs/53c0c3509b7a11e89fc57ca23e93a89f 我坦白我是标题党,SB只是SCSS-Bourbon的简写 ...
- WebClient下载文件
public void DownDile(string url) { WebClient client = new WebClient(); string URLAddress = @"ht ...
- C#生成随机数的三种方法
随机数的定义为:产生的所有数字毫无关系. 在实际应用中很多地方会用到随机数,比如需要生成唯一的订单号. 在C#中获取随机数有三种方法: 一.Random 类 Random类默认的无参构造函数可以根据当 ...
- Java开发笔记(二十一)二维数组的扩展
前面介绍的数组容纳的是一串数字,仿佛一根线把这组数字串了起来,故而它只是一维数组.一维数组用来表示简单的数列尚可,要是表达复杂的平面坐标系,那就力不从心了.由于平面坐标系存在水平和垂直两个方向,因此可 ...