Pku1236 Network of Schools
Description
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
Output
Sample Input
5
2 4 3 0
4 5 0
0
0
1 0
Sample Output
1 2题目大意:N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输, 问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件。 2,至少需要添加几条传输线路(边),使任意向一个学校发放软件后,经过若干次传送,网络内所有的学校最终都能得到软件。有用的定理:有向图中所有入度不为0的点,一定可以由某个入度为0的点出发可达。(由于无环,所以从任何入度不为0的点往回走,必然终止于为0的点)。 题解:1.求出所有强连通分量。 2.每个强连通分量缩成一点,则形成一个有向无环图DAG,DAG上面有多少个入度为0的定点,问题1的答案就是多少。 3.在DAG上要加几条边,才能使得DAG变成强连通的,问题2的答案就是多少。 加边的方法:要为每个入度为0的点添加出边。 假定有n个入度为0的点,m个出度为0的点,max(m,n)就是问题2的答案(证明难,略)。 代码与 PKU 2186 Popular Cows 神似,只要改一些小细节就好了。 详见PKU2186 Popular Cows 受欢迎的牛 本题代码:#include<bits/stdc++.h> using namespace std; int n,k,ans,group,cnt,top,total,ans2,x,tot=0; int belong[10010],du[10010],first[10010],dfn[10010],low[10010],st[10010],du2[10010]; struct node { int s,e,next; }v[50050]; int read() { int x=0,f=1;char ch=getchar(); while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } void add(int x,int y) { tot++; v[tot].next=first[x]; first[x]=tot; } void tarjan(int k) { dfn[k]=++cnt; low[k]=cnt; st[++top]=k; int ttt=top; for(int i=first[k];i;i=v[i].next) if(!belong[v[i].e]) { if(!dfn[v[i].e])tarjan(v[i].e); low[k]=min(low[k],low[v[i].e]); } if(dfn[k]==low[k]) { total++; for(int i=ttt;i<=top;i++) belong[st[i]]=total; top=ttt-1; } } int main() { n=read(); for(int i=1;i<=n;i++) { x=read(); while(x) { add(i,x); v[++k].s=i;v[k].e=x; x=read(); } } for(int i=1;i<=n;i++) if(!belong[i])tarjan(i); for(int i=1;i<=k;i++) if(belong[v[i].s]!=belong[v[i].e]) { ++du[belong[v[i].s]]; ++du2[belong[v[i].e]]; } for(int i=1;i<=total;i++) { if(du2[i]==0)ans++; if(du[i]==0)ans2++; } ans2=max(ans,ans2); if(total==1)ans2=0; printf("%d\n%d\n",ans,ans2); return 0; }