POJ 1236 IOI 1236 Network of School [强连通分量] [缩点]

来源:互联网 发布:如何注册淘宝卖家 编辑:程序博客网 时间:2024/06/06 07:16

Network of Schools
Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %lld & %llu

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


第一问是求要至少在多少个学校分发软件才能让所有学校都得到,第二问求出至少增加多少条边使得原图是强连通分量。。
那么第一问就直接找出所有SCC,然后统计SCC的入度为0的点的个数ans1,然后第二问再求出所有SCC出度为0的个数ans2,然后答案就是max(ans1,ans2)。。。
因为原图要变成SCC那么必然使每个点的出度入度都非0,那么一个入度和一个出度可以抵消,那么最多的就是要加的边数。。
另外如果不保证原图只有一个连通分量的话,那还要加上连通分量之间的。。。

#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<vector>#include<queue>#include<stack>#include<map>#include<set>#include<string>#include<iomanip>#include<ctime>#include<climits>#include<cctype>#include<algorithm>#ifdef WIN32#define AUTO "%I64d"#else#define AUTO "%lld"#endifusing namespace std;const int INF=0x3f3f3f3f;const int maxn=105;struct Edge{    int to,next;}edge[maxn*maxn];int head[maxn];int maxedge;inline void addedge(int u,int v){    edge[++maxedge]=(Edge){v,head[u]};    head[u]=maxedge;}int n;void init(){    scanf("%d",&n);    memset(head,-1,sizeof(head));    maxedge=-1;    int tmp=-1;    for(int i=1;i<=n;i++)        while(true)        {            scanf("%d",&tmp);            if(!tmp) break;            addedge(i,tmp);        }}int dfn[maxn],dfs_clock,sccno[maxn];bool insta[maxn];stack <int> sta;struct Scc{    int cnt;    vector <int> a[maxn];    void clear()    {        cnt=0;        for(int i=1;i<=n+1;i++) a[i].clear();    }    void add(int tmp) { a[cnt].push_back(tmp);sccno[tmp]=cnt; }}scc;int dfs(int u){    int lowu=dfn[u]=++dfs_clock;    sta.push(u);insta[u]=true;    for(int i=head[u];~i;i=edge[i].next)    {        int v=edge[i].to;        if(!dfn[v])        {            int lowv=dfs(v);            lowu=min(lowu,lowv);        }        else if(insta[v]) lowu=min(lowu,dfn[v]);    }    if(lowu>=dfn[u])    {        scc.cnt++;        int tmp;        do        {            tmp=sta.top();sta.pop();insta[tmp]=false;            scc.add(tmp);        }while(tmp^u);    }    return lowu;}void tarjan(){    memset(dfn,0,sizeof(dfn));    memset(sccno,0,sizeof(sccno));    dfs_clock=0;scc.clear();    for(int i=1;i<=n;i++)        if(!dfn[i]) dfs(i);}int out[maxn],in[maxn];void work(){    tarjan();    memset(out,0,sizeof(out));    memset(in,0,sizeof(in));    for(int i=1;i<=n;i++)        for(int j=head[i];~j;j=edge[j].next)            if(sccno[i]^sccno[edge[j].to]) out[sccno[i]]++,in[sccno[edge[j].to]]++;    int tot_in=0,tot_out=0;    for(int i=1;i<=scc.cnt;i++)    {        if(!in[i]) tot_in++;        if(!out[i]) tot_out++;    }    if(scc.cnt^1) printf("%d\n%d\n",tot_in,max(tot_in,tot_out));    else printf("1\n0\n");}int main(){    #ifdef Local    freopen("net.in","r",stdin);    freopen("net.out","w",stdout);    #endif    init();    work();    return 0;}
0 0