Network of Schools
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 16343   Accepted: 6484

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
【分析】第一问就是问最少有多少个连通块,可以用Tarjan缩点,然后看入度为0的连通分量有多少个就行了。
第二问就是问最少添加几条边使得这个无向图变成一个连通块,缩点后,令father为树根数,令son为子叶数,则答案为max(father,son),若缩点后就一个点,则输出1 0。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 0x3f3f3f3f
#define mod 10000
typedef long long ll;
using namespace std;
const int N=105;
const int M=10005;
int s,t,n,m,cnt,tim,top,cut;
int head[N],dfn[N],low[N],stack1[N];
int num[N],in[N],out[N],vis[N];
bool flag=false;
struct man
{
    int to,nxt;
}edg[M];
void addedg(int u,int v)
{
    edg[cnt].to=v;edg[cnt].nxt=head[u];head[u]=cnt++;
}
void init()
{
    cnt=0;tim=1;top=cut=0;
    memset(head,-1,sizeof head);
    memset(dfn,0,sizeof dfn);
    memset(low,0,sizeof low);
    memset(stack1,0,sizeof stack1);
    memset(num,0,sizeof num);
    memset(in,0,sizeof in);
    memset(out,0,sizeof out);
    memset(vis,0,sizeof vis);
    memset(edg,0,sizeof edg);
}
void dfs(int u) {
    int v;
    low[u] = dfn[u] = ++tim;
    stack1[top++] = u;
    vis[u] = 1;
    for(int e = head[u]; e != -1; e = edg[e].nxt)
    {
        v = edg[e].to;
        if(!dfn[v])
        {
            dfs(v);
            low[u] = min(low[u], low[v]);
        }
        else if(vis[v])
        {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if(low[u] == dfn[u])
    {
        cut++;//printf("!!!%d\n",u);
        do
        {
            v = stack1[--top];
            num[v] = cut;
            vis[v] = 0;
        }while(u != v);
    }
}
int main() {
    while(~scanf("%d",&n)){
    init();
    for(int i=1;i<=n;i++){
        while(1){
            scanf("%d",&m);
            if(!m)break;
            addedg(i,m);
        }
    }
    for(int i=1;i<=n;i++)if(!dfn[i])dfs(i);
    for(int i=1;i<=n;i++){
        for(int j=head[i];j!=-1;j=edg[j].nxt){
            int v=edg[j].to;//printf("%d %d %d %d\n",i,num[i],v,num[v]);
            if(num[i]!=num[v])out[num[i]]++,in[num[v]]++;
        }
    }
    int father=0,son=0;
    for(int i=1;i<=cut;i++){
        if(in[i]==0)father++;
        if(out[i]==0)son++;
    }
    if(cut==1)printf("1\n0\n");
    else printf("%d\n%d\n",father,max(father,son));
    }
    return 0;
}

POJ1236 Network of Schools (强连通)(缩点)的更多相关文章

  1. POJ1236 Network of Schools —— 强连通分量 + 缩点 + 入出度

    题目链接:http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Tot ...

  2. poj-1236.network of schools(强连通分量 + 图的入度出度)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27121   Accepted: 10 ...

  3. POJ1236 Network of Schools (强连通分量,注意边界)

    A number of schools are connected to a computer network. Agreements have been developed among those ...

  4. [poj1236]Network of Schools(targin缩点SCC)

    题意:有N个学校,从每个学校都能从一个单向网络到另外一个学校.1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件.2:至少需要添加几条边,使任意向一个学校发放软件后,经过若干次 ...

  5. poj1236 Network of Schools(SCC缩点+结论推导)

    第一问简单不讲. 第二问简化后问题是给一张DAG求最少添加几条边使得DAG变成一个SCC.首先所有中间点(有入度有出度)肯定直接顺着走到无出度点,所以肯定是无出度点连向无入度点. 把无入度点作为点集S ...

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

    P2746 [USACO5.3]校园网Network of Schools// POJ1236: Network of Schools 题目描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学 ...

  7. POJ 1236 Network Of Schools (强连通分量缩点求出度为0的和入度为0的分量个数)

    Network of Schools A number of schools are connected to a computer network. Agreements have been dev ...

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

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13801   Accepted: 55 ...

  9. POJ 1236 Network of Schools Tarjan缩点

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22729   Accepted: 89 ...

随机推荐

  1. bzoj 2820 YY的GCD 莫比乌斯反演

    题目大意: 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对 这里就抄一下别人的推断过程了 后面这个g(x) 算的方法就是在线性 ...

  2. URAL 1519 基础插头DP

    题目大意: 给定一个图,一部分点'*'作为障碍物,求经过所有非障碍点的汉密尔顿回路有多少条 基础的插头DP题目,对于陈丹琦的论文来说我觉得http://blog.sina.com.cn/s/blog_ ...

  3. 戴文的Linux内核专题:03驱动程序

    转自Linux中国 驱动程序是使内核能够沟通和操作硬件或协议(规则和标准)的小程序.没有驱动程序,内核不知道如何与硬件沟通或者处理协议(内核实际上先发送指令给BIOS,然后BIOS传给硬件). Lin ...

  4. Android listview 制作表格样式+由下往上动画弹出效果实现

    效果是这样的:点击按下弹出表格的按钮,会由下往上弹出右边的列表,按下返回按钮就由上往下退出界面. 布局文件: activity_main.xml <RelativeLayout xmlns:an ...

  5. 发布Restful服务时出现IIS 指定了身份验证方案错误时的解决方案(IIS specified authentication schemes)

    发布RESTful服务,当访问.svc文件时出现如下错误时: IIS 指定了身份验证方案“IntegratedWindowsAuthentication, Anonymous”,但绑定仅支持一种身份验 ...

  6. Hadoop c++开发

    假设你有上百G的数据,你要统计出这些数据中,含有某些你感兴趣的内容的数据的有多少条,你会怎么做?在硬件条件允许的情况下,用hadoop并行计算是一个不错的选择. 为了使本文得以清晰地说明,我们不妨假设 ...

  7. mysql 为某一数据库下所有表中添加相同字段

    BEGIN  DECLARE s_tablename VARCHAR(100);  /*显示表的数据库中的所有表 SELECT table_name FROM information_schema.t ...

  8. Iterator之java.util.ConcurrentModificationException

    在运行以下代码时,会报java.util.ConcurrentModificationException异常, public class Demo { public static void main( ...

  9. AFNetworking vs ASIHTTPRequest vs MKNetworkKit

    AFNetworking vs ASIHTTPRequest vs MKNetworkKit

  10. Ch2.Making Reconmmendation in PCI

    做<Programing Collective Intelligence>中chapter 2.Making Recommendation的实例,有3个问题花了好长时间: 1. 遇到报错& ...