来源poj1236

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

找入度为0的强通量,和出度为0 的强通量,特例,如果只有一个强通量的时候为1 0;

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
const ll mod=1e9+100;
const double eps=1e-8;
using namespace std;
const double pi=acos(-1.0);
const int inf=0xfffffff;
const int N=105;
struct Edge {
int v,next;
}edge[N*N];
int dfn[N],low[N];
int stack[N],node[N],visit[N],cnt,tot,index;
int belong[N],bcnt;
void add_edge(int x,int y)
{
edge[cnt].next=node[x];
edge[cnt].v = y;
node[x]=cnt++;
return ;
}
void tarjan(int x)//代表第几个点在处理。递归的是点。
{
dfn[x]=low[x]=++tot;// 新进点的初始化。
stack[++index]=x;//进站
visit[x]=1;//表示在栈里
for(int i=node[x];i!=-1;i=edge[i].next)
{
if(!dfn[edge[i].v]) {//如果没访问过
tarjan(edge[i].v);//往下进行延伸,开始递归
low[x]=min(low[x],low[edge[i].v]);//递归出来,比较谁是谁的儿子/父亲,就是树的对应关系,涉及到强连通分量子树最小根的事情。
}
else if(visit[edge[i].v ]){ //如果访问过,并且还在栈里。
low[x]=min(low[x],dfn[edge[i].v]);//比较谁是谁的儿子/父亲。就是链接对应关系
}
}
if(low[x]==dfn[x]) //发现是整个强连通分量子树里的最小根。
{
bcnt++;
do{
belong[stack[index]]=bcnt;
visit[stack[index]]=0;
index--; }while(x!=stack[index+1]);//出栈,并且输出。
}
return ;
}
int in[N],out[N];
void solve(int n)
{
tot=index=bcnt=0;
mm(dfn,0);
mm(low,0);
mm(in,0);
mm(belong,0);
mm(out,0);
mm(visit,0);
rep(i,1,n+1)
if(!dfn[i])
tarjan(i);
int ans1=0,ans2=0;
rep(i,1,n+1)
{
for(int j=node[i];j!=-1;j=edge[j].next)
{
if(belong[i]!=belong[edge[j].v])
{
in[belong[edge[j].v]]++;
out[belong[i]]++;
}
}
}
rep(i,1,bcnt+1)
{
if(in[i]==0) ans1++;
if(out[i]==0) ans2++;
}
if(bcnt==1)
pf("1\n0\n");
else
pf("%d\n%d\n",ans1,max(ans2,ans1));
}
int main()
{
int n;
while(~sf("%d",&n))
{
rep(i,1,n+1)
node[i]=-1;
cnt=1;
rep(i,1,n+1)
{
int x;
while(sf("%d",&x)&&x)
add_edge(i,x);
}
solve(n);
}
return 0;
}

J - Network of Schools的更多相关文章

  1. POJ 1236 Network of Schools(Tarjan缩点)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16806   Accepted: 66 ...

  2. Network of Schools --POJ1236 Tarjan

    Network of Schools Time Limit: 1000MS Memory Limit: 10000K Description A number of schools are conne ...

  3. [强连通分量] POJ 1236 Network of Schools

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16803   Accepted: 66 ...

  4. POJ1236 - Network of Schools tarjan

                                                     Network of Schools Time Limit: 1000MS   Memory Limi ...

  5. POJ1236 Network of Schools (强连通)(缩点)

                                                                Network of Schools Time Limit: 1000MS   ...

  6. poj 1236 Network of Schools(连通图入度,出度为0)

    http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  7. poj 1236 Network of Schools(又是强连通分量+缩点)

    http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  8. poj 1236 Network of Schools【强连通求孤立强连通分支个数&&最少加多少条边使其成为强连通图】

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

  9. [tarjan] poj 1236 Network of Schools

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

随机推荐

  1. linux bash timeout

    http://www.digitalinternals.com/unix/unix-linux-run-command-with-timeout/500/ There are two ways to ...

  2. centos修改主机名 root@后面的名字

    阿里云买的新的ESC,名字都是一串字符,不利于平时使用.我们可以重命名主机来标记. centos6 [root@centos6 ~]$ hostname # 查看当前的hostnmae centos6 ...

  3. 获得最近一天的提交,并使用winscp上传到服务器

    @echo off D:\dev\Git\bin\git.exe pull origin master D:\dev\Git\bin\git.exe add -A D:\dev\Git\bin\git ...

  4. [Python设计模式] 第16章 上班,干活,下班,加班——状态模式

    github地址:https://github.com/cheesezh/python_design_patterns 题目 用代码模拟一天的工作状态,上午状态好,中午想睡觉,下午渐恢复,加班苦煎熬. ...

  5. PL/SQL学习笔记程序单元

    一:程序单元组成 一个PL/SQL程序单元主要包括三部分: 声明与定义部分:声明变量.常量.类型等:定义过程.函数等: 执行部分:执行PL/SQL语句:调用过程.参数:处理游标等: 异常处理部分:处理 ...

  6. 字符串截取mysql

    2017-05-26 --SUBSTR(addtime, 1, 10) --substr(string string,num start,num length); --string为字符串 --sta ...

  7. 摘:PC客户端 XP兼容性调查

    现象1:XP SP2下,客户端安装失败(或者启动失败) 解决方案:需要安装以下3个补丁(已经集成到安装包中) ==系统Hotfix(仅仅XP SP2需要安装): http://support.micr ...

  8. mysql 动态增加列,查找表中有多少列,具体什么列。 通过JSON生成mysql表 支持子JSON

    好消息, 程序员专用早餐机.和掌柜说 ideaam,可以节省20元. 点击链接  或復·制这段描述¥k3MbbVKccMU¥后到淘♂寳♀ 或者 淘宝扫码 支持下同行哈 ---------------- ...

  9. 物联网架构成长之路(25)-Docker构建项目用到的镜像1

    0. 前言 现在项目处于初级阶段,按照规划,先构建几个以后可能会用到的Image,并上传到阿里云的Docker仓库.以后博客中用到的Image,大部分都会用到这几个基础的Image,构建一个简单的物联 ...

  10. go mod代理和小技巧

    代理 go mod的代理比较出名的有微软的athens,可以基于它搭建一个私有的代理,管理内部的私有代码,而且微软提供了一个公共的代理,我们可以直接使用 Linux export GOPROXY=&q ...