Network of schools
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 11180 |
|
Accepted: 4448 |
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
Test Instructions: Given the connection of each computer, ask two questions: 1, at least to several computers in the network to put files, the entire network of all computers can immediately obtain the file; 2. To add a few one-way connections to the network can make the network as long as the delivery of a file to any computer, The file is available to all computers. problem: The first garbow question, it is necessary to note that when a given diagram is a connected diagram, you do not need to add any edges. Improved version: 0ms
#include <stdio.h> #include <string.h> #define MAXN 102#define maxm MAXN * maxnint HEAD[MAXN], STA1[MAXN], sta2 [MAXN], Scc[maxn];int LOW[MAXN], id, ID1, id2, sec, Sccnum;bool In[maxn], out[maxn];struct node{int to, next; e[maxm];void Addedge (int u, int v) {e[id].to = v; E[id].next = Head[u]; Head[u] = id++;} void Getmap (int n) {memset (head,-1, sizeof (int) * (n + 1)); int I, V; id = 0; for (i = 1; I <= n; ++i) while (scanf ("%d", &v), V) Addedge (i, v); }void garbow (int pos) {sta1[id1++] = sta2[id2++] = pos; Low[pos] = ++sec; for (int i = head[pos]; i =-1; i = e[i].next) {if (!low[e[i].to]) Garbow (e[i].to); else if (!scc[e[i].to]) while (low[e[i].to] < low[sta2[id2-1]])--id2; if (scc[e[i].to]) in[scc[e[i].to]] = 1; } if (sta2[id2-1] = = pos) {++sccnum;--id2; int v; do{v = sta1[--id1]; SCC[V] = Sccnum; } while (STA1[ID1]! = POS); }}void Solve (int n) {memset (low, 0, sizeof (int) * (n + 1)); memset (SCC, 0, sizeof (int) * (n + 1)); memset (in, 0, sizeof (BOOL) * (n + 1)); memset (out, 0, sizeof (BOOL) * (n + 1)); int I, j, ans1 = 0, ans2 = 0; Id1 = Id2 = sec = Sccnum = 0; for (i = 1; I <= n; ++i) if (!low[i]) Garbow (i); for (i = 1; I <= N, ++i) for (j = head[i]; J! = 1; j = e[j].next) if (scc[i]! = Scc[e[j].to]) { Out[scc[i]] = 1; Break if (sccnum! = 1) {for (i = 1; I <= sccnum; ++i) {if (!out[i]) ++ans2; if (!in[i]) ++ans1; } if (Ans1 > Ans2) ans2 = ans1; }else ans1 = 1; printf ("%d\n%d\n", ans1, Ans2);} int main () {int n; while (scanf ("%d", &n) = = 1) {Getmap (n); Solve (n); } return 0;}
Original 32ms:
#include <stdio.h> #include <string.h> #define MAXN 102#define maxm MAXN * maxnint HEAD[MAXN], STA1[MAXN], sta2 [MAXN], Scc[maxn];int LOW[MAXN], id, ID1, id2, sec, Sccnum;bool In[maxn], out[maxn];struct node{int to, next; e[maxm];void Addedge (int u, int v) {e[id].to = v; E[id].next = Head[u]; Head[u] = id++;} void Getmap (int n) {memset (head,-1, sizeof (int) * (n + 1)); int I, V; id = 0; for (i = 1; I <= n; ++i) while (scanf ("%d", &v), V) Addedge (i, v); }void garbow (int pos) {sta1[id1++] = sta2[id2++] = pos; Low[pos] = ++sec; for (int i = Head[pos]; I! =-1; i = e[i].next) if (!low[e[i].to]) Garbow (e[i].to); else if (!scc[e[i].to]) while (low[e[i].to] < low[sta2[id2-1]])--id2; if (sta2[id2-1] = = pos) {++sccnum;--id2; int v; do{v = sta1[--id1]; SCC[V] = Sccnum; } while (STA1[ID1]! = POS); }}void solve (int n) {memset (low, 0, sizeof (int) * (n + 1)); memset (SCC, 0, sizeof (int) * (n + 1)); memset (in, 0, sizeof (BOOL) * (n + 1)); memset (out, 0, sizeof (BOOL) * (n + 1)); int I, j, ans1 = 0, ans2 = 0; Id1 = Id2 = sec = Sccnum = 0; for (i = 1; I <= n; ++i) if (!low[i]) Garbow (i); for (i = 1; I <= N, ++i) for (j = head[i]; J! = 1; j = e[j].next) if (scc[i]! = Scc[e[j].to]) { Out[scc[i]] = 1; In[scc[e[j].to]] = 1; if (sccnum! = 1) {for (i = 1; I <= sccnum; ++i) {if (!out[i]) ++ans2; if (!in[i]) ++ans1; } if (Ans1 > Ans2) ans2 = ans1; }else ans1 = 1; printf ("%d\n%d\n", ans1, Ans2);} int main () {int n; while (scanf ("%d", &n) = = 1) {Getmap (n); Solve (n); } return 0;}
POJ1236 Network of schools "strong connected component Garbow"