ACM: 图论题 poj 1236 强连通

来源:互联网 发布:java酒店管理系统程序 编辑:程序博客网 时间:2024/05/17 18:15
Network ofSchools

 

Description

A number of schools areconnected to a computer network. Agreements have been developedamong those schools: each school maintains a list of schools towhich it distributes software (the “receiving schools”). Note thatif B is in the distribution list of school A, then A does notnecessarily appear in the list of school B
You are to write a program that computes the minimal number ofschools that must receive a copy of the new software in order forthe software to reach all schools in the network according to theagreement (Subtask A). As a further task, we want to ensure that bysending the copy of new software to an arbitrary school, thissoftware will reach all schools in the network. To achieve thisgoal we may have to extend the lists of receivers by new members.Compute the minimal number of extensions that have to be made sothat whatever school we send the new software to, it will reach allother schools (Subtask B). One extension means introducing one newmember into the list of receivers of one school.

Input

The first line contains aninteger N: the number of schools in the network (2<= N <= 100). The schools areidentified by the first N positive integers. Each of the next Nlines describes a list of receivers. The line i+1 contains theidentifiers 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 twolines to the standard output. The first line should contain onepositive integer: the solution of subtask A. The second line shouldcontain the solution of subtask B.

Sample Input

5
2 4 3 0
4 5 0
0
0
1 0

Sample Output

1
2

题意: n个学校要进行软件维护, 软件进行拷贝, 并且每个学校有自己的维护表(拷贝软件给其它学校),

     至少需要复制多少次软件, 使毎个学校都能够得到该软件, 要在所有的学校的清单里面至少一共

     增加几项才能 使得软件给任意一个学校,所有的学校都能收得到.

 

解题思路:

     1. 题意很明确, 强连通图问题. tarjan求出强连通分量, 然后进行每一个连通分量进行缩点处理

        最后求出缩点后入度为0的个数, 和 max(入度为0的个数, 出度为0的个数)即可.

 

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 105

int n;
int g[MAX][MAX];
int dfn[MAX], low[MAX];
int stack[MAX], bcc[MAX], count, top;
bool vis[MAX];
int in[MAX], out[MAX];

inline int min(int a, int b)
{
 return a < b ? a : b;
}

void readGraph()
{
 memset(g, 0, sizeof(g));
 memset(dfn, 0, sizeof(dfn));
 memset(low, 0, sizeof(low));
 memset(stack, 0, sizeof(stack));
 memset(bcc, 0, sizeof(bcc));
 memset(vis, false, sizeof(vis));
 memset(in, 0, sizeof(in));
 memset(out, 0, sizeof(out));
 count = top = 0;
 int v;
 for(int i = 1; i <= n; ++i)
 {
  while(true)
  {
   scanf("%d",&v);
   if(v == 0)break;
   else g[i][v]= 1;
  }
 }
}

void tarjan(int u, int deep)
{
 dfn[u] = low[u] = deep;
 vis[u] = true;
 stack[top++] = u;
 for(int v = 1; v <= n; ++v)
 {
  if( g[u][v] )
  {
   if( !dfn[v])
   {
    tarjan(v,deep+1);
    low[u]= min(low[u], low[v]);
   }
   else if(vis[v] )
    low[u]= min(low[u], dfn[v]);
  }
 }

 if( dfn[u] == low[u] )
 {
  count++;
  int v;
  do
  {
   v =stack[top-1];
   top--;
   vis[v] =false;
   bcc[v] =count;
  }while(v != u);
 }
}

void solve()
{
 int i, j;
 int t_in = 0, t_out = 0;
 for(i = 1; i <= n; ++i)
 {
  for(j = 1; j <=n; ++j)
  {
   if(g[i][j]&& bcc[i] != bcc[j])
   {
    in[bcc[j] ]++;
    out[bcc[i] ]++;
   }
  }
 }

 for(i = 1; i <= count;++i)
 {
  if( in[i] == 0 ) t_in++;
  if( out[i] == 0 )t_out++;
 }

 if(count == 1)
  printf("1\n0\n");
 else
  printf("%d\n%d\n", t_in, t_in> t_out ? t_in : t_out);
}

int main()
{
// freopen("input.txt", "r", stdin);
 while(scanf("%d", &n) !=EOF)
 {
  readGraph();
  for(int i = 1; i<= n; ++i)
   if( !low[i])
    tarjan(i,1);
  solve();
 }
 return 0;
}

0 0
原创粉丝点击