题目链接

题意及题解参见lrj训练指南

#include<bits/stdc++.h>
using namespace std; const int maxn=1e3+;
int n,m;
int dfn[maxn],low[maxn],time_tag;
int bccno[maxn],bcc_cnt;
int iscut[maxn];
int A[maxn][maxn];
vector<int> adj[maxn];
vector<int> bcc[maxn];
int odd[maxn];
struct Edge
{
int u,v;
Edge() {}
Edge(int u_,int v_)
{
u=u_,v=v_;
}
};
stack<Edge> st;
void init()
{
memset(A,,sizeof(A));
memset(dfn,,sizeof(dfn));
memset(iscut,,sizeof(iscut));
memset(bccno,,sizeof(bccno));
memset(odd,,sizeof(odd));
for(int i=; i<=n; i++)
adj[i].clear(),bcc[i].clear();
bcc_cnt=time_tag=;
}
void dfs(int u,int pre)
{
low[u]=dfn[u]=++time_tag;
int child=; //子节点数目
for(int v:adj[u])
{
if(v==pre) continue;
if(!dfn[v]) // 把dfn[]当vis[]使用
{
st.push(Edge(u,v));
child++;
dfs(v,u);
low[u]=min(low[u],low[v]);
if(low[v]>=dfn[u])
{
iscut[u]=;
bcc_cnt++;
while()
{
Edge x=st.top();st.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(dfn[v]<dfn[u])
{
st.push(Edge(u,v));
low[u]=min(low[u],dfn[v]);
}
}
if(pre<&&child==) iscut[u]=; //只有一个孩子的根节点
}
void find_bcc()
{
for(int i=;i<n;i++)
if(!dfn[i]) dfs(i,-);
} int color[maxn];
//判定结点u所在的连通分量是否为二分图
bool bipartite(int u,int tag)
{
for(int v:adj[u])
{
if(bccno[v]!=tag) continue;
if(color[v]==color[u]) return false;
if(!color[v])
{
color[v]=-color[u];
if(!bipartite(v,tag)) return false;
}
}
return true;
} int main()
{
while(scanf("%d%d",&n,&m)>&&n)
{
init();
for(int i=;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
u--,v--;
A[u][v]=A[v][u]=;
}
for(int u=;u<n;u++)
for(int v=u+;v<n;v++)
if(!A[u][v]) adj[u].push_back(v),adj[v].push_back(u);
find_bcc();
for(int i=;i<=bcc_cnt;i++)
{
memset(color,,sizeof(color));
for(int u:bcc[i]) bccno[u]=i;
int u=bcc[i][];
color[u]=;
if(!bipartite(u,i))
for(int v:bcc[i]) odd[v]=;
}
int ans=n;
for(int i=;i<n;i++) if(odd[i]) ans--;
printf("%d\n",ans);
}
}

UVALive 3523 : Knights of the Round Table (二分图+BCC)的更多相关文章

  1. UVALive - 3523 - Knights of the Round Table

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

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

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

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

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

  4. uva 3523 Knights of the Round Table

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

  5. 【POJ2942】Knights of the Round Table(二分图 点双联通分量)

    题目链接 大意 给定\(N\)个点与\(M\)个关系,每个关系表示某两个点间没有直接的边相连,求不在所有奇环上的点的个数. (\(1\le N\le 1e3,1\le M\le 1e6\)) 思路 考 ...

  6. UVAlive3523 Knights of the Round Table(bcc)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18122 [思路] 点-双连通分量 求出bcc,判断每个bcc是否为 ...

  7. UVA-1364.Knights of the Round Table 无向图BCC

    题目链接:https://vjudge.net/problem/UVA-1364 题意:有n个人参加会议,互相憎恨的人不能坐在相邻的位置,并且每个会议参加的人数必须是奇数,求有多少个人不能参加任何一个 ...

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

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

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

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

随机推荐

  1. jmeter响应数据Unicode编码转换为汉字

    2018-07-09     10:24:34 每次用jmeter做接口测试时,响应信息中文总是显示Unicode编码格式,每次都要在网上寻找这一段转换的代码,但是我发现在网上找这段代码有点麻烦,像我 ...

  2. mysql 8.0.12 安装配置方法图文教程

    一.安装 1.从网上下载MySQL8.0.12版本,下载地址 2. 下载完成后解压 我解压的路径是:D:\Java\mysql-8.0.12-winx64 3. 配置文件 首先在解压的路径下查看是否含 ...

  3. qrcode-reader——二维码识别

    JavaScript QRCode reader for HTML5 enabled browser 参考资料1:[https://www.npmjs.com/package/qrcode-reade ...

  4. css换行用省略号代替

    css换行用省略号代替,也可以说是长标题的文章可以使用简单的CSS样式实现省略号控制显示. 一般的文字截断(适用于内联与块): .text-overflow{ display:block;/*内联对象 ...

  5. IDEA和VS快捷键对比

    IDEA和Visual Studio快捷键对比 VS     F5 F9            resume programe 恢复程序     Alt+F10       show executio ...

  6. cocos2dx基础篇(28) 布景层Layer的三个子类

    [3.x]     (1)去掉 "CC" [CCLayerColor] 颜色布景层CCLayerColor有两个父类:CCLayerRGBA.CCBlendProtocol.相信有 ...

  7. github局部不同图片合并插件

    用于解决游戏开发时,一套图里有局部地区图片不同其他地方相同,导致资源重复过大的问题 地址:https://github.com/Elringus/SpriteDicing

  8. noi.ac-CSP模拟Day5T1 组【二分图最大匹配】

    虽然是T3,但是想通了之后还是不难的. 数据规模也不大. 可以考虑先枚举一个班长,根据题意,和班长连边的学生就可以不用管,没有和班长连边的学生就要去找一个和班长连边的学生组队,如果所有没有和班长连边的 ...

  9. 【VS开发】字符串进制等转换关系及方法

    C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串.以下是用itoa()函数将整数转换为字符串的一个例子:# include <stdio.h># in ...

  10. k8s-kubernettes-sercet存储

    Secret Secret存在意义 Secret解决了密码.token.密钥等敏感数据的配置问题,而不需要把这些敏感数据暴露到镜像或者Pod Spec中.Secret可以以Volume或者环境变量的方 ...