Peng Lv

毋意,毋必,毋固,毋我。 言必行,行必果。

导航

POJ 1236 Network of Schools (强连通+缩点)

ft........这个题开始想错了,无限WA,后来看了DIS才知道,自己完全错了,这个题做的很郁闷........努力啊

不会Tarjan,丢人了,贴个连接,抓紧学习。

http://www.byvoid.com/blog/scc-tarjan/

 

#include <iostream>
#include
<cstdio>
#include
<algorithm>
#include
<memory.h>
#include
<cmath>
#include
<bitset>
#include
<queue>
#include
<vector>
using namespace std;

const int BORDER = (1<<20)-1;
const int MAXSIZE = 37;
const int MAXN = 205;
const int INF = 1000000000;
#define CLR(x,y) memset(x,y,sizeof(x))
#define ADD(x) x=((x+1)&BORDER)
#define IN(x) scanf("%d",&x)
#define OUT(x) printf("%d\n",x)
#define MIN(m,v) (m)<(v)?(m):(v)
#define MAX(m,v) (m)>(v)?(m):(v)
#define ABS(x) ((x)>0?(x):-(x))

using namespace std;

typedef
struct{
int v;
int next;
}Edge;

Edge edge_p[MAXN
*MAXN],edge_r[MAXN*MAXN];

int n,m,k,ans,index_p,index_r,cnt,ret1,ret2;
int net_p[MAXN],net_r[MAXN],order[MAXN],set[MAXN];
int in_dg[MAXN],out_dg[MAXN];
bool visit[MAXN];

void add_edge(Edge* edge,int* net,const int& u,const int& v,int& index)
{
edge[index].v
= v;
edge[index].next
= net[u];
net[u]
= index++;
}
int init()
{
index_r
= index_p = 0;
CLR(net_p,
-1);
CLR(net_r,
-1);
CLR(visit,
0);
CLR(order,
0);
CLR(
set,0);
CLR(in_dg,
0);
CLR(out_dg,
0);
return 0;
}
int input()
{
int tmp;
for(int i = 1; i <= n; ++i)
{
for(int j = 0; ; ++j)
{
IN(tmp);
if(!tmp)
break;
add_edge(edge_p,net_p,i,tmp,index_p);
add_edge(edge_r,net_r,tmp,i,index_r);
}
}
return 0;
}
void dfs_p(const int& u)
{
visit[u]
= true;
for(int i = net_p[u]; i != -1; i = edge_p[i].next)
if(!visit[edge_p[i].v])
dfs_p(edge_p[i].v);
order[
++cnt] = u;
}
void dfs_r(const int& u)
{
visit[u]
= true;
set[u] = cnt;
for(int i = net_r[u]; i != -1; i = edge_r[i].next)
if(!visit[edge_r[i].v])
dfs_r(edge_r[i].v);
}
int find_dg()
{
int i,j,tmp;
ret1
= ret2 = 0;
for(i = 1; i <= n; ++i)
for(j = net_p[i]; j != -1; j = edge_p[j].next)
{
if(set[i] != set[edge_p[j].v])
{
in_dg[
set[edge_p[j].v]] = 1;
out_dg[
set[i]] = 1;
}
}
for(i = 1; i <= cnt; ++i)
{
if(!in_dg[i])
++ret1;
if(!out_dg[i])
++ret2;
}
return 0;
}
int work()
{
int i,j,tmp,u,v;
CLR(visit,
0);
cnt
= 0;
for(i = 1; i <= n; ++i)
if(!visit[i])
dfs_p(i);
CLR(visit,
0);
cnt
= 0;
for(i = n; i > 0; --i)
if(!visit[order[i]])
{
++cnt;
dfs_r(order[i]);
}
find_dg();
if(cnt == 1)
printf(
"1\n0\n");
else
printf(
"%d\n%d\n",ret1,MAX(ret1,ret2));
return 0;
}
int main()
{
while(IN(n)!=EOF)
{
init();
input();
work();
}
return 0;
}

 

 

posted on 2010-04-27 18:46  Lvpengms  阅读(447)  评论(0编辑  收藏  举报