[POJ 1236] [IOI 1996] Network of Schools, pojioi

Source: Internet
Author: User

[POJ 1236] [IOI 1996] Network of Schools, pojioi

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 "processing ing 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 shocould write two lines to the standard output. The first line shoshould contain one positive integer: the solution of subtask A. The second line shoshould contain the solution of subtask B.

Sample Input

52 4 3 04 5 0001 0

Sample Output

12

Source

IOI 1996


Given a directed graph, the minimum number of vertices can be obtained from these vertices. The minimum number of edges must be added to reach all vertices from any point.

First, we need to introduce a theorem: in DAG, for all vertices whose input degree is not 0, there must be a point with an inbound value of 0 (because the point with an inbound value of 0 goes backwards, it will certainly be able to reach a point with an inbound value of not 0)

Therefore, this question can be scaled down by tarjan to find the number of points with 0 inbound. This is the answer to the first question.

The answer to the second question is the minimum value of the point with the inbound degree 0 and the point with the outbound degree 0.

For this question, we only need to record the strongly connected component that each vertex belongs to in the tarjan process, and then calculate the output.

# Include <iostream> # include <stdio. h> # include <string. h> # define MAXE 500 # define MAXV 3000 using namespace std; int N; struct edge {int u, v, next;} edges [MAXV]; int head [MAXE], nCount = 0; int dfn [MAXE], low [MAXE], index = 0; int belong [MAXE], tot = 0; // belong [I] = strongly connected component of point I, tot = Total number of strongly connected components bool inStack [MAXE]; int stack [MAXE * 4], top = 0; bool map [MAXE] [MAXE]; int inDegree [MAXE], outDegree [MAXE], inZero = 0, outZero = 0; // inbound, outbound int max (Int a, int B) {if (a> B) return a; return B;} int min (int a, int B) {if (a <B) return; return B;} void AddEdge (int U, int V) {edges [++ nCount]. u = U; edges [nCount]. v = V; edges [nCount]. next = head [U]; head [U] = nCount;} void tarjan (int u) {dfn [u] = low [u] = ++ index; stack [++ top] = u; // point inbound stack inStack [u] = true; for (int p = head [u]; p! =-1; p = edges [p]. next) {int v = edges [p]. v; if (! Dfn [v]) {tarjan (v); low [u] = min (low [u], low [v]);} else if (inStack [v]) {low [u] = min (low [u], dfn [v]) ;}} int v; if (dfn [u] = low [u]) {tot ++; do {v = stack [top --]; belong [v] = tot; inStack [v] = false;} while (u! = V) ;}} int main () {int to; cin> N; memset (head,-1, sizeof (head); for (int I = 1; I <= N; I ++) {while (1) {cin> to; if (to = 0) break; AddEdge (I, ); map [I] [to] = true ;}}for (int I = 1; I <= N; I ++) if (! Dfn [I]) tarjan (I); for (int I = 1; I <= N; I ++) for (int j = 1; j <= N; j ++) {if (map [I] [j] & belong [I]! = Belong [j]) {inDegree [belong [j] ++; outDegree [belong [I] ++ ;}} for (int I = 1; I <= tot; I ++) {if (! InDegree [I]) inZero ++; if (! OutDegree [I]) outZero ++;} if (tot = 1) cout <1 <endl <0 <endl; else cout <inZero <endl <max (inZero, outZero) <endl; return 0 ;}

Zookeeper


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.