poj 1236 Network of Schools 1)至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点 2)至少要加多少条边,才能使得从任何一个顶点出发,都能到达全部顶点

Description

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 <= N <= 100). The schools are identified by the first N positive integers. Each of the next N lines describes a list of receivers. The line i+1 contains the identifiers of the receivers of school i. Each list ends with a 0. An empty list contains a 0 alone in the line.

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
#include
#include
#include
#include
using namespace std;
/*
给定一个有向图,求:

1) 至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点
DAG上入度为0的点个数
2) 至少要加多少条边,才能使得从任何一个顶点出发,都能到达全部顶点
DAG上max(入度为0的点数,出度为0的点数);
n=1时单独考虑
*/
const int maxn=100000;
struct edge
{
    int t,w;//u->t=w;
    int next;
};
int V,E;//点数(从1开始),边数
int p[maxn],pf[maxn];//邻接表原图,逆图
edge G[maxn],Gf[maxn];//邻接表原图,逆图
int l,lf;
void init()
{
    memset(p,-1,sizeof(p));
    memset(pf,-1,sizeof(pf));
    l=lf=0;
}
void addedge(int u,int t,int w,int l)
{
    G[l].w=w;
    G[l].t=t;
    G[l].next=p[u];
    p[u]=l;
}
void addedgef(int u,int t,int w,int l)
{
    Gf[l].w=w;
    Gf[l].t=t;
    Gf[l].next=pf[u];
    pf[u]=l;
}

///Kosaraju算法,返回为强连通分量个数
bool flag[maxn]; //访问标志数组
int belg[maxn]; //存储强连通分量,其中belg[i]表示顶点i属于第belg[i]个强连通分量
int numb[maxn]; //结束时间(出栈顺序)标记,其中numb[i]表示离开时间为i的顶点
//用于第一次深搜,求得numb[1..n]的值
void VisitOne(int cur, int &sig)
{
  flag[cur] = true;
  for (int i=p[cur];i!=-1;i=G[i].next)
  {
     if (!flag[G[i].t])
     {
         VisitOne(G[i].t,sig);
     }
  }
  numb[++sig] = cur;
}
//用于第二次深搜,求得belg[1..n]的值
void VisitTwo(int cur, int sig)
{
  flag[cur] = true;
  belg[cur] = sig;
  for (int i=pf[cur];i!=-1;i=Gf[i].next)
  {
     if (!flag[Gf[i].t])
     {
         VisitTwo(Gf[i].t,sig);
     }
  }
}
//Kosaraju算法,返回为强连通分量个数
int Kosaraju_StronglyConnectedComponent()
{
  int  i, sig;
  //第一次深搜
  memset(flag,0,sizeof(flag));
  for ( sig=0,i=1; i<=V; ++i )
  {
     if ( false==flag[i] )
     {
         VisitOne(i,sig);
     }
  }
  //第二次深搜
  memset(flag,0,sizeof(flag));
  for ( sig=0,i=V; i>0; --i )
  {
     if ( false==flag[numb[i]] )
     {
         VisitTwo(numb[i],++sig);
     }
  }
  return sig;
}
//缩点
int n;//缩点后的点个数1~n
int g[maxn];
edge eg[maxn];//邻接表
int re;
int cont[maxn];
int cntin0,cntout0;//入度为0的点个数和出度为0的点个数
int in[maxn],out[maxn];//入度 出度
void dinit(int sig)
{
	memset(g,-1,sizeof(g));
	n=sig;
	re=cntin0=cntout0=0;
	memset(in,0,sizeof(in));
	memset(out,0,sizeof(out));
	memset(cont,0,sizeof(cont));//1~n 第sig块强联通分量中的点数
}
void addedge0(int u,int t,int w,int l)
{
    eg[l].w=w;
    eg[l].t=t;
    eg[l].next=g[u];
    g[u]=l;
}
int main()
{
    while(scanf("%d",&V)==1)
    {
        init();E=0;
		for(int i=1;i<=V;i++)
		{
			int u=i,t,w=1;
			while(scanf("%d",&t)==1&&t)
			{
				addedge(u,t,w,l++);
				addedgef(t,u,w,lf++);
				E++;
			}
		}
        int ans=Kosaraju_StronglyConnectedComponent();
        //printf("%d/n",ans);
        ///缩点
        dinit(ans);
        if(n==1)//当DAG中只有1个点时 单独考虑
        {
        	printf("1/n0/n");
        	continue;
        }
        for(int i=1;i<=V;i++)//构图
        {
        	for(int j=p[i];j!=-1;j=G[j].next)
        	{
        		int st=belg[i],ed=belg[G[j].t];
        		if(st!=ed)
        		{
        			int flag=1;
        			for(int k=g[st];k!=-1;k=eg[k].next)
        			{
        				if(eg[k].t==ed)
        				{
        					flag=0;
        					break;
        				}
        			}
        			if(flag) addedge0(st,ed,1,re++);
        		}
        	}
        }
        for(int i=1;i<=n;i++)
        {
        	for(int j=g[i];j!=-1;j=eg[j].next)
        	{
        		in[eg[j].t]++,out[i]++;
        	}
        }
        for(int i=1;i<=n;i++)
        {
        	if(in[i]==0) cntin0++;
        	if(out[i]==0) cntout0++;
        }
        printf("%d/n%d/n",cntin0,max(cntin0,cntout0));
    }
    return 0;
}
 
 

你可能感兴趣的:(acm_图论)