[ACM] POJ 1236 Network of schools (with strongly connected components)

Source: Internet
Author: User
Tags ord

Network of schools
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 11407 Accepted: 4539

Description

A number of schools is connected to a computer network. Agreements has been developed among those Schools:each School maintains a list of schools to which it distributes Softwa Re (the "Receiving schools"). Note that if B was in the distribution list of school A, then a does not necessarily appear in the list of school B
You is to write a program this computes the minimal number of schools that must receive a copy of the new software in Ord Er for the software to reach all schools in the network according to the agreement (Subtask A). As a further task, we want to ensure is sending the copy of new software to an arbitrary school, this software would r Each of the schools in the network. To achieve this goal we are having to extend the lists of receivers by new members. Compute the minimal number of extensions that has to is made so this whatever school we send the new software to, it'll 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 a integer n:the number of schools in the network (2 <= N <= 100). The schools is 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 is ends with a 0. An empty list contains a 0 alone in the line.

Output

Your program should write, 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

52 4 3 04 5 0001 0

Sample Output

12

Source

IOI 1996



Test instructions and solving ideas from Peking University summer Training Courseware:










An example of this is explained:


Code:

#include <iostream> #include <algorithm> #include <stdio.h> #include <string.h> #include < vector>using namespace Std;const int maxn=102;vector<int>g[maxn],gre[maxn];//store forward and inverse map int ord[maxn];//forward Search, The number of vertices bool Vis[maxn];int out[maxn];//translates to the out-of-size int in[maxn];int of each indent after the Dag belong[maxn];//the current vertex belongs to which collection, equivalent to staining, The current vertex is dyed into what color int ans[maxn];//Each color includes how many vertices, that is, the number of strong Unicom components int color;//represents a different color int no;//forward search sort of number int n;//vertex number void dfs1 (int u)//    Starting from the current U vertex dfs{vis[u]=1;        for (int i=0;i<g[u].size (); i++) {int v=g[u][i];    if (!vis[v]) DFS1 (v);    } ord[no++]=u;//for each vertex number}void dfs2 (int u) {vis[u]=1;        belong[u]=color;//current vertex u is dyed as color for (int i=0;i<gre[u].size (); i++) {int v=gre[u][i];        if (!vis[v]) {DFS2 (v);    }}}void Kosaraju () {color=1,no=1;    memset (in,0,sizeof (VIS));    Memset (out,0,sizeof (out));    memset (vis,0,sizeof (VIS));    for (int i=1;i<=n;i++) if (!vis[i]) DFS1 (i); memset (Vis, 0,sizeof (VIS));        for (int i=no-1;i>=1;i--)//After coding, search {int v=ord[i] from automatic arranging's largest start;            if (!vis[v]) {DFS2 (v);        color++; }}//construct dag for (int i=1;i<=n;i++) {for (int j=0;j<g[i].size (); j + +) {if (belong[            I]==BELONG[G[I][J]]) continue;            out[belong[i]]++;        in[belong[g[i][j]]]++;    }} int inzero=0,outzero=0;        for (int i=1;i<color;i++) {if (!in[i]) inzero++;    if (!out[i]) outzero++;    } if (color==2) printf ("1\n0\n"); else printf ("%d\n%d\n", Inzero,max (Inzero,outzero));}    int main () {scanf ("%d", &n);    int to;            for (int i=1;i<=n;i++) {while (scanf ("%d", &to) &&to) {g[i].push_back (to);        Gre[to].push_back (i);    }} Kosaraju (); return 0;}




[ACM] POJ 1236 Network of schools (with strongly connected components)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.