zoukankan      html  css  js  c++  java
  • poj1236 Network of Schools(Tarjan+缩点)

    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 27058   Accepted: 10670

    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

     
    题目大意
    单组数据,n个点,之后1~n行输入每行有向连接的点,0结尾
    任务A:至少选择几台电脑,通过文件传输使得n台电脑都能接收到文件
    任务B:至少添加几条连接(边),使得通过任意一台电脑能到达所有n台电脑
     
    思路
    任务A:缩点后求解入度为0的点的个数
    任务B:缩点后求解max(入度为0的点的个数,出度为0的点的个数)
     
     1 #include <cstdio>
     2 #include <vector>
     3 #include <algorithm>
     4 using namespace std;
     5 
     6 const int N=1e2+5;
     7 int low[N],dfn[N],Stack[N],belong[N],rdu[N],cdu[N],vis[N][N];
     8 bool inStack[N];
     9 int n,m,tot,tag,top;
    10 vector<int> g[N],vec[N];
    11 
    12 void tarjan(int u){
    13     int v;
    14     low[u]=dfn[u]=++tot;
    15     Stack[++top]=u;
    16     inStack[u]=true;
    17     for(int i=0;i<g[u].size();i++){
    18         v=g[u][i];
    19         if(dfn[v]==0){
    20             tarjan(v);
    21             low[u]=min(low[u],low[v]);
    22         }
    23         else if(inStack[v]==true){
    24             low[u]=min(low[u],dfn[v]);
    25         }
    26     }
    27     if(dfn[u]==low[u]){
    28         tag++;
    29         do{
    30             v=Stack[top--];
    31             inStack[v]=false;
    32             belong[v]=tag;
    33         }while(u!=v);
    34     }
    35 }
    36 
    37 int main(){
    38     scanf("%d",&n);
    39     for(int i=1;i<=n;i++){
    40         int x;
    41         while(scanf("%d",&x),x){
    42             g[i].push_back(x);
    43         }
    44     }
    45     for(int i=1;i<=n;i++){
    46         if(dfn[i]==0){
    47             tot=0;
    48             tarjan(i);
    49         }
    50     }
    51     if(tag==1){
    52         printf("1
    0
    ");
    53         return 0;
    54     }
    55     for(int i=1;i<=n;i++){
    56         for(int j=0;j<g[i].size();j++){
    57             int x=g[i][j],bi=belong[i],bx=belong[x];
    58             if(bi!=bx&&vis[bi][bx]==0){
    59                 vec[bi].push_back(bx);
    60                 vis[bi][bx]=1;
    61             }
    62         }
    63     }
    64     for(int i=1;i<=tag;i++){
    65         for(int j=0;j<vec[i].size();j++){
    66             int x=vec[i][j];
    67             cdu[i]++;
    68             rdu[x]++;
    69         }
    70     }
    71 //    for(int i=1;i<=tag;i++){
    72 //        printf(" %d",i);
    73 //        for(int j=0;j<vec[i].size();j++){
    74 //            printf(" %d",vec[i][j]);
    75 //        }
    76 //        printf("
    ");
    77 //    }
    78     int cnta=0,cntb=0;
    79     for(int i=1;i<=tag;i++){
    80         if(rdu[i]==0){
    81             cnta++;
    82         }
    83         if(cdu[i]==0){
    84             cntb++;
    85         }
    86     }
    87     printf("%d
    ",cnta);
    88     printf("%d
    ",max(cnta,cntb));
    89 }
  • 相关阅读:
    nginx内置变量
    MySQL获取错误编号 try判断 获取 Exception 上的错误
    MySQL错误代码大全(转)
    PHP递归菜单/权限目录(无限极数组)
    PHP魔术方法
    php and和&&的一个坑(文章是发现其他博客,保存自己笔记)
    nginx配置php与前后端分离(文档只供本人观看,接受错误但勿喷!)
    servlet的构造器与init方法
    模板方法设计模式
    MVC
  • 原文地址:https://www.cnblogs.com/ChangeG1824/p/11424510.html
Copyright © 2011-2022 走看看