This question is meant
problem
1: Initial release software needs at least to the number of schools, so that all schools in the network will eventually have access to the software.
2: The need to add at least a few transmission line (edge), after the payment of any software to a school, after several transmitted within the network eventually all schools have access to the software.
Actually, the problem is to ask 1, the dominating set figure how much? ? ? To solve this problem is very simple, after shrinking the map point, find the number of 0-degree point, is the dominating set. A dominant inside, all points may not strongly connected, but can be reached by this point.
The Second problem is actually more simple, we just need to enroll in and out of 0 0 of the number of points you can take a maximum, as to ask what is, quite simply, we can point out the degree of connection 0 to the degree of the above points to 0, thus ensuring strongly connected graph properties. In fact, this is complemented drawing a graph.
#include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int N = 100010,M=1000010; int ver[M],Next[M],head[N],dfn[N],low[N]; int sta[N],ins[N],c[N],in_deg[N],out_deg[N]; int n,m,tot,num,top,cnt; void add(int x,int y) { ver[++tot]=y,Next[tot]=head[x],head[x]=tot; } void tarjan(int x) { DFN [X] = Low [X] NUM = ++; // + 1'd SEQ ID STA [Top ++] = X, INS [X] = . 1 ; // stack, the stack and marked for ( int head = I [X]; I; I = the Next [I]) // begin accessing IF ! (DFN [Ver [I]]) // if not treated { Tarjan (Ver [I]); // the DFS Low [X] = min (Low [X], Low [Ver [I]]); // recursive out, who is the son of comparison / father, is the corresponding relation tree, related to the strongly connected component sub-tree root minimum things } the else IF (INS [Ver [I]]) Low [X] = min (Low [X], DFN [Ver [I]]); // Comparative who is the son / father. Is the correspondence between the link IF(DFN [X] == Low [X]) // find the smallest root of the whole subtree strongly connected component inside. { CNT ++ ; int Y; do { Y = STA [Top -], INS [Y] = 0 ; C [Y] = CNT; // SCC [CNT] .push_back (Y); } the while (X! = Y); } } void Solve ( int n-) { for ( int I = . 1 ; I <= n-; I ++ ) { IF (!dfn[i]) { tarjan(i); } } if (cnt==1) { printf("1\n0\n"); return; } for (int u=1; u<=n; ++u) { for (int j=head[u]; j; j=Next[j]) { int v=ver[j]; if (c[u]==c[v]) { continue; } out_deg[c[u]]++; in_deg[c[v]]++; } } int a,b; a=0; b=0; for (int i=1; i<=cnt; i++) { if (in_deg[i]==0) { a++; } else if (out_deg[i]==0) { b++; } } printf("%d\n",a); printf("%d\n",max(a,b)); } void init() { memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(head,0,sizeof(head)); memset(in_deg,0,sizeof(in_deg)); memset(out_deg,0,sizeof(out_deg)); memset(ins,0,sizeof(ins)); top=0; tot=0; cnt=0; num=0; } int main() { int tmp; while(~scanf("%d",&n)) { init(); for (int i=1; i<=n; i++) { while(scanf("%d",&tmp)&&tmp) { add(i,tmp); } } solve(n); } return 0; }