UVaLive3523
来源:互联网 发布:java防止sql注入代码 编辑:程序博客网 时间:2024/06/03 23:41
UVaLive3523 Knights of the Round Table
Description
有
多组测试数据。
Input
每组测试数据:
第一行两个整数,
接下来
当
Output
一个整数,一定不能参加的骑士的个数(原题样例有毒)。
solution
不互相憎恨的骑士间连一条边,能开会的骑士一定在同一个简单奇圈,简单奇圈一定在双连通分量上,只要这个双连通分量不是二分图就信了。
问题就转化成找不在不是二分图的双连通分量上的点的个数。
code
#include<iostream>#include<cstdio>#include<cstdlib>#include<vector>#include<string.h>//#include<>using namespace std;struct EDGE{ int v; EDGE *next;}edge[2100000],*ind[1100];struct S{ int u,v;}s[2100000];int n,m,el=0,sl,sum;bool map[1100][1100],ans[1100];///vector<int> bcc[1100];int pre[1100],Time,bn,bccn[1100];///void input(int u,int v){ edge[++el].v=v; edge[el].next=ind[u]; ind[u]=&edge[el];}int dfs(int u,int fa){ int lowu=pre[u]=++Time,child=0; for(EDGE *k=ind[u];k!=NULL;k=k->next) { int v=k->v; if(pre[v]==0) { s[++sl].v=v,s[sl].u=u; child++; int lowv=dfs(v,u); lowu=min(lowv,lowu); if(lowv>=pre[u]) { bcc[++bn].clear(); while(1) { int x=s[sl].u,y=s[sl].v; sl--; if(bccn[x]!=bn) { bcc[bn].push_back(x); bccn[x]=bn; } if(bccn[y]!=bn) { bcc[bn].push_back(y); bccn[y]=bn; } if(x==u&&y==v) break; } } } else if(pre[v]<pre[u] && v!=fa) { s[++sl].v=v,s[sl].u=u; lowu=min(lowu,pre[v]); } } return lowu;}void find_bcc(){ for(int i=1;i<=n;i++) { if(!pre[i]) dfs(i,-1); }}int color[1100];bool color_che(int u,int c){ for(EDGE *k=ind[u];k!=NULL;k=k->next) { int v=k->v; if(bccn[v]!=c) continue; if(color[v]==color[u]) return 0; if(!color[v]) { color[v]=3-color[u]; if(color_che(v,c)==0) return 0; } } return 1;}void che(){ for(int i=1;i<=bn;i++) { memset(color,0,sizeof(color)); color[bcc[i][0]]=1; for(int j=0;j<bcc[i].size();j++) bccn[bcc[i][j]]=i; if(!color_che(bcc[i][0],i)) for(int j=0;j<bcc[i].size();j++) ans[bcc[i][j]]=1; }}void init(){ for(int i=0;i<=n;i++){ ind[i]=NULL; pre[i]=ans[i]=bccn[i]=0; } bn=sl=Time=sum=el=0;}int main(){ scanf("%d%d",&n,&m); init(); while(n!=0&&m!=0) { for(int i=1;i<=m;i++) { int x,y; scanf("%d%d",&x,&y); map[x][y]=1; map[y][x]=1; } for(int i=1;i<n;i++) for(int j=i+1;j<=n;j++) if(!map[i][j]) input(i,j),input(j,i); else map[i][j]=0; find_bcc(); che(); for(int i=1;i<=n;i++) if(!ans[i]) sum++; printf("%d\n",sum); scanf("%d%d",&n,&m); init(); } return 0;}
0 0
- UVaLive3523
- UVALive3523-Knights of the Round Table(BCC+二分图判定)
- UVAlive3523 Knights of the Round Table(bcc)
- uvalive3523非主流的代码!!求点双连通分量
- UVALive3523 [Knights of the Round Table] tarjan求无向图双联通分量
- #UVALive3523#Knights of the Round Table(点双连通分量 + 二分图染色判奇环)
- BZOJ2809
- linux安装
- 简易MAP3播放小程序
- Java 可变参 Object...objects 方法的陷进
- ubuntu14.04安装sogou-pinyin
- UVaLive3523
- 【数据结构】堆串的连接,替换,插入操作
- PHPMailer发送邮件出现Permission denied的解决办法
- glew使用问题汇总,看能折腾到多少条
- 欢迎使用CSDN-markdown编辑器
- JdbcTemplate 易被 Java 8 Lambda 带入的坑
- 深入理解Java虚拟机学习笔记——四、Java内存模型与多线程
- JavaSE 13 集合与泛型
- C#上机实验之题目三