A - Network of Schools POJ - 1236(Tarjan)

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 
#include 
using namespace std;
/*
Tarjan 算法
复杂度为O(N+M)
*/
const int MAXN = 20010;//点数
const int MAXM = 50010;//边数
struct Edge
{
  int to, next;
}edge[MAXM];
int head[MAXN], tot;
int Low[MAXN], DFN[MAXN], Stack[MAXN],Belong[MAXN];//Belong数组的值是1~scc
int Index, top;
int scc;//强连通分量的个数
bool Instack[MAXN];
int num[MAXN];//各个强连通分量包含的点的个数,数组编号1~scc

void addedge(int u, int v)
{
   edge[tot].to = v;
   edge[tot].next = head[u];
   head[u] = tot++;
}
//总的来数,这个算法是在dfs的基础上,通过访问,来判断是否可以构成一个强连通分量。
void Tarjan(int u)
{
   int v;
   Low[u] = DFN[u] = ++Index;//Low 记录能够到达当前点的最小编号是多少,DFN时间戳
   Stack[top++] = u;
   Instack[u] = true;
   for(int i=head[u];i!=-1;i=edge[i].next)//遍历所有的边
   {
      v = edge[i].to;
      if(!DFN[v])
      {
         Tarjan(v);
         if(Low[u]>Low[v])
         {
            Low[u] = Low[v];
         }
      }
      else if(Instack[v]&&Low[u]>DFN[v])
      Low[u] = DFN[v];
   }
   if(Low[u]==DFN[u])
   {
      scc++;
      do
      {
         v = Stack[--top];
         Instack[v] = false;
         Belong[v] = scc;
         //num[scc]++;
      }
      while(v!=u);
   }
}
int in[MAXN], out[MAXN];//入度,出度
void solve(int N)
{
   memset(DFN, 0, sizeof(DFN));
   memset(Instack, false, sizeof(Instack));
   //memset(num, 0, sizeof(num));
   Index = scc = top = 0;
   for(int i=1;i<=N;i++)
   {
     if(!DFN[i])
     {
       Tarjan(i);
     }
   }
   //kaishi
   if(scc==1)
   {
     printf("1\n0\n");
     return ;
   }
   for(int i=1;i<=scc;i++)
   in[i] = out[i] = 0;
   for(int u=1;u<=N;u++)
   {
      for(int i= head[u];i!=-1;i=edge[i].next)
      {
         int v = edge[i].to;
         if(Belong[u]!=Belong[v])
         {
           in[Belong[v]]++;
           out[Belong[u]]++;
         }
      }
   }
   int ans1 = 0;
   int ans2 = 0;
   for(int i=1;i<=scc;i++)
   {
      if(in[i]==0)
      ans1++;
      if(out[i]==0)
      ans2++;
   }
   printf("%d\n%d\n", ans1, max(ans1, ans2));
}
void init()//初始化
{
  tot = 0;
  memset(head, -1, sizeof(head));
}
int main()
{
  int n;
  int v;
  while(~scanf("%d", &n))
  {
     init();
     for(int i=1;i<=n;i++)
     {
        while(~scanf("%d", &v)&&v)
        {
          addedge(i, v);//建边
        }
     }
     solve(n);
  }
  return 0;
}



强连通图:有向图+强连通
题目大意:
就是说假如有n个入度为0的点,m个出度为0的点,输出:1,n; 2,max(n, m);要考虑一个节点的情况。

你可能感兴趣的:(POJ,强连通图)