A - Network of Schools - poj 1236(求连通分量)

题意:学校有一些单向网络,现在需要传一些文件,1,求最少需要向几个学校分发文件才能让每个学校都收到,2,需要添加几条网络才能在任意一个学校分发都可以传遍所有学校。

分析:首先应该求出来连通分量,进行缩点,然后求每个分量的入度和出度,入度等于0的很明显都需要分发一个文件,至于需要添加几条边可以成为一个强连通,就是出度和入度最大的那个,因为需要把出度和入度相连。

****************************************************************

#include<stdio.h>
#include<string.h>
#include<stack>
#include<algorithm>
using namespace std;

const int MAXN = 105;

///邻接表变量
struct Edge{int v, next;}e[MAXN*MAXN];
int Head[MAXN], cnt;

void AddEdge(int u, int v)
{
    e[cnt].v = v;
    e[cnt].next = Head[u];
    Head[u] = cnt++;
}

///tarjan算法变量
int sta[MAXN], inStack[MAXN], top;
int dfn[MAXN], low[MAXN], index;
int belong[MAXN], bnt;

void tarjan(int k)
{
    int j;

dfn[k] = low[k] = ++index;
    inStack[k] = true;
    sta[++top] = k;

for(j=Head[k]; j!=-1; j=e[j].next)
    {
        int v = e[j].v;
        if( !dfn[v] )
        {
            tarjan(v);
            low[k] = min(low[k], low[v]);
        }
        else if(inStack[v])
            low[k] = min(low[k], dfn[v]);
    }

if(dfn[k] == low[k])
    {
        ++bnt;
        do
        {
            j = sta[top--];
            inStack[j] = false;
            belong[j] = bnt;
        }
        while(k != j);
    }
}
void InIt(int N)
{
    cnt = bnt = index = 0;
    top = 0;

for(int i=1; i<=N; i++)
    {
        Head[i] = -1;
        dfn[i] = false;
    }
}

int main()
{
    int N;

while(scanf("%d", &N) != EOF)
    {
        int i, j, u, v;

InIt(N);

for(i=1; i<=N; i++)
        {
            while(scanf("%d", &v), v)
                AddEdge(i, v);
        }

for(i=1; i<=N; i++)if(!dfn[i])
            tarjan(i);

int r[MAXN]={0}, c[MAXN]={0}, rn=0, cn=0;

for(i=1; i<=N; i++)
        for(j=Head[i]; j!=-1; j=e[j].next)
        {
            u = belong[i], v = belong[e[j].v];
            if(u != v)
            {
                c[u]++;
                r[v]++;
            }
        }

for(i=1; i<=bnt; i++)
        {
            if(r[i] == 0)rn++;
            if(c[i] == 0)cn++;
        }

if(bnt == 1)
            printf("1\n0\n");
        else
            printf("%d\n%d\n", rn, max(rn, cn));
    }

return 0;

}

时间: 2024-08-20 16:44:21

A - Network of Schools - poj 1236(求连通分量)的相关文章

Network of Schools POJ 1236

首先应该求出来连通分量,进行缩点,然后求每个分量的入度和出度,入度等于0的很明显都需要分发一个文件,至于需要添加几条边可以成为一个强连通,就是出度和入度最大的那个,因为需要把出度和入度相连.#include <cstdio> #include <iostream> #include <cstring> #include <cstring> #include <algorithm> #include <stack> #include &

poj 2524 求连通分量(并查集模板题)

求连通分量 Sample Input 10 91 21 31 41 51 61 71 81 91 1010 42 34 54 85 80 0Sample Output Case 1: 1Case 2: 7 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6 # include

POJ 1236 Network of Schools(tarjan求强连通分量+思维)

题目链接:http://poj.org/problem?id=1236 题目大意: 给你一个网络(有向图),有两个任务: ①求出至少同时需要几份副本可以使得整个网络都获得副本 ②至少添加多少信息表(有向边)使得副本传到任一点,都可以使得整个网络都获得副本 解题思路: 即给定一个有向图,求: ①至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点 ②至少要加多少条边,才能使得从任何一个顶点出发,都能到达全部顶点缩点后,分别求出出度入度为0的点数为sum1,sum2,问题①的答案就为sum2;

poj1236 Network of Schools ,有向图求强连通分量(Tarjan算法),缩点

题目链接: 点击打开链接 题意: 给定一个有向图,求: 1) 至少要选几个顶点.才干做到从这些顶点出发,能够到达所有顶点 2) 至少要加多少条边.才干使得从不论什么一个顶点出发,都能到达所有顶点 顶点数<= 100 求完强连通分量后,缩点,计算每一个点的入度,出度. 第一问的答案就是入度为零的点的个数, 第二问就是max(n,m) // 入度为零的个数为n, 出度为零的个数为m. //kuangbin巨巨分析非常棒! #include<cstdio> #include<cstrin

How Many Tables(POJ 1213 求连通分量)

How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 20974    Accepted Submission(s): 10382 Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinn

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

[tarjan] poj 1236 Network of Schools

题目链接: http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11433   Accepted: 4551 Description A number of schools are connected to a computer network. Agreements have been developed among thos

[POJ 1236][IOI 1996]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 if B is in the

poj 1236 Network of Schools 【强连通图】

题目:poj 1236 Network of Schools 类似题目hdoj 2767 3836 /*******以下kuang大神的解释,写的很好就不解释了*************************/ 强连通分量缩点求入度为0的个数和出度为0的分量个数 题目大意:N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输,问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件.2,至少需要添加几条传输线路(边),使任