Tarjan + contraction point + strong connected theorem, tarjan connected Theorem
C-Network of SchoolsTime Limit:1000 MSMemory Limit:10000KB64bit IO Format:% I64d & % I64uSubmit Status
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
The biggest difficulty of this question lies in Task 2, which means to add the least edge to make a directed graph into a strongly connected graph, and one theorem is max (n, m) where n is the number of points with an outbound degree of 0, and m is the number of points with an inbound degree of 0. Of course, if this graph is a strongly connected graph, we need to discuss it, then the answer is 0. Solve the second question. This question is a template question, but I still haven't proved the correctness of this question! Obviously, I think we can prove it. I hope you can help me with this blog. Thank you.
#include<stdio.h>#include<stack>#include<string.h>#include<algorithm>using namespace std;int dfn[120];int belong[120],bnt,instack[120];int index,out[120],in[120],low[120];int map[120][120];stack<int>S;void tarjan(int i){dfn[i] = low[i] = ++index;S.push(i);instack[i] = 1;for(int j = 1;j<=map[i][0];j++){int k = map[i][j];if(!dfn[k]){tarjan(k);low[i] = min(low[i],low[k]);}else if(instack[k])low[i] = min(low[i],dfn[k]);} if(low[i] == dfn[i]){bnt++;int j;do{j = S.top();S.pop();instack[j] = 0;belong[j] = bnt;}while(i!=j);}}int main(){int n,m,ans,ans1;while(~scanf("%d",&n)){memset(dfn,0,sizeof(dfn));memset(low,0,sizeof(low));memset(instack,0,sizeof(instack));memset(out,0,sizeof(out));memset(in,0,sizeof(in));memset(map,0,sizeof(map));bnt = index = 0;ans = ans1 = 0;for(int i=1;i<=n;i++){while(scanf("%d",&m),m)if(m)map[i][++map[i][0]] = m;}for(int i=1;i<=n;i++)if(!dfn[i])tarjan(i);for(int i=1;i<=n;i++){for(int j = 1;j<=map[i][0];j++){if(belong[i]!=belong[map[i][j]]){out[belong[i]]++;in[belong[map[i][j]]]++;}}}for(int i=1;i<=bnt;i++){if(out[i]==0)ans++;if(in[i]==0)ans1++;}printf("%d\n",ans1);if(bnt ==1)printf("0\n");else printf("%d\n",max(ans1,ans));}}
How to scale down graph algorithms
Directed Graph. Each node has a valid value. You have used the Tarjan algorithm to find the strongly connected component (including several nodes). To reduce the strongly connected component to two points, one node has the maximum value, A node connects all nodes of the strongly connected component to the minimum value, the nodes connected by the strongly connected component are connected from the maximum node to eliminate the loops in the graph. For each strongly connected component, all nodes in the graph are traversed. It seems unrealistic to modify the edges one by one.
Pascal implementation of the Tarjan Algorithm for strongly connected components
Use an array to simulate the writes of the linked list, O (n + m)
Recursive version:
Procedure tarjan (u: longint );
Var
I, y, v: longint;
Begin
Inc (time );
Low [u]: = time;
DFN [u]: = time;
Visit [u]: = true;
Ins [u]: = true;
Inc (t );
Stack [t]: = u;
I: = last [u];
While I <> 0 do
Begin
Y: = e [I]. y;
If not (visit [y]) then
Begin
Tarjan (y );
Low [u]: = min (Low [u], Low [y]);
End
Else
If InS [y] then Low [u]: = min (Low [u], DFN [y]);
I: = e [I]. next;
End;
If DFN [u] = Low [u] then
Begin
Inc (sumH );
Repeat
V: = stack [t];
Dec (t );
Belong [v]: = sumH;
InS [v]: = false;
Until u = v;
End;
End;
Non-recursive version:
Procedure tarjan;
Var
J, k: longint;
Flag, flag1: boolean;
Begin
Repeat
If not (visit [u]) then
Begin
Inc (time );
Low [u]: = time;
DFN [u]: = time;
Visit [u]: = true;
InS [u]: = true;
Inc (t );
Stack [t]: = u;
End;
Flag: = false;
K: = last [u];
Flag1: = true;
While k <> 0 do
Begin
J: = e [k]. y;
If not (visit [j]) then
Begin
Flag: = true;
Pre [j]: = u;
L [j]: = 1;
U: = j;
Break;
End
Else
If did [j] then
Begin
If l [j] = 1 then
Begin
If low [j] <low [u] then low [u]: = dfn [j];
L [j]: = 0;
End
Else
If InS [j] then
Begin
If dfn [j] <low [u] th ...... remaining full text>