poj1236——Network of Schools(强连通分量+缩点)

来源:互联网 发布:java音乐网站系统 编辑:程序博客网 时间:2024/05/22 08:12

Network of Schools
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 15674 Accepted: 6213
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

题目大意:给你一个有向图,求出最少选几个点,可以使整个图都是它们及它们的子节点。再求出最少加几条边可使原图变为强连通图。

题解:此题需要稍稍思考一下。求强连通分量并进行缩点后,设入度为0的点有a个,出度为零的点有b个。则ans1=a,ans2 = max(a,b)。另有特殊情况,当共有一个强连通分量时,ans2=0。

代码如下:

#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <stack>#include <vector>#include <cassert>#include <sstream>using namespace std;const int maxn = 5000+50;const int maxm = 1e6+10;struct Edge{    int u;    int v;    int w;}edges[maxm];vector<int> G[maxn];int pre[maxn];int sccno[maxn];            //每个点所在的强连通分量的编号 int lowlink[maxn];int mark[maxn];int is_ans[maxn];int in0[maxn];int out0[maxn];int n,m,ans,a_from;int dfs_clock,scc_cnt;stack<int> S; void dfs(int u){    pre[u]=lowlink[u]=++dfs_clock;    S.push(u);    for(int i=0;i<G[u].size();i++){        int e = G[u][i];        int v = edges[e].v;        if(!pre[v]){            dfs(v);            lowlink[u] = min(lowlink[u],lowlink[v]);        }        else if(!sccno[v]){            lowlink[u] = min(lowlink[u],pre[v]);        }    }    if(lowlink[u] == pre[u]){        scc_cnt++;        for(;;){            int x = S.top();S.pop();            sccno[x] = scc_cnt;            if(x == u) break;        }    }}void find_scc(int n){    dfs_clock = scc_cnt = 0;    memset(sccno,0,sizeof(sccno));    memset(pre,0,sizeof(pre));    for(int i=1;i<=n;i++)      if(!pre[i]) dfs(i);}int main(){    freopen("B.in","r",stdin);    freopen("B.out","w",stdout);    scanf("%d",&n); m=0;    ans = 0;dfs_clock=0;    memset(pre,0,sizeof(pre));    memset(sccno,0,sizeof(sccno));    memset(lowlink,0,sizeof(lowlink));    memset(is_ans,0,sizeof(is_ans));        for(int i=1;i<=n;i++) G[i].clear();    string s;getline(cin,s);    for(int i=1;i<=n;i++){        getline(cin,s);//cout << s <<endl;        stringstream ss(s);        int t;        while(ss >> t && t) {          m++;          edges[m].u=i;edges[m].v=t;          G[edges[m].u].push_back(m);        }    }    find_scc(n);    for(int i=1;i<=scc_cnt;i++)  in0[i] = out0[i] = 1;    for(int i=1;i<=n;i++)      for(int j=0;j<G[i].size();j++){        int e =  G[i][j];        int v = edges[e].v;        if(sccno[i]!=sccno[v]) out0[sccno[i]] = in0[sccno[v]]= 0;      }    int ans = 0;    for(int i=1;i<=scc_cnt;i++)   if(in0[i]) ans++;    printf("%d\n",ans);    int ans2 = 0;    for(int i=1;i<=scc_cnt;i++)   if(out0[i]) ans2++;    ans2 = max(ans,ans2);    if(scc_cnt==1) ans2=0;    printf("%d\n",ans2);    return 0;}
0 0
原创粉丝点击