Problem  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

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

题解:双连通分量+二分图性质及判断,首先抽象出本题的模型,可以相邻而坐的骑士之间连一条边,对于简单奇环中的每一个骑士都可以参加会议,而不被任意一个简单奇环包含的骑士不能参加会议,因此此题就是统计不在任意一个简单奇环中的节点个数,做出这个题首先需要知道两个知识点

  一、二分图中必定没有奇环

  二、点双连通分量是满足如下性质:任意两条边都在同一个简单环中

通常来说如果要求任意两条边在同一个简单环中,那么就是求点-双连通,因此此题就相当于求完点双连通之后判断一下每个双连通分量是否是二分图,其实还需要证明一个东西就是如果该双连通分量不是二分图,那么其中每一个节点都会存在于某个奇环内,证明很简单不赘述。有了这些理论基础之后其实就是两个模板放在一起即可。

 #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的更多相关文章

  1. uvalive 3523 Knights of the Round Table 圆桌骑士(强连通+二分图)

    题目真心分析不出来.看了白书才明白,不过有点绕脑. 容易想到,把题目给的不相邻的关系,利用矩阵,反过来建图.既然是全部可行的关系,那么就应该能画出含奇数个点的环.求环即是求双连通分量:找出所有的双连通 ...

  2. UVALive 3523 Knights of the Round Table 圆桌骑士 (无向图点双连通分量)

    由于互相憎恨的骑士不能相邻,把可以相邻的骑士连上无向边,会议要求是奇数,问题就是求不在任意一个简单奇圈上的结点个数. 如果不是二分图,一定存在一个奇圈,同一个双连通分量中其它点一定可以加入奇圈.很明显 ...

  3. UVALive 3523 : Knights of the Round Table (二分图+BCC)

    题目链接 题意及题解参见lrj训练指南 #include<bits/stdc++.h> using namespace std; ; int n,m; int dfn[maxn],low[ ...

  4. uva 3523 Knights of the Round Table

    题意:给你n,m n为有多少人,m为有多少组关系,每组关系代表两人相互憎恨,问有多少个骑士不能参加任何一个会议. 白书算法指南 对于每个双联通分量,若不是二分图,就把里面的节点标记 #include ...

  5. 【LA3523】 Knights of the Round Table (点双连通分量+染色问题?)

    Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress ...

  6. POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]

    Knights of the Round Table Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 12439   Acce ...

  7. POJ 2942 Knights of the Round Table

    Knights of the Round Table Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 10911   Acce ...

  8. poj 2942 Knights of the Round Table 圆桌骑士(双连通分量模板题)

    Knights of the Round Table Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 9169   Accep ...

  9. 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 ...

随机推荐

  1. Java基础19:Java集合框架梳理

    更多内容请关注微信公众号[Java技术江湖] 这是一位阿里 Java 工程师的技术小站,作者黄小斜,专注 Java 相关技术:SSM.SpringBoot.MySQL.分布式.中间件.集群.Linux ...

  2. Linux基础知识第一讲,基本目录结构与基本命令

    目录 一丶Window 与 Linux的目录结构 1.Windows 与 Linux目录简介 2.Linux目录主要作用 3.任务栏与菜单栏,与关闭按钮 二丶Linux终端与常见命令学习 1.终端中的 ...

  3. Django学习笔记(9)—— 开发用户注册与登录系统

    一,项目题目: 开发用户注册与登录系统 该项目主要练习使用Django开发一个用户注册与登录的系统,通过这个项目然后巩固自己这段时间所学习的Django知识. 二,项目需求: 开发一个简单的用户登录与 ...

  4. 再探go modules:使用与细节

    还有半个月go1.12就要发布了.这是首个将go modules纳入正式支持的稳定版本. 距离go modules随着go1.11正式面向广大开发者进行体验也已经过去了半年,这段时间go module ...

  5. 第27章 联合网关 - Identity Server 4 中文文档(v1.0.0)

    通用架构是所谓的联合网关.在此方法中,IdentityServer充当一个或多个外部身份提供商的网关. 该架构具有以下优点: 您的应用程序只需要了解一个令牌服务(网关),并且屏蔽了有关连接到外部提供程 ...

  6. ORA-01940 无法删除当前已连接的用户之解决方案

    在执行drop user的时候,提示报错信息:ORA-01940: cannot drop a user that is currently connected SQL> drop user l ...

  7. C# 如何添加Excel页眉页脚(图片、文字、奇偶页不同)

    简介 我们可以通过代码编程来对Excel工作表实现很多操作,在下面的示例中,将介绍如何来添加Excel页眉.页脚.在页眉处,我们可以添加文字,如公司名称.页码.工作表名.日期等,也可以添加图片,如LO ...

  8. 【转】Android必备知识点- Android文件(File)操作

    Android 使用与其他平台上基于磁盘的文件系统类似的文件系统. 本文讲述如何使用 Android 文件系统通过 File API 读取和写入文件. File 对象适合按照从开始到结束的顺序不跳过地 ...

  9. JAVA Swing 改变标题栏左上角默认咖啡图标

    前言 最近使用Java的swing开发了一个小程序,想要实现改变标题栏左上角的图标,找了网上的资料,经过了一个下午的尝试,都是未能成功,最后,终于是在Java的一本书上找到了结果 我只能说,网上的东西 ...

  10. PHP基础:MYSQL数据库操作

    1.连接到数据库: · 面向对象的方法: $db = new mysqli('hostname', 'username', 'password', 'dbname'); · 面向过程的方法: $db ...