zoukankan      html  css  js  c++  java
  • COGS——C 908. 校园网 || 洛谷——P 2746 [USACO5.3]校园网Network of Schools

    http://www.cogs.pro/cogs/problem/problem.php?pid=908   ||  https://www.luogu.org/problem/show?pid=2746

    ★★   输入文件:schlnet.in   输出文件:schlnet.out   简单对比
    时间限制:1 s   内存限制:128 MB

    USACO/schlnet(译 by Felicia Crazy)

    描述

    一些学校连入一个电脑网络。那些学校已订立了协议:每个学校都会给其它的一些学校分发软件(称作“接受学校”)。注意如果 B 在 A 学校的分发列表中,那么 A 不必也在 B 学校的列表中。

    你要写一个程序计算,根据协议,为了让网络中所有的学校都用上新软件,必须接受新软件副本的最少学校数目(子任务 A)。更进一步,我们想要确定通过给任意一个学校发送新软件,这个软件就会分发到网络中的所有学校。为了完成这个任务,我们可能必须扩展接收学校列表,使其加入新成员。计算最少需要增加几个扩展,使得不论我们给哪个学校发送新软件,它都会到达其余所有的学校(子任务 B)。一个扩展就是在一个学校的接收学校列表中引入一个新成员。

    PROGRAM NAME: schlnet

    INPUT FORMAT (file schlnet.in)

    输入文件的第一行包括一个整数 N:网络中的学校数目(2 <= N <= 100)。学校用前 N 个正整数标识。接下来 N 行中每行都表示一个接收学校列表(分发列表)。第 i+1 行包括学校 i 的接收学校的标识符。每个列表用 0 结束。空列表只用一个 0 表示。

    OUTPUT FORMAT(file schlnet.out)

    你的程序应该在输出文件中输出两行。第一行应该包括一个正整数:子任务 A 的解。第二行应该包括子任务 B 的解。

    SAMPLE INPUT (file schlnet.in)


    2 4 3 0
    4 5 0
    0

    1 0

    SAMPLE OUTPUT (file schlnet.out)

    1
    2

    A: 缩点后入度为零的点;B:缩点后出度为零的点和(缩点后的点数-入读为零的点的个数)取MAX

    特判只有一个连通块的的时候

     1 #include <algorithm>
     2 #include <cstdio>
     3 
     4 using namespace std;
     5 
     6 const int N(110*110);
     7 int n,ans_a,ans_b,cnt;
     8 
     9 int head[N],sumedge;
    10 struct Edge
    11 {
    12     int v,next;
    13     Edge(int v=0,int next=0): v(v),next(next){}
    14 }edge[N];
    15 void ins(int u,int v)
    16 {
    17     edge[++sumedge]=Edge(v,head[u]);
    18     head[u]=sumedge;
    19 }
    20 
    21 int tim,dfn[N],low[N];
    22 int top,Stack[N],instack[N];
    23 int sumcol,col[N],rd[N],cd[N],point[N];
    24 void DFS(int now)
    25 {
    26     dfn[now]=low[now]=++tim;
    27     Stack[++top]=now; instack[now]=1;
    28     for(int i=head[now];i;i=edge[i].next)
    29     {
    30         int v=edge[i].v;
    31         if(!dfn[v]) DFS(v),low[now]=min(low[now],low[v]);
    32         else if(instack[v]) low[now]=min(low[now],dfn[v]);
    33     }
    34     if(low[now]==dfn[now])
    35     {
    36         col[now]=++sumcol;
    37         point[sumcol]++;
    38         for(;Stack[top]!=now;top--)
    39         {
    40             col[Stack[top]]=sumcol;
    41             point[sumcol]++;
    42             instack[Stack[top]]=0;
    43         }
    44         instack[now]=0; top--;
    45     }
    46 }
    47 
    48 int main()
    49 {
    50 //    freopen("schlnet.in","r",stdin);
    51 //    freopen("schlnet.out","w",stdout);
    52     scanf("%d",&n);
    53     for(int u=1;u<=n;u++)
    54         for(int v;scanf("%d",&v)&&v;) ins(u,v);
    55     for(int i=1;i<=n;i++)
    56         if(!dfn[i]) DFS(i);
    57     for(int u=1;u<=n;u++)
    58     {
    59         for(int i=head[u];i;i=edge[i].next)
    60         {
    61             int v=edge[i].v;
    62             if(col[u]==col[v]) continue;
    63             if(!rd[col[v]]) cnt++;
    64             cd[col[u]]++; rd[col[v]]++;
    65         }
    66     }    
    67     for(int i=1;i<=sumcol;i++)
    68     {
    69         if(!rd[i]) ans_a++;
    70         if(!cd[i]) ans_b++;
    71     } ans_b=max(ans_b,sumcol-cnt);
    72     if(sumcol==1) ans_b=0;
    73     printf("%d
    %d",ans_a,ans_b);
    74     return 0;
    75 }
    ——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
  • 相关阅读:
    Building a Basic .NET Remoting Application
    Building a Basic .NET Remoting Application 之一 Building a Remotable Type
    Type Library Exporter (Tlbexp.exe)
    .NET Remoting Architecture
    Runtime Hosts
    Building a Basic .NET Remoting Application 之二 Building a Host Application
    Building a Basic .NET Remoting Application 之三 Building a Client Application
    C语言字符串大全(转自百度百科)
    作业 双向链表
    递推练习 简单n!
  • 原文地址:https://www.cnblogs.com/Shy-key/p/7214148.html
Copyright © 2011-2022 走看看