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

“明天到了”

题目链接

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

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. Java与C++进行系统间交互:Protocol Buffer

    在一次项目中,因笔者负责的java端应用需要与公司C++系统进行交互,公司选定Protocol Buffer方案,故简单的了解一下 有需要的可以看一下其他作者的文章,了解一下Protobuf: htt ...

  2. Jmeter学习(三)

    Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试,它最初被设计用于Web应用测试,但后来扩展到其他测试.(来自百度) jmeter的特点: 开源免费. ...

  3. React开发时候注意点

    JSX 使用jsx的使用,用一个{}包裹起来,例如 const method = {<div> 123 </div>} 使用()小括号,防止分号自动插入 const eleme ...

  4. P2384洛谷 最短路

    题目描述 给定n个点的带权有向图,求从1到n的路径中边权之积最小的简单路径. 输入输出格式 输入格式: 第一行读入两个整数n,m,表示共n个点m条边. 接下来m行,每行三个正整数x,y,z,表示点x到 ...

  5. LeetCode 389——找不同

    1. 题目 2. 解答 2.1. 方法一 将 s 和 t 转化为 Python 的列表,然后遍历列表 s 的元素,将它们从列表 t 中删除,最后列表 t 中会余下一个元素,即为所求. class So ...

  6. [leetcode-655-Print Binary Tree]

    Print a binary tree in an m*n 2D string array following these rules: The row number m should be equa ...

  7. AMF3 在Unity中使用AMF3和Java服务器通信

    现在在做的项目是一个网页游戏的移植到移动端. 所以服务器直接使用原来的代码.原来的游戏是as3实现,使用flash amf3数据通信. Unity 使用C#作为脚本语言,所以就需要.net的amf3解 ...

  8. Python如何进行中文注释

    最近,由于实习工作的需要,开始接触Python,但是第一个大的脚本写下来之后,连中文注释都没办法加,很郁闷,遂在网上找解决办法,在Python 官网上看到这个页面:http://www.python. ...

  9. C语言分支/顺序作业总结

    总结 1.1 基本要求(1分) 按时交 - 有分 未交 - 0分 迟交一周以上 - 倒扣本次作业分数 抄袭 - 0分 博客作业格式不规范,没有用Markdown语法 -扣分 泛泛而谈(最多七分) 1. ...

  10. JavaScript五种继承方式详解

    本文抄袭仅供学习http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance.html 一. 构造函数绑定 ...