tarjan+縮點+強連通定理,tarjan連通定理
C - Network of SchoolsTime Limit:1000MS Memory Limit:10000KB 64bit 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 “receiving 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 should write two 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
這個題最大的痛點在於任務二,任務二的意思就是添加最少的邊使一個有向圖變成強連通圖,有一個定理是max(n,m)其中n是出度為0的點的個數,m是入度為0的點的個數,當然,如果這個圖是強連通圖的話,就需要討論了,這時答案就是0了。將第二個問題解決掉,這個題就是一個模板題了,但是我現在仍沒有證明這個命題的正確性!有大神說顯然,我覺得應該可以證出來,希望看到這個部落格的大神可以幫幫忙,謝謝。
#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));}}
教圖演算法縮點怎進行
有向圖,每個節點帶有權值已經使用Tarjan演算法求出強連通分量(包含若干個節點)欲將強連通分量縮為兩個點,一個節點擁有最大值,一個結點擁有最小值連入該強連通分量的所有節點指向最小值的節點,強連通分量連出的節點都改為從最大值節點處連出以此消去圖中的環對於每一個強連通分量都遍曆一遍圖中所有節點,一一修改邊貌似不太現實把
強連通分量 Tarjan 演算法 Pascal 實現
用數組類比鏈表寫的,O(n+m)
遞迴版本:
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;
非遞迴版本:
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......餘下全文>>