poj1236(强连通缩点)

传送门:Network of Schools

题意:一些学校联接在一个计算机网络上,学校之间存在软件支援协议,每个学校都有它应支援的学校名单(A学校支援学校B,并不表示B学校一定支援学校A)。当某校获得一个新软件时,无论是直接获得还是通过网络获得,该校都应立即将这个软件通过网络传送给它应支援的学校。因此,一个新软件若想让所有联接在网络上的学校都能使用,只需将其提供给一些学校即可。第一问:至少需要多少份软件,才能使得所有学校都能拥有软件;第二问:如果只用一份软件,那么需要添加多少条变,使得所有学校都能拥有软件。

分析:一个强连通分量中必定能相互连通,肯定能共享一个软件,因此第一问只需求入度为0的强连通分量个数即可。第二问求需要添加多少条变,使得整个图都成为一个强连通,即任意两个学校都可到达,那么取入度为0的个数a和出度为0的个数b中的最大值,因为强连通分量中必定不会有出度为0或入度为0的点,因此首先用边连接入度和出度为0的点,等其中一个完后再任意连接边把出度为0或入度为0的点补完。

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 100000000
#define inf 0x3f3f3f3f
#define eps 1e-6
#define N 110
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PII pair<int,int>
using namespace std;
struct edge
{
int v,next;
edge(){}
edge(int v,int next):v(v),next(next){}
}e[N*N];
int n,scc,step,top,tot;
int head[N],dfn[N],low[N],belong[N],Stack[N];
int in[N],out[N];
bool instack[N];
void init()
{
tot=;step=;scc=;top=;
FILL(head,-);FILL(dfn,);
FILL(low,);FILL(instack,false);
FILL(in,);FILL(out,);
}
void addedge(int u,int v)
{
e[tot]=edge(v,head[u]);
head[u]=tot++;
}
void tarjan(int u)
{
int v;
dfn[u]=low[u]=++step;
Stack[top++]=u;
instack[u]=true;
for(int i=head[u];~i;i=e[i].next)
{
v=e[i].v;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v])
{
low[u]=min(low[u],dfn[v]);
}
}
if(dfn[u]==low[u])
{
scc++;
do
{
v=Stack[--top];
instack[v]=false;
belong[v]=scc;
}while(v!=u);
}
}
void solve()
{
for(int i=;i<=n;i++)
if(!dfn[i])tarjan(i);
if(scc==)
{
printf("1\n0\n");
return;
}
for(int u=;u<=n;u++)
{
for(int i=head[u];~i;i=e[i].next)
{
int v=e[i].v;
if(belong[v]!=belong[u])
{
out[belong[u]]++;
in[belong[v]]++;
}
}
}
int a=,b=;
for(int i=;i<=scc;i++)
{
if(!in[i])a++;
if(!out[i])b++;
}
printf("%d\n%d\n",a,max(a,b));
}
int main()
{
int u;
while(scanf("%d",&n)>)
{
init();
for(int i=;i<=n;i++)
{
while(scanf("%d",&u)&&u)
addedge(i,u);
}
solve();
}
}
上一篇:ReferenceEquals()、static Equals() 、instance Equals() 与 operator==之间的联系与区别


下一篇:苹果推送APNS自己总结