POJ1236 Network of Schools(强连通分量)
标签:man art back push std esc sar ant and
A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a list of schools to which it distributes software (the “receiving schools”). Note that if B is in the distribution list of school A, then A does not necessarily appear in the list of school B
You are to write a program that computes the minimal number of schools that must receive a copy of the new software in order for the software to reach all schools in the network according to the agreement (Subtask A). As a further task, we want to ensure that by sending the copy of new software to an arbitrary school, this software will reach all schools in the network. To achieve this goal we may have to extend the lists of receivers by new members. Compute the minimal number of extensions that have to be made so that whatever school we send the new software to, it will reach all other schools (Subtask B). One extension means introducing one new member into the list of receivers of one school.
Input
The first line contains an integer N: the number of schools in the network (2
Output
Your program should write two lines to the standard output. The first line should contain one positive integer: the solution of subtask A. The second line should contain the solution of subtask B.
Sample Input
5
2 4 3 0
4 5 0
0
0
1 0
Sample Output
1
2
给定一个图,问至少要选择多少个顶点,才能做到从这些顶点出发可以到达全部的顶点;至少要添加多少边才能使得从任意一个顶点出发可以到达全部顶点。
Tarjan求SCC,缩点建新图,第一问直接输出入度为0的点个数即可(因为新图是DAG),第二问输出max(in,out)即max(入度为0的点的个数,出度为0的点的个数)不会证明 放个链接https://blog.csdn.net/tomandjake_/article/details/81842963
注意特判cnt==1的情况。
#include
#include
#include
#include using namespace std;
const int N = 105;
const int M = 10005;
int ver[M],Next[M],head[N],dfn[N]={0},low[N];
int vc[M],nc[M],hc[N],tc=0;
int sstack[N],ins[N],c[N];
vectorint>scc[N];
int n,m,tot=0,num=0,top=0,cnt=0;
int deg_in[N]={0},deg_out[N]={0};
void add(int x, int y)
{
ver[++tot]=y,Next[tot]=head[x],head[x]=tot;
}
void add_c(int x,int y)
{
vc[++tc]=y,nc[tc]=hc[x],hc[x]=tc;
}
void tarjan(int x)
{
dfn[x]=low[x]=++num;
sstack[++top]=x,ins[x]=1;
int i;
for(i=head[x];i;i=Next[i])
{
if(!dfn[ver[i]])
{
tarjan(ver[i]);
low[x]=min(low[x],low[ver[i]]);
}
else if(ins[ver[i]]) low[x]=min(low[x],dfn[ver[i]]);
}
if(dfn[x]==low[x])
{
cnt++;
int y;
do
{
y=sstack[top--],ins[y]=0;
c[y]=cnt,scc[cnt].push_back(y);
}while(x!=y);
}
}
int main()
{
cin>>n;
int i,j,x;
for(i=1;i)
{
int x,y;
while(scanf("%d",&y)&&y!=0)
{
x=i;
add(x,y);
}
}
for(i=1;i)
{
if(!dfn[i])tarjan(i);
}
int deg[N]={0};
for(x=1;x)
{
for(i=head[x];i;i=Next[i])
{
int y=ver[i];
if(c[x]==c[y])continue;
add_c(c[x],c[y]);
deg_in[c[y]]++;
deg_out[c[x]]++;
}
}
int in=0,out=0;
for(i=1;i//注意 这里的新图一共有cnt个点
{
if(deg_in[i]==0)in++;
if(deg_out[i]==0)out++;
}
coutinendl;
if(cnt!=1)coutin,out)endl;
else cout0;
return 0;
}
POJ1236 Network of Schools(强连通分量)
标签:man art back push std esc sar ant and
原文地址:https://www.cnblogs.com/lipoicyclic/p/12821382.html
评论