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
The first question of the simulation today
test instructions is to give a diagram, the first ask for the minimum number of points to choose, so that from these points can traverse the entire map. The second question is at least how many forward edges are added, making the entire graph into a strong connected graph
First ask SB. Tarjan the number of points in which the indentation is 0, the second question is a bit of a pit. The g{e,v} represents a weakly connected graph, I (g) Represents the number of points in degrees 0 in G, and O (g) Represents the number of points in G that have a degree of 0. The
originally I thought was Σmax (I (i), O (i)). It's actually Max (Ōi (i), σo (i)). The difference is easy to see.
#include <cstdio> #include <iostream> #define LL long longusing namespace Std;inline ll read () {ll X=0,f=1;char Ch=getchar (); while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} struct Edge{int to,next;} E[1000010];int n,cnt,cnt3,tt,sumi,sumo;int head[100010];bool inset[100010];int dfn[100010],low[100010];int zhan[ 100010],top;int belong[100010];int i[100010],o[100010];inline void ins (int u,int v) {E[++cnt].to=v;e[cnt].next=head[u ];head[u]=cnt;} inline void dfs (int x) {zhan[++top]=x;inset[x]=1;dfn[x]=low[x]=++tt;for (int i=head[x];i;i=e[i].next) {if (!dfn[e[i]. To] {DFS (e[i].to); Low[x]=min (low[x],low[e[i].to]); }else if (inset[e[i].to]) low[x]=min (low[x],dfn[e[i].to]); }if (Low[x]==dfn[x]) {cnt3++;int p=-1;while (p!=x) {p=zhan[top--];belong[p]=cnt3;inset[p]=0;}}} inline void Tarjan () {for (int i=1;i<=n;i++) if (!dfn[i]) DFS (i);} int main () {n=read (); for (int i=1;i<=n;i++) {int x; while (scanf ("%d", &x) &&x) ins (i,x);} Tarjan (); for (int. i=1;i<=n;i++) {for (int j=head[i];j;j=e[j].next) if (belong[i]!=belong[e[j].to]) {o[belong[i]]++; i[belong[e[j].to]]++; }}if (cnt3==1) {printf ("1\n0\n"); return 0;} for (int i=1;i<=cnt3;i++) {if (! I[i]) sumi++; if (! O[i]) sumo++;} printf ("%d\n%d\n", Sumi,max (Sumi,sumo)); return 0;}
poj1236 Network of schools