先放这吧,没时间写,明天再补

“明天到了”

题目链接

题意:求不在任何奇环内的点的数量。

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(双联通分量)的更多相关文章

  1. Spoj 2878 KNIGHTS - Knights of the Round Table | 双联通分量 二分图判定

    题目链接 考虑建立原图的补图,即如果两个骑士不互相憎恨,就在他们之间连一条无向边. 显而易见的是,如果若干个骑士在同一个点数为奇数的环上时,他们就可以在一起开会.换句话说,如果一个骑士被一个奇环包含, ...

  2. POJ2942 UVA1364 Knights of the Round Table 圆桌骑士

    POJ2942 洛谷UVA1364(博主没有翻墙uva实在是太慢了) 以骑士为结点建立无向图,两个骑士间存在边表示两个骑士可以相邻(用邻接矩阵存图,初始化全为1,读入一对憎恨关系就删去一条边即可),则 ...

  3. 「题解」:[POJ2942]Knights of the Round Table

    问题 E: Knights of the Round Table 时间限制: 1 Sec  内存限制: 256 MB 题面 题目描述 作为一名骑士是一个非常有吸引力的职业:寻找圣杯,拯救遇难的少女,与 ...

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

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

  5. POJ 2942 Knights of the Round Table

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

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

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

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

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

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

  9. UVALive - 3523 - Knights of the Round Table

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

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

随机推荐

  1. Django笔记 —— 高级视图和URL配置

    最近在学习Django,打算玩玩网页后台方面的东西,因为一直很好奇但却没怎么接触过.Django对我来说是一个全新的内容,思路想来也是全新的,或许并不能写得很明白,所以大家就凑合着看吧- 本篇笔记(其 ...

  2. js解决img标签加载失败显示默认图片

    问题: 为所有显示楼盘的页面添加一个加载失败的默认图片. 基本思路: img标签中有个onerror属性,专门用来处理加载失败的事件.所以可以用jquery添加onerror属性,在onerror中加 ...

  3. KVM web管理工具——WebVirtMgr(一)

    WebVirtMgr 介绍     WebVirtMgr采用几乎纯Python开发,其前端是基于Python的Django,后端是基于Libvirt的Python接口,将日常kvm的管理操作变的更加的 ...

  4. 剑指offer-数值的整数次方12

    class Solution: def Power(self, base, exponent): # write code here if base==0: return 0 if exponent= ...

  5. 更换ubuntu软件源的方法

    第一步:查看本系统Codename 输入lsb_release -a查看本系统Codename,我的codename是bionic,如图: 第二步:搜索与codename对应的镜像地址 我搜索到的是: ...

  6. 基于语音转录的ted演讲推荐

    论文地址:https://arxiv.org/abs/1809.05350v1 二.  实现 我们从Kaggle[6]中获取了TED演讲数据集,其中包括2400个TED演讲的数据,包括标题.演讲者.标 ...

  7. 为DEV C++/CodeBlock配置gdb调试遇到的问题

    DEV C++和CodeBlock都只是一个IDE,不能编译调试,需要自己配置MINGW和gdb调试 1.MINGW 在这下载mingw-get-setup.exe安装即可. https://sour ...

  8. DPDK Qos之报文处理流水线

    原创翻译,转载请注明出处. 下面是一个支持Qos的复杂报文处理流水线的图: 流水线是通过DPDP可重用的软件库构建出来的.在流水线里实现QoS主要是如下模块:policer,dropper,shced ...

  9. 【python爬虫】对喜马拉雅上一个专辑的音频进行爬取并保存到本地

    >>>内容基本框架: 1.爬虫目的 2.爬取过程 3.代码实现 4.爬取结果  >>>实验环境: python3.6版本,pycharm,电脑可上网. [一 爬虫目 ...

  10. [剑指Offer] 41.和为S的连续正数序列

    题目描述 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没多久,他 ...