zoukankan      html  css  js  c++  java
  • POJ 1236 Network of Schools Tarjan缩点

    Network of Schools
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 22729   Accepted: 8926

    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
    哦豁,这道水题。我wa了一天,从11点WA到晚上九点。。最后发现少写一句话orz...
    忘记给u标记orzorzorzorz然后发现自己上午敲的板子就是错的 wori....
    以后我只用bin神的模板!!!

    这个题很简单,还是tarjan缩点,分别找一下入度和出度为0的点。
    最少通知几个,易得,考虑入度为0的强连通分量~
    最少加几条边,sum1和sum2的最大值?为什么呢,因为要让任意选一个点都满足条件,画一下图,得让每个强连通分量有入有出。所以要取最大值。
    贴代码
      1 #include<cstdio>
      2 #include<cstring>
      3 #include<iostream>
      4 #include<cstdlib>
      5 #include<algorithm>
      6 #include<cmath>
      7 #include<vector>
      8 #include<stack>
      9 #include<map>
     10 using namespace std;
     11 #define mem(a,b) memset(a,b,sizeof(a))
     12 #define ll long long
     13 #define inf 1000000000
     14 #define maxn 105
     15 #define maxm 100005
     16 struct node
     17 {
     18     int to,next;
     19 } edge[maxm];
     20 int n,m,head[maxn],vis[maxn],dfn[maxn],low[maxn],cdu[maxn],num[maxn],cnt,timi,stack1[maxn],top,cut,rdu[maxn];
     21 stack <int> s;
     22 void init()
     23 {
     24     memset(dfn,0,sizeof(dfn));
     25     memset(low,0,sizeof(low));
     26     memset(head,-1,sizeof(head));
     27     memset(vis,0,sizeof(vis));
     28     memset(num,0,sizeof(num));
     29     memset(cdu,0,sizeof(cdu));
     30     memset(rdu,0,sizeof(rdu));
     31     while(!s.empty()) s.pop();
     32     cnt=0;
     33     timi=1;
     34     top=0;
     35     cut=0;
     36 }
     37 void addedge(int u,int v)
     38 {
     39     edge[cnt].to=v;
     40     edge[cnt].next=head[u];
     41     head[u]=cnt;
     42     cnt++;
     43 }
     44 void tarjan(int u)
     45 {
     46     dfn[u]=timi;
     47     low[u]=timi;
     48     timi++;
     49     s.push(u);
     50     vis[u]=1;
     51     for(int i=head[u]; i!=-1; i=edge[i].next)
     52     {
     53         int v=edge[i].to;
     54         if(!dfn[v])
     55         {
     56             tarjan(v);
     57             low[u]=min(low[u],low[v]);
     58         }
     59         else
     60         {
     61             if(vis[v])
     62                 low[u]=min(low[u],dfn[v]);
     63         }
     64     }
     65     if(low[u]==dfn[u])
     66     {
     67         cut++;
     68         int x=s.top();
     69         while(x!=u)
     70         {
     71             vis[x]=0;
     72             num[x]=cut;
     73             s.pop();
     74             x=s.top();
     75         }
     76         num[x]=cut;
     77         vis[x]=0;
     78         s.pop();
     79     }
     80 }
     81 int mp[maxn][maxn];
     82 int main()
     83 {
     84     int a;
     85     while(cin>>n)
     86     {
     87         init();
     88         for(int i=1; i<=n; ++i)
     89         {
     90             while(~scanf("%d",&a)&&a) {
     91             addedge(i,a);
     92             mp[i][a]=1;
     93             }
     94         }
     95         for(int i=1; i<=n; ++i)
     96         {
     97             if(!dfn[i]) tarjan(i);
     98         }
     99         if(cut==1)
    100         {
    101             printf("1
    0
    ");
    102             continue;
    103         }
    104         for(int i=1; i<=n; ++i)
    105             for(int j=1; j<=n; ++j)
    106             {
    107                 if(mp[i][j]==1&&num[i]!=num[j]) 
    108                 {cdu[num[i]]++;rdu[num[j]]++;}
    109             }
    110         int sum1=0,sum2=0;
    111         for(int i=1; i<=cut; ++i)
    112         {
    113             if(!cdu[i])  sum1++;
    114             if(!rdu[i])  sum2++;
    115         }
    116         //cout<<sum1<<" "<<sum2<<endl;
    117         printf("%d
    %d
    ",sum2,max(sum1,sum2));
    118     }
    119     return 0;
    120 }
    View Code


  • 相关阅读:
    PHP:__get()、__set()、__isset()、__unset()、__call()、__callStatic()六个魔术方法
    概念:RPG游戏中两个兵种互相攻击的逻辑
    php怎么获取checkbox复选框的内容?
    20150724之问题
    Uploadify 之使用
    oneThink后台添加插件步骤详解
    针对各种浏览器,前端解决方案(持续更新...)
    解决IE8中select下拉列表文字上下不居中的问题
    针对IE6 7 8当独写样式
    document对象详解
  • 原文地址:https://www.cnblogs.com/TYH-TYH/p/9392854.html
Copyright © 2011-2022 走看看