【洛谷 SP2878】Knights of the Round Table(双联通分量)
先放这吧,没时间写,明天再补
“明天到了”
题目链接
题意:求不在任何奇环内的点的数量。
Tarjan求点双联通分量,然后再染色判断是不是二分图就好了。
只是不懂为什么Tarjan求双联通分量时要用栈保存点对,希望大佬留言帮助。
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
inline int min(int a, int b){
return a > b ? b : a;
}
inline int max(int a, int b){
return a > b ? a : b;
}
inline int read(){
int s = 0, w = 1;
char ch = getchar();
while(ch < '0' || ch > '9') { if(ch == '-') w = -1; ch = getchar(); }
while(ch >= '0' && ch <= '9') { s = s * 10 + ch - '0'; ch = getchar(); }
return s * w;
}
const int MAXN = 1010;
const int MAXM = 1000010;
int n, m;
int a, b;
int s[MAXN][MAXN];
struct Edge{
int next, to;
}e[MAXM << 1];
int head[MAXN], num;
inline void Add(int from, int to){
e[++num].to = to;
e[num].next = head[from];
head[from] = num;
}
int dfn[MAXN], low[MAXN], ID, color[MAXN], can[MAXN], belong[MAXN], top, ans, cnt;
struct point{
int u, v;
}stack[MAXN << 2];
vector <int> dcc[MAXN];
bool Judge(int u, int Color, int now){
color[u] = Color;
for(int i = head[u]; i; i = e[i].next){
if(belong[e[i].to] != now) continue;
if(!color[e[i].to])
if(Judge(e[i].to, 3 - Color, now)) return true;
else;
else
if(color[e[i].to] == color[u])
return true;
}
return false;
}
void Tarjan(int u, int fa){
dfn[u] = low[u] = ++ID;
for(int i = head[u]; i; i = e[i].next){
if(!dfn[e[i].to]){
stack[++top] = (point){ u, e[i].to };
Tarjan(e[i].to, u);
low[u] = min(low[u], low[e[i].to]);
if(low[e[i].to] >= dfn[u]){
dcc[++cnt].clear();
point now;
do{
now = stack[top--];
if(belong[now.u] != cnt) belong[now.u] = cnt, dcc[cnt].push_back(now.u);
if(belong[now.v] != cnt) belong[now.v] = cnt, dcc[cnt].push_back(now.v);
}while(now.u != u || now.v != e[i].to);
}
}
else if(dfn[e[i].to] < dfn[u] && e[i].to != fa)
stack[++top] = (point){ u, e[i].to }, low[u] = min(low[u], dfn[e[i].to]);
}
}
int main(){
while(233){
n = read(); m = read();
if(!n && !m) break;
memset(s, 0, sizeof s);
memset(dfn, 0, sizeof dfn);
memset(low, 0, sizeof low);
memset(can, 0, sizeof can);
memset(head, 0, sizeof head);
memset(color, 0, sizeof color);
memset(belong, 0, sizeof belong);
ID = ans = top = num = cnt = 0;
for(int i = 1; i <= m; ++i){
a = read(); b = read();
s[a][b] = s[b][a] = 1;
}
for(int i = 1; i < n; ++i)
for(int j = i + 1; j <= n; ++j)
if(!s[i][j])
Add(i, j), Add(j, i);
for(int i = 1; i <= n; ++i)
if(!dfn[i])
Tarjan(i, 0);
for(int i = 1; i <= cnt; ++i){
memset(color, 0, sizeof color);
for(int j = 0; j < dcc[i].size(); ++j)
belong[dcc[i][j]] = i;
if(Judge(dcc[i][0], 1, i)){
for(int j = 0; j < dcc[i].size(); ++j)
can[dcc[i][j]] = 1;
}
}
for(int i = 1; i <= n; ++i)
if(!can[i])
++ans;
printf("%d\n", ans);
}
return 0;
}
【洛谷 SP2878】Knights of the Round Table(双联通分量)的更多相关文章
- Spoj 2878 KNIGHTS - Knights of the Round Table | 双联通分量 二分图判定
题目链接 考虑建立原图的补图,即如果两个骑士不互相憎恨,就在他们之间连一条无向边. 显而易见的是,如果若干个骑士在同一个点数为奇数的环上时,他们就可以在一起开会.换句话说,如果一个骑士被一个奇环包含, ...
- POJ2942 UVA1364 Knights of the Round Table 圆桌骑士
POJ2942 洛谷UVA1364(博主没有翻墙uva实在是太慢了) 以骑士为结点建立无向图,两个骑士间存在边表示两个骑士可以相邻(用邻接矩阵存图,初始化全为1,读入一对憎恨关系就删去一条边即可),则 ...
- 「题解」:[POJ2942]Knights of the Round Table
问题 E: Knights of the Round Table 时间限制: 1 Sec 内存限制: 256 MB 题面 题目描述 作为一名骑士是一个非常有吸引力的职业:寻找圣杯,拯救遇难的少女,与 ...
- 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 ...
- 【LA3523】 Knights of the Round Table (点双连通分量+染色问题?)
Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress ...
- 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 ...
- UVALive - 3523 - Knights of the Round Table
Problem UVALive - 3523 - Knights of the Round Table Time Limit: 4500 mSec Problem Description Input ...
- poj 2942 Knights of the Round Table - Tarjan
Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress ...
随机推荐
- mysql 大数据分页查询优化
应用场景: 当有一张表的数据非常大,需要使用到分页查询,分页查询在100w条后查询效率非常低: 解决方案: 1.业务层解决:只允许用户翻页一百页以内,十条一页: 2.使用where id > 5 ...
- 《.NET 微服务:适用于容器化 .NET 应用的体系结构》关键结论
作为总结和要点,以下是本指南中最重要的结论.1 使用容器的好处: 基于容器的解决方案有节约成本的好处,因为容器是针对生产环境中缺少依赖而导致的部署问题提出的解决方案.容器能够显著改善devops和生产 ...
- Scala学习笔记(三):基础知识
有了可运行的环境,就需要写一些简单的语句来认识一下Scala,本文没有写那么详细,只是为了方便查看.唤起回忆 (1)变量的定义方法 Scala有两种变量 var val 注意:在解释器中,可以用一个之 ...
- 在Linux下通过rpm打包发布Java程序
这个东西涉及的内容较多,根据下面这些文章慢慢学习 一个简单的例子 http://blog.csdn.net/king_on/article/details/7169384 按照文章中的步骤来,打包之后 ...
- Java日志(一):log4j与.properties配置文件
日志是应用软件中不可缺少的部分,Apache的开源项目log4j是一个功能强大的日志组件,提供方便的日志记录,在Apache网站jakarta.apache.org/log4j可以免费下载到Log4j ...
- springmvc基础篇—修改默认的配置文件名称及位置
springmvc的默认配置文件是放在WEB-INF下的,叫action-servlet.xml.根据咱们编程的习惯,一般都将配置文件放到src的根目录下,那么如何将这个文件迁移过来呢?其实很简单,请 ...
- Java泛型的基本介绍与使用
为什么要使用泛型? 在Java中增加泛型之前,泛型程序设计是用继承来实现的,例如ArrayList,只维护Object引用的数组: public class ArrayList{ private Ob ...
- C++学习014函数值传递和地址传递
当我们给一个函数传参数的时候,可以直接值传入函数,也给可以把一个地址传入函数 区别就是一个本身不被改变,而另一本身也在改变, 在开发时候都会用到, 这里做下记录 #include <iostre ...
- 第二十五篇 hashlib模块(* *)
用于加密相关的操作,Python 3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法. 加密复杂程度: SHA1 ...
- Jmeter使用时异常问题解决
1.执行jmeter请求时,响应数据中出现乱码异常(如图) 解决方案: 打开E:\apache-jmeter-4\bin\jmeter.properries(jmeter安装目录),查找到语句行:#s ...