Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 17549 | Accepted: 6943 |
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
Source
题意:N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输,问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件。2,至少需要添加几条传输线路(边),使任意向一个学校发放软件后,经过若干次传送,网络内所有的学校最终都能得到软件
解题思路:求出所有强连通分量,每个强连通分量缩成一点,则形成一个有向无环图。图里有多少个入度为0的顶点,问题1的答案就是多少。在图上要加几条边,才能使得图变成强连通的,问题2的答案就是多少。要为每个入度为0的点添加入边,为每个出度为0的点添加出边。若有 n 个入度为0的点,m个出度为0的点,把所有入度为0的点编号 0,1,2,3,4 ....N -1,每次为一个编号为i的入度为0的点可达的出度为0的点,添加一条出边,连到编号为(i+1)%N 的那个出度0点,这需要加n条边。若 m <= n,则加了这n条边后,已经没有入度0点。若 m > n,则还有m-n个出度0点,则从这些点以外任取一点,和这些点都连上边即可,这还需加m-n条边。所以,max(m,n)就是第二个问题的解
注意:当只有一个强连通分支的时候,就是缩点后只有一个点,虽然入度出度为0的都有一个,但是实际上不需要增加清单的项了,所以答案是1,0;
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <climits>
using namespace std;
#define LL long long
const int INF=0x3f3f3f3f;
const int N=1010;
int n,m;
struct Node
{
int v,nt;
}edge[N*100];
int s[N],cnt;
int dfn[N],low[N],id[N],dep;
bool vis[N],instack[N];
int in[N],out[N];
int res;
stack<int>st;
void AddEdge(int u,int v)
{
edge[cnt].v=v;
edge[cnt].nt=s[u];
s[u]=cnt++;
}
void tarjan(int u,int pre)
{
st.push(u);
instack[u]=true;
vis[u]=true;
dfn[u]=low[u]=++dep;
for(int i=s[u]; ~i; i=edge[i].nt)
{
int v=edge[i].v;
if(v==pre) continue;
if(!vis[v]) // 生成树的边.
{
tarjan(v,u);
low[u]=min(low[u],low[v]);
}
else if(instack[v])//在栈中,回边.
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])//顶点u为根的子树是一个强连同块
{
int t;
do
{
id[t=st.top()]=res;
st.pop();
instack[t]=false; //low[t] = n;
}
while(t!=u);
res++;//强连通分量增加
}
}
void solve()
{
res=0,dep=0;
while(!st.empty()) st.pop();
memset(vis,0,sizeof vis);
memset(instack,0,sizeof instack);
for(int i=1; i<=n; i++)
if(!vis[i]) tarjan(i,0);
// Debug
/*for(int i = 1; i <= n; i++)
printf("dfn[%d] = %d, low[%d] = %d\n", i,dfn[i], i,low[i]);
for(int i = 1; i <= n; i++)
printf("id[%d] = %d\n", i, id[i] );*/
memset(out,0,sizeof out);
memset(in,0,sizeof in);
for(int u=1; u<=n; u++)
{
for(int i=s[u]; ~i; i=edge[i].nt)
{
int v=edge[i].v;
if(id[v]==id[u]) continue;
in[id[v]]++,out[id[u]]++;
}
}
int sum1=0,sum2=0;
for(int i=0; i<res; i++)
{
if(!in[i]) sum1++;
if(!out[i]) sum2++;
}
if(res==1) printf("1\n0\n");
else printf("%d\n%d\n",sum1,max(sum1,sum2));
}
int main()
{
while(~scanf("%d",&n))
{
memset(s,-1,sizeof s);
cnt=0;
for(int i=1; i<=n; i++)
{
int v;
while(1)
{
scanf("%d",&v);
if(!v) break;
AddEdge(i,v);
}
}
solve();
}
return 0;
}