P2746 [usac5.3] campus Network of Schools, p2746usaco5.3
Description
Some schools connect to a computer network. Those schools have an agreement: each school will distribute software to other schools (known as "Accept schools "). Note that even if B is in the distribution list of school A, A is not necessarily in the list of school B.
You need to write A program for computing. According to the Protocol, in order to allow all schools on the network to use new software, you must accept the minimum number of schools with new software copies (subtask ). Furthermore, we want to determine that by sending new software to any school, the software will be distributed to all schools on the network. To complete this task, we may have to expand the list of receiving schools to add them to new members. Computing requires at least a few extensions so that no matter which school we send new software to, it will reach all other schools (subtask B ). An extension is to introduce a new member to the receiving school list of a school.
Input/Output Format
Input Format:
The first line of the input file contains an integer N: Number of Schools in the network (2 <= N <= 100 ). The school uses the first N positive integers.
Each row in the next N rows represents a list of receiving schools (distribution list ). Line I + 1 includes the identifier of the receiving school of school I. Each list ends with 0. The empty list is represented by only one 0.
Output Format:
Your program should output two rows in the output file.
The first line should include A positive integer: the solution of subtask.
The second line should include the solution of subtask B.
Input and Output sample input sample #1:
52 4 3 04 5 0001 0
Output sample #1:
12
Description
The question translation is from NOCOW.
USACO Training Section 5.3
Question 1: How many school software should be provided to ensure that all schools have software, that is, to calculate the number of points whose input degree is 0 after the contraction point (because if the input degree is 0, no other school can transmit the software to it)
- Question 2: After the contraction, the inbound and outbound degrees of all schools are greater than 0 (in this way, software can be provided to any school, and software can be used by all schools.
A strongly connected graph requires special determination.
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <algorithm> 6 # include <queue> 7 # include <stack> 8 using namespace std; 9 const int MAXN = 1000001; 10 static void read (int & n) 11 {12 char c = '+'; int x = 0; bool flag = 0; 13 while (c <'0' | c> '9') {c = getchar (); if (c = '-') flag = 1 ;} 14 while (c> = '0' & c <= '9') {x = (x <1) + (x <3) + (c-48 ); c = getchar ();} 15 flag = 1? N =-x: n = x; 16} 17 struct node 18 {19 int u, v, nxt; 20} edge [MAXN]; 21 int head [MAXN]; 22 int num = 1; 23 void add_edge (int x, int y) 24 {25 edge [num]. u = x; 26 edge [num]. v = y; 27 edge [num]. nxt = head [x]; 28 head [x] = num ++; 29} 30 int dfn [MAXN]; 31 int low [MAXN]; 32 int tot = 0; 33 bool vis [MAXN]; 34 int color [MAXN]; 35 int colornum; 36 stack <int> s; 37 int rudu [MAXN]; 38 int chudu [MAXN]; 39 void tarjan (int n Ow) 40 {41 dfn [now] = low [now] = ++ tot; 42 vis [now] = 1; 43 s. push (now); 44 for (int I = head [now]; I! =-1; I = edge [I]. nxt) 45 {46 if (! Dfn [edge [I]. v]) 47 {48 tarjan (edge [I]. v); 49 low [now] = min (low [now], low [edge [I]. v]); 50} 51 else if (vis [edge [I]. v]) // common ancestor 52 {53 low [edge [I]. u] = min (low [edge [I]. u], dfn [edge [I]. v]); 54} 55} 56 if (dfn [now] = low [now]) // root 57 {58 colornum ++; 59 while (now! = S. top () 60 {61 if (! Color [s. top ()]) 62 color [s. top ()] = colornum; 63 vis [s. top ()] = 0; 64 s. pop (); 65} 66 if (! Color [s. top ()]) 67 color [s. top ()] = colornum; 68 vis [s. top ()] = 0; 69 s. pop (); 70} 71} 72 int main () 73 {74 // freopen ("schlnet. in "," r ", stdin); 75 // freopen (" schlnet. out "," w ", stdout); 76 int n; 77 memset (head,-1, sizeof (head); 78 read (n ); 79 for (int I = 1; I <= n; I ++) 80 {81 int to; 82 while (scanf ("% d", & to) &! = 0) 83 add_edge (I, to); 84} 85 86 for (int I = 1; I <= n; I ++) 87 if (! Dfn [I]) 88 tarjan (I); 89 memset (vis, 0, sizeof (vis); 90 91 for (int I = 1; I <= n; I ++) 92 for (int j = head [I]; j! =-1; j = edge [j]. nxt) 93 if (color [edge [j]. u]! = Color [edge [j]. v]) 94 {95 rudu [color [edge [j]. v] ++; 96 chudu [color [edge [j]. u] ++; 97} 98 int ans1 = 0; 99 int ans2 = 0; 100 for (int I = 1; I <= colornum; I ++) 101 {102 if (! Rudu [I]) 103 ans1 ++; 104 if (! Chudu [I]) 105 ans2 ++; 106} 107 ans2 = max (ans2, ans1); 108 if (colornum = 1) 109 ans1 = 1, ans2 = 0; 110 printf ("% d \ n % d", ans1, ans2); 111 return 0; 112}