题目链接
http://poj.org/problem?id=1236
题目
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
题意
给定一个有向图(题目未说有没有环),选最少点使沿有向边可以访问所有点、加最少边使有向图强连通。
分析
求出图的所有强连通分量,对分量缩点,求出每一个缩点的入度和出度。统计入度为0的缩点数sum_in,出度为0的缩点数sum_out.
第一个问题的答案就是sum_in.
第二个问题的答案就是max(sum_in,sum_out)。
特别地,当强连通分量总数为1是,上述结论不成立,答案为1和0.
AC代码
//32ms 1.5MB
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <stack>
typedef long long ll;
using namespace std;const int maxn=2e4+100;
vector<int> g[maxn],G[maxn];
stack<int> sta;
int n,cnt,dep;
int low[maxn],dfn[maxn],insta[maxn];
int par[maxn];void tarjan(int u)//dfs确定时间戳,根据时间戳判断强连通分量
{low[u]=dfn[u]=++dep;//打时间戳sta.push(u);//入栈insta[u]=1;//标记在栈中for(int i=0;i<g[u].size();i++){int v=g[u][i];if(!dfn[v])//未访问过{tarjan(v);//dfslow[u]=min(low[u],low[v]);//更新low}else if(insta[v])//访问过且还在栈里面{//注意必须还在栈里low[u]=min(low[u],dfn[v]);}}if(dfn[u]==low[u])//遍历完整个深搜树后u的dfn值等于low值,则它是该深搜子树的根{//从栈顶到u的结点组成一个强连通分量++cnt;//强连通分量数while(!sta.empty()){int tmp=sta.top();sta.pop();insta[tmp]=0;par[tmp]=cnt;if(tmp==u) break;}}
}
void init()
{cnt=0;//强连通分量编号dep=0;//结点深度memset(insta,0,sizeof(insta));for(int i=0;i<=n;i++) par[i]=i;memset(dfn,0,sizeof(dfn));memset(low,0,sizeof(low));for(int i=0;i<=n;i++) g[i].clear();
}
int in[maxn],out[maxn];
int main()
{while(~scanf("%d",&n)){init();for(int u=1;u<=n;u++){int v;while(scanf("%d",&v)){if(v==0) break;g[u].push_back(v);}}for(int i=1;i<=n;i++)if(!dfn[i]) tarjan(i);if(cnt==1) ///特例{printf("%d\n%d\n",1,0);continue;}memset(in,0,sizeof(in));memset(out,0,sizeof(out));for(int i=1;i<=n;i++)for(int j=0;j<g[i].size();j++){int u=i;int v=g[i][j];if(par[u]==par[v]) continue;in[par[v]]++;out[par[u]]++;}int sum_in=0,sum_out=0;for(int i=1;i<=cnt;i++){if(!in[i]) sum_in++;if(!out[i]) sum_out++;}printf("%d\n%d\n",sum_in,max(sum_in,sum_out));}return 0;
}