[POJ 1236] Network of Schools | Tarjan缩点

简介: DescriptionA 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”).

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


Source

IOI 1996


题意:


有n个学校,每个学校可以给其它一些学校发送消息。

第一问:最少选择多少个学校投放消息,才能够使得所有的学校都收到消息


第二问:如果要使得在任意学校放置消息,其他学校都可以收到,至少需要添加多少条边

首先将图建立之后,进行缩点

缩点后找到每个强连通同分量的入度和出度


第一问是

入度为0的点


第二问是

max(入度为0的点,出度为0的点)数量的最大值

特殊情况:但是如果说只有一个强连通分量

就应该是{

1

0

}

假设以题中的样例为例:

缩点之前:


f3194f026b284b8195b83808f70e2959.png


缩点之后:

(1,2,5是强连通的)


86fe1cb35ec34f09964b073ae6281913.png


这里,要加入4->1,3->1两条边

对于第二问,不能让出度为0的店没法发送消息,也不能让入度为0的点没法接收消息,所以说就得二者取max


int cnt,head[107];
int n;
struct node {
  int u,v,nex;
} e[maxn];
void _Init() {
  cnt = 0;
  Clear(head,-1);
}
void add(int u,int v) {
  e[cnt].u = u;
  e[cnt].v = v;
  e[cnt].nex = head[u];
  head[u] = cnt ++;
}
stack<int> stk;
int cntSCC;
int low[107],dfn[107],dfc,pos[107];
bool instack[107];
int vis[107][107];
void Tarjan(int u) {
  dfn[u] = low[u] = ++ dfc;
  stk.push(u);
  instack[u] = 1;
  for(int i=head[u]; ~i; i=e[i].nex) {
    int to = e[i].v;
    if(!dfn[to]) {
      Tarjan(to);
      low[u] = min(low[u], low[to]);
    } else if(instack[to]) low[u] = min(low[u], dfn[to]);
  }
  if(low[u] == dfn[u]) {
    int tp;/// = stk.top();
    ++ cntSCC;
    do {
      tp = stk.top();
      stk.pop();
      instack[tp] = 0;
      pos[tp] = cntSCC;
    } while(tp != u);
  }
}
int out[107],in[107];
int main() {
  n = read;
  _Init();
  for(int i=1; i<=n; i++) {
    int x = read;
    while(x) {
      add(i, x);
      x = read;
    }
  }
  for(int i=1; i<=n; i++) {
    if(!dfn[i]) Tarjan(i);
  }
  for(int i=1; i<=n; i++) {
    int u = i;
    for(int j=head[u]; ~j; j=e[j].nex) {
      int to = e[j].v;
      if(pos[u] != pos[to] && !vis[u][to]) {
        out[pos[u]] ++;
        in[pos[to]] ++;
      }
    }
  }
  /**
  for(int i=1; i<=n; i++) {
    printf("%d %d %d\n",i,out[i],in[i]);
  }
  **/
  int cntout = 0,cntin = 0;
  for(int i=1; i<=cntSCC; i++) {
    if(!out[i]) cntout ++;
    if(!in[i]) cntin ++;
  }
  if(cntSCC == 1) puts("1\n0");
  else {
    cout << cntin << endl << max(cntout,cntin) << endl;
  }
  return 0;
}
/**
**/


目录
相关文章
|
1月前
POJ—2236 Wireless Network
POJ—2236 Wireless Network
|
1月前
Arctic Network( POJ - 2349)
Arctic Network( POJ - 2349)
|
算法 数据建模
【POJ 1236 Network of Schools】强联通分量问题 Tarjan算法,缩点
题目链接:http://poj.org/problem?id=1236 题意:给定一个表示n所学校网络连通关系的有向图。现要通过网络分发软件,规则是:若顶点u,v存在通路,发给u,则v可以通过网络从u接收到。
1158 0
|
Web App开发 算法