zoukankan      html  css  js  c++  java
  • POJ1236 tarjan

    Network of Schools
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 19613   Accepted: 7725

    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

    思路:求一个有向图从几个点出发可以遍历整个图、以及至少加几条边使整张图强联通。

    缩点以后,显然入度为0的点的个数就是第一问的答案。 
    然后第二问答案显然是入度为0和出度为0的个数的最大值,即出入度为0的点间连条边就可以了。

    代码:

     1 #include"bits/stdc++.h"
     2 
     3 #define db double
     4 #define ll long long
     5 #define vl vector<ll>
     6 #define ci(x) scanf("%d",&x)
     7 #define cd(x) scanf("%lf",&x)
     8 #define cl(x) scanf("%lld",&x)
     9 #define pi(x) printf("%d
    ",x)
    10 #define pd(x) printf("%f
    ",x)
    11 #define pl(x) printf("%lld
    ",x)
    12 #define rep(i, a, n) for (int i=a;i<n;i++)
    13 #define per(i, a, n) for (int i=n-1;i>=a;i--)
    14 #define fi first
    15 #define se second
    16 using namespace std;
    17 typedef pair<int, int> pii;
    18 const int N = 1e4 + 5;
    19 const int mod = 1e9 + 7;
    20 const int MOD = 998244353;
    21 const db PI = acos(-1.0);
    22 const db eps = 1e-10;
    23 const int inf = 0x3f3f3f3f;
    24 const ll INF = 0x3fffffffffffffff;
    25 int low[N], dfn[N],head[N],beg[N];
    26 bool ins[N];
    27 int in[N],out[N];
    28 int n;
    29 int cnt, id, num;
    30 stack<int> s;
    31 
    32 struct P {
    33     int to, nxt;
    34 } e[2 * N];
    35 
    36 void add(int u, int v) {
    37     e[cnt].to = v;
    38     e[cnt].nxt = head[u];
    39     head[u] = cnt++;
    40 }
    41 void tarjan(int u)
    42 {
    43     low[u]=dfn[u]=++id;    ins[u]=1;
    44     s.push(u);//入栈
    45     for(int i=head[u];~i;i=e[i].nxt){
    46         int v=e[i].to;
    47         if(!dfn[v]) tarjan(v),low[u]=min(low[u],low[v]);
    48         else if(ins[v]) low[u]=min(low[u],dfn[v]);
    49     }
    50     if(low[u]==dfn[u]){
    51         int v;
    52         do{
    53             v=s.top();s.pop();
    54             beg[v]=num;//缩点
    55             ins[v]=0;
    56         }while(u!=v);
    57         num++;
    58     }
    59 }
    60 
    61 void init() {
    62     memset(ins, 0, sizeof(ins));
    63     memset(head, -1, sizeof(head));
    64     memset(dfn, 0, sizeof(dfn));
    65     memset(low, 0, sizeof(low));
    66     memset(beg, 0, sizeof(beg));
    67     memset(in, 0, sizeof(in));
    68     memset(out, 0, sizeof(out));
    69     while(!s.empty()) s.pop();
    70     cnt = id = num = 0;
    71 }
    72 int main() {
    73     while (scanf("%d",&n) != EOF) {
    74         init();
    75         for(int i=1;i<=n;i++){
    76             int x;
    77             while(scanf("%d",&x)&&x) add(i,x);
    78         }
    79         for (int i = 1; i <= n; i++) if (!dfn[i]) tarjan(i);
    80         for (int i = 1; i <= n; i++) {
    81             for (int j = head[i]; j != -1; j = e[j].nxt) {
    82                 int v=e[j].to;
    83                 if(beg[i]!=beg[v]) out[beg[i]]++,in[beg[v]]++;
    84             }
    85         }
    86         int I=0,O=0;
    87         for(int i=0;i<num;i++){
    88             if(!in[i]) I++;
    89             if(!out[i]) O++;
    90         }
    91         if(num==1) printf("1
    0");
    92         else pi(I),pi(max(I,O));
    93     }
    94     return 0;
    95 }
  • 相关阅读:
    实验9: 静态路由和默认路由
    实验8:路由器IOS升级2
    实验7:交换机IOS升级
    实验6:路由器IOS升级
    实验5: IOS的升级与恢复
    实验4: 路由器的密码恢复
    实验3: DHCP 基本配置
    Linux用户的基本操作3 (组的基本管理,用户提权)
    拓展练习:(用户的基本管理及用户提权部分)
    linux用户的基本操作2 用户密码管理
  • 原文地址:https://www.cnblogs.com/mj-liylho/p/9556053.html
Copyright © 2011-2022 走看看