usaco Network of Schools

强连通分量的求解,虽说第一眼一看数据量就知道能用Floyd写,但是谁让我太渣呢,还是别偷懒了。熟悉下tarjan。

/*
ID: modengd1
PROG: schlnet
LANG: C++
*/
#include <iostream>
#include <stdio.h>
#include <vector>
#include <stack>
#include <memory.h>
using namespace std;
bool vis[101];
bool insatck[101];
vector<int> G[101];
int N;
int dfn[101];
int low[101];
stack<int> ST;
int index;
int colored[101];
int color;
int newG[101][101];
void tarjan(int u)
{
    vis[u]=true;
    low[u]=dfn[u]=++index;
    ST.push(u);
    insatck[u]=true;
    for(int i=0;i<G[u].size();i++)
    {
        if(!vis[G[u][i]])
        {
            tarjan(G[u][i]);
            low[u]=min(low[G[u][i]],low[u]);
        }
        else if(insatck[G[u][i]])
        {
            low[u]=min(low[u],dfn[G[u][i]]);
        }
    }
    if(low[u]==dfn[u])//同一个联通分量中的点染成一个颜色
    {
        color++;
        while(ST.top()!=u)
        {
            colored[ST.top()]=color;
            insatck[ST.top()]=false;
            ST.pop();
        }
        colored[ST.top()]=color;
        insatck[ST.top()]=false;
        ST.pop();
    }
}
void buildnew()
{
    memset(newG,false,sizeof(newG));
    for(int i=1;i<=N;i++)
    {
        for(int j=0;j<G[i].size();j++)
        {
            if(colored[i]!=colored[G[i][j]])
            newG[colored[i]][colored[G[i][j]]]=true;
        }
    }
    int cterO[101],cterI[101];
    memset(cterO,0,sizeof(cterO));
    memset(cterI,0,sizeof(cterI));
    for(int i=1;i<=color;i++)
    {
        for(int j=1;j<=color;j++)
        {
            if(newG[i][j])
            {
                cterO[i]++;
                cterI[j]++;
            }
        }
    }
    int ans0=0,ans1=0;
    for(int i=1;i<=color;i++)
    {
        if(cterI[i]==0)//入度为零的点的个数
            ans0++;
        if(cterO[i]==0)//出度为零的点的个数
            ans1++;
    }
    if(color==0)
    {
        cout<<1<<endl<<0<<endl;
    }
    else
    {
        cout<<ans0<<endl<<max(ans0,ans1)<<endl;
    }
}
int main()
{
    freopen("schlnet.in","r",stdin);
    freopen("schlnet.out","w",stdout);
    scanf("%d",&N);
    for(int i=1;i<=N;i++)
    {
        int v;
        while(scanf("%d",&v)&&v!=0)
        {
            G[i].push_back(v);
        }
    }
    index=0;
    color=0;
    memset(colored,0,sizeof(color));
    for(int i=1;i<=N;i++)
    {
        if(colored[i]==0)
            tarjan(i);
    }
    buildnew();
    return 0;
}

  

时间: 2024-10-13 00:48:09

usaco Network of Schools的相关文章

[IOI1996] USACO Section 5.3 Network of Schools(强连通分量)

nocow上的题解很好. http://www.nocow.cn/index.php/USACO/schlnet 如何求强连通分量呢?对于此题,可以直接先用floyd,然后再判断. ---------------------------------------------------------------------------------- #include<cstdio> #include<iostream> #include<algorithm> #includ

JDOJ-1833: Network of Schools 校园网

1833: Network of Schools 校园网 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 155  Solved: 63[Submit][Status][Web Board] Description 一些学校连入一个电脑网络.那些学校已订立了协议:每个学校都会给其它的一些学校分发软件(称作“接受学校”).注意如果 B 在 A 学校的分发列表中,那么 A 不必也在 B 学校的列表中. 你要写一个程序计算,根据协议,为了让网络中所有的学校

P2746 [USACO5.3]校园网Network of Schools// POJ1236: Network of Schools

P2746 [USACO5.3]校园网Network of Schools// POJ1236: Network of Schools 题目描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学校都会给其它的一些学校分发软件(称作“接受学校”).注意即使 B 在 A 学校的分发列表中, A 也不一定在 B 学校的列表中. 你要写一个程序计算,根据协议,为了让网络中所有的学校都用上新软件,必须接受新软件副本的最少学校数目(子任务 A).更进一步,我们想要确定通过给任意一个学校发送新软件,这个

POJ 1236——Network of Schools——————【加边形成强连通图】

Network of Schools Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1236 Description A number of schools are connected to a computer network. Agreements have been developed among those schools: e

Network of Schools(强连通分量+缩点) (问添加几个点最少点是所有点连接+添加最少边使图强连通)

Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13801   Accepted: 5505 Description A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a li

poj 1236 Network of Schools(tarjan+缩点)

Network of Schools 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

【连通图|强连通分量+缩点】POJ-1236 Network of Schools

Network of Schools Time Limit: 1000MS Memory Limit: 10000K 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 softwa

POJ 1236 Network of Schools(强连通分量)

POJ 1236 Network of Schools 链接:http://poj.org/problem?id=1236 题意:有一些学校连接到一个计算机网络.这些学校之间达成了一个协议:每个学校维护着一个学校列表,它向学校列表中的学校发布软件.注意,如果学校B 在学校A 的列表中,则A 不一定在B 的列表中. 任务A:计算为使得每个学校都能通过网络收到软件,你至少需要准备多少份软件拷贝. 任务B:考虑一个更长远的任务,想确保给任意一个学校发放一个新的软件拷贝,该软件拷贝能发布到网络中的每个学

POJ 1236 Network of Schools(强联通缩点)

Network of Schools 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