这道题其实就是求割点数量
用点双联通
#include <iostream>
#include <cstring>
#include <cstdio>
#include <stack>
using namespace std ;
const int MAXN = 1001 ;
const int MAXM = 2*MAXN * MAXN ;
const int INF = 99999999 ;
struct type
{
int end , next ;
} ;
type edge[MAXM] ;
int head[MAXN] , Count , map[MAXN][MAXN] , vis[MAXN] , visit[MAXN] , low[MAXN] , dfn[MAXN] ;
int color[MAXN] , belone[MAXN] , val[MAXN] , ans , cnt , Time , flag ;
stack < int > S ;
void addedge( int start , int end )
{
edge[Count].end = end ;
edge[Count].next = head[start] ;
head[start] = Count ++ ;
}
void dfs( int root , int col )
{
int i , end ;
color[root] = col ;
for( i = head[root] ; i != -1 ; i = edge[i].next )
{
end = edge[i].end ;
if( belone[end] != cnt ) continue ;
if( color[end] == 1 - col ) continue ;
if( color[end] == -1 ) dfs( end , 1 - col ) ;
else if( color[end] == col ) flag = 1 ;
if( flag ) return ;
}
}
void tarjan_dfs( int cur , int father )
{
S.push( cur ) ;
dfn[cur] = low[cur] = Time ++ ;
visit[cur] = 1 ;
for( int i = head[cur] ; i != -1 ; i = edge[i].next )
{
int end = edge[i].end ;
if( end == father ) continue ;
if( !visit[end] )
{
tarjan_dfs( end , cur ) ;
low[cur] = min( low[cur] , low[end] ) ;
if( low[end] >= dfn[cur] )
{
int ss ;
cnt ++ ;
ans = 0 ;
do
{
ss = S.top() ;
S.pop() ;
val[++ans] = ss ;
belone[ss] = cnt ;
}while( ss != end ) ;
belone[cur] = cnt ;
val[++ans] = cur ;
flag = 0 ;
memset( color , -1 , sizeof( color ) ) ;
dfs( cur , 0 ) ;
if( flag ) for( int j = 1 ; j <= ans ; j ++ )
vis[val[j]] = 1 ;
}
}
else low[cur] = min( low[cur] , dfn[end] ) ;
}
}
int main()
{
int n , m , i , j , temp , sum ;
while( scanf( "%d%d" , & n , & m ) != EOF && n && m )
{
memset( head , -1 , sizeof( head ) ) ;
memset( map , 0 , sizeof( map ) ) ;
Count = 0 ;
while( m -- )
{
int start , end ;
scanf( "%d%d" , & start , & end ) ;
map[start][end] = map[end][start] = 1 ;
}
for( i = 1 ; i <= n ; i ++ )
for( j = i + 1 ; j <= n ; j ++ )
if( ! map[i][j] ){
addedge( i , j ) ;
addedge( j , i ) ;
}
cnt = 0 ; Time = 0 ;
memset( vis , 0 , sizeof( vis ) ) ;
memset( visit , 0 , sizeof( visit ) ) ;
memset( belone , -1 , sizeof( belone ) ) ;
for( int i = 1 ; i <= n ; i ++ )
if( ! visit[i] ) tarjan_dfs( i , -1 ) ;
sum = 0 ;
for( int i = 1 ; i <= n ; i ++ )
if( vis[i] ) sum ++ ;
printf( "%d\n" , n - sum ) ;
}
return 0 ;
}
分享到:
相关推荐
poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题...
poj 3414解题报告poj 3414解题报告poj 3414解题报告poj 3414解题报告
poj 1012解题报告poj 1012解题报告poj 1012解题报告poj 1012解题报告
poj 2329解题报告poj 2329解题报告poj 2329解题报告poj 2329解题报告
poj 1440解题报告 poj 1440解题报告 poj 1440解题报告 poj 1440解题报告
poj 3083解题报告poj 3083解题报告poj 3083解题报告poj 3083解题报告
poj 1659解题报告poj 1659解题报告poj 1659解题报告poj 1659解题报告
poj 3720解题报告poj 3720解题报告poj 3720解题报告poj 3720解题报告
poj1691解题报告 题目来源:http://acm.pku.edu.cn/JudgeOnline/showproblem?problem_id=1691(POJ No.1691) 解法: 搜索
poj2828解题报告,希望能帮到志同道合的算法爱好者
北大poj解题报告,希望能帮到软件工程的同学,每天一道,持之以恒,熟能生巧,与您共勉!
POJ2942-Knights of the Round Table 【Tarjan算法】 解题报告+AC代码 http://hi.csdn.net/!s/F3L8HO ================================== 我的POJ所有解题报告:...
北大ACM1316解题报告
Problem 1061 青蛙的约会 poj解题报告。有源码。可直接提交C++实现
这是我发的第二篇解题报告,写的不怎么的。呵呵!!!!!
ACM Poj Pku 解题报告答案 打包 下载 600多题 史上最全 不是网上乱传的200多题,更不是100多题就挂着10分才能下的题 下了这个 大家也不要浪费分数去下载其它版本的了,基本上都有 共享 一起进步 中国加油 ACMer...
ACM POJ 解题报告北大POJ 大量解题代码
本人整理的POJ解题报告,一共有250道题
北大ACM在线评测系统POJ的题目解题报告。涵盖各种类型的acm题目。值得参考借鉴。打包下载。
不错的资源 收集了网上的一些解题报告 方便同学们学习参考