1. 程式人生 > 實用技巧 >POJ.1236-Network of Schools(Tarjan縮點+思維)

POJ.1236-Network of Schools(Tarjan縮點+思維)

Language:Network of Schools
Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 30208Accepted: 11857

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

5
2 4 3 0
4 5 0
0
0
1 0

Sample Output

1
2

Source

IOI 1996 題意:略; 思路:tarjan演算法縮點,重點在求解,在tarjan演算法將圖改為DAG後,注意我們把入度為0的點稱為起點,相應的我們把出度為0的點稱為終點。第一問詢問至少需要幾個學校,答案是起點的數量,因為由一個起點能遍歷到某些點,但永遠不可能遍歷到另外一個起點,所以起點的數量就是第一問的答案; 第二問詢問至少需要添幾條邊,將有向圖變成強連通圖,不妨還是站在起點終點的角度去考慮,我們要變成強連通圖,必須保證消滅所有的起點和終點,因為一個點若是起點,那麼除自身外任何一個點均到達不了他,一個點若是終點,那麼他到達不了任何點,所以應在圖中終點連起點,答案就是max(起點數量,終點數量),需要注意的是存在一種狀態,即圖本身便是強連通圖,即它本身既是起點又是終點,顯然情況ans為0; 程式碼如下: ```

include

include

include

include

include

include

include

define pb push_back

using namespace std;
const int N = 1e4+10,M=2e5+10;
int n,var[M],head[N],nxt[N],tot,t,cnt,id[N],all[N],dfn[N],low[N];bool st[N];
void add(int x,int y){
var[++tot] = y,nxt[tot] = head[x],head[x] = tot;
}
vectors[N];
stackG;
void tarjan(int x){
low[x] = dfn[x] = ++t;
st[x] = true;G.push(x);
for(int i=head[x];i;i=nxt[i]){
int y = var[i];
if(!dfn[y]){
tarjan(y);
low[x] = min(low[x],low[y]);
}
else if(st[y] == true)low[x] = min(low[x],dfn[y]);
}
if(dfn[x] == low[x]){
int k;++cnt;
do{
k = G.top();
st[k] = false;
G.pop();
id[k] = cnt;all[cnt]++;
}while(k!=x);
}
}
int in[N],out[N];bool vis[N];int x;
void solve(){
scanf("%d",&n);
//tot = 1;
for(int i=1;i<=n;++i)while(true){
int v;
scanf("%d",&v);
if(v == 0)break;
//if(v == i)continue;
add(i,v);
}
for(int i=1;i<=n;++i)if(!dfn[i])tarjan(i);
for(int i=1;i<=n;++i){
for(int y=head[i];y;y=nxt[y]){
int p = var[y];
if(id[p]!=id[i]){
in[id[p]]++;out[id[i]]++;
//s[id[i]].pb(id[p]);
}
}
}
int ans2 = 0,ans1 = 0;
for(int i=1;i<=cnt;++i){
if(in[i] == 0){
ans1++;
}
if(out[i] == 0)ans2++;
}
if(cnt == 1)ans2 = 0;
else ans2 = max(ans1,ans2);
printf("%d\n%d\n",ans1,ans2);
}

int main(){
solve();
return 0;
}

寫在最後,感覺這個將圖補成強連通圖的問題可以當做一個結論,結論就是max(入度為0的點,出度為0的點),有些許的偏思維;