POJ topic 1236 Network of schools (strong unicom)

Source: Internet
Author: User

Network of schools
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 12015 Accepted: 4783

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 1996wa several times, when there is a unicom point when the output device 1 0 The main idea: there are n schools, schools can pass information, such as school A can convey information to B, that is a--> B, but b Not necessarilyCan pass information to a. Tell you which schools each school can send information to, and then have two questions:
    original information to ensure that all schools are able to receive information.
    Question two: How many groups of relationships should be added at least (each set of relationship types, such as right: A can pass information to B) to ensure that any school's original information is available to all other schools.
AC Code
#include <stdio.h> #include <string.h> #include <vector> #include <iostream>using namespace std ; #define MIN (A, b) (A&GT;B?B:A) #define MAX (A, B) (a>b?a:b)/*struct s{int U,v,next;} Edge[110*110*2*100];*/int Low[110],dfn[110],ins[110],belong[110],head[110],stack[110],in[110],out[110];int CNT, Taj,top,time;vector<int>vt[110];void init () {memset (dfn,-1,sizeof (DFN)); memset (ins,0,sizeof (INS)); Memset ( Belong,-1,sizeof (DFN)); Memset (low,-1,sizeof (Low)); Memset (stack,0,sizeof (stack)); Memset (Head,-1,sizeof (head)); Memset (out,0,sizeof (out)), memset (In,0,sizeof (in)); cnt=top=time=taj=0;} /*void Add (int u,int v) {edge[cnt].u=u;edge[cnt].v=v;edge[cnt].next=head[u];head[u]=cnt++;} */void Tarjan (int u) {dfn[u]=low[u]=time++;ins[u]=1;stack[top++]=u;for (int i=0;i<vt[u].size (); i++) {int v=vt[u][i ];if (dfn[v]==-1) {Tarjan (v); Low[u]=min (Low[v],low[u]);} ElseIf (Ins[v]) low[u]=min (Dfn[v],low[u]);} if (Low[u]==dfn[u]) {int now;taj++;d O{now=stack[--top];ins[now]=0;belong[now]=taj;} while (now!=u);}} int main() {int n;while (scanf ("%d", &n)!=eof) {int i;init (); for (i=0;i<=n;i++) vt[i].clear (); for (i=1;i<=n;i++) {int V ; while (scanf ("%d", &v)!=eof,v) {//add (i,v); Vt[i].push_back (v);}} for (i=1;i<=n;i++) {if (dfn[i]==-1) Tarjan (i);} if (taj==1) {printf ("1\n0\n"); continue;} for (i=1;i<=n;i++) {//int u=edge[i].u;//int v=edge[i].v;//printf ("%d%d\n", U,belong[u]),//printf ("%d%d\n", V, BELONG[V]); int u=i;for (int j=0;j<vt[u].size (); j + +) {int v=vt[u][j];if (Belong[u]!=belong[v]) {in[belong[v]]++;out [Belong[u]]++;}}} int Ans1=0,ans2=0;for (i=1;i<=taj;i++) {if (in[i]==0) ans1++;if (out[i]==0) ans2++;} printf ("%d\n", ans1);p rintf ("%d\n", Max (ANS1,ANS2));}}

POJ topic 1236 Network of schools (strong unicom)

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.