【洛谷 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 ...
随机推荐
- vue3.0 部署的基础流程
1.创建vue.config.js 主要是负责做设置的 2.修改vue.config.js 参考官方说明: 注意:对于本地开发的同学要注意,你之前在处理网络请求时是在8080端口下请求,现在如果换成了 ...
- python学习总结----简单数据结构
mini-web服务器 - 能够完成简单的请求处理 - 使用http协议 - 目的:加深对网络编程的认识.为后面阶段学习web做铺垫 简单数据结构 - 排列组合 import itertools # ...
- Linux服务架设篇--ping命令
工作原理: 向远程机发送包含一定字节数的ICMP数据包,如果能收到对方的回复的数据包,就表明网络是相通的,而且根据两个数据包的时间差,还可以知道相互之间网络链接的速度. 注意: 有些远程主机由于某种原 ...
- markdown(自己看)
https://www.cnblogs.com/james-lee/p/6847906.html https://maxiang.io/
- Mybatis学习系列(七)缓存机制
Mybatis缓存介绍 MyBatis提供一级缓存和二级缓存机制. 一级缓存是Sqlsession级别的缓存,Sqlsession类的实例对象中有一个hashmap用于缓存数据.不同的Sqlsessi ...
- JMS实战——ActiveMQ
安装 官网下载地址:http://activemq.apache.org/ 小编这里以5.9.0版本为例,做简单介绍. 下载之后解压到制定路径,目录结构如下: 启动 直接运行bin下的activemq ...
- java有几种对象(PO,VO,DAO,BO,POJO)
首先,java有几种对象(PO,VO,DAO,BO,POJO) 一.PO:persistant object 持久对象,可以看成是与数据库中的表相映射的java对象.使用Hibernate来生成PO是 ...
- 【EasyNetQ】- 发布确认
默认的AMQP发布不是事务性的,并不保证您的消息实际到达代理.AMQP确实指定了事务发布,但是使用RabbitMQ它非常慢,我们还没有通过EasyNetQ API支持它.对于高性能保证交付,建议您使用 ...
- 利用Github Pages建立仓库“门面”
嘛是Github Pages? Github Pages 是一个静态网站托管服务,用来从你的Github仓库中直接发布 个人.组织或项目的网站页面 Github Pages发布的页面统一使用githu ...
- WebService使用介绍(三)
jax-ws开发深入 JAX-WS注解 注解说明 WebService的注解都位于javax.jws包下: @WebService-定义服务,在public class上边 targetNamespa ...