J - Network of Schools
来源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的更多相关文章
- POJ 1236 Network of Schools(Tarjan缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16806 Accepted: 66 ...
- Network of Schools --POJ1236 Tarjan
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Description A number of schools are conne ...
- [强连通分量] POJ 1236 Network of Schools
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16803 Accepted: 66 ...
- POJ1236 - Network of Schools tarjan
Network of Schools Time Limit: 1000MS Memory Limi ...
- POJ1236 Network of Schools (强连通)(缩点)
Network of Schools Time Limit: 1000MS ...
- poj 1236 Network of Schools(连通图入度,出度为0)
http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- poj 1236 Network of Schools(又是强连通分量+缩点)
http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- poj 1236 Network of Schools【强连通求孤立强连通分支个数&&最少加多少条边使其成为强连通图】
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13800 Accepted: 55 ...
- [tarjan] poj 1236 Network of Schools
主题链接: http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K To ...
随机推荐
- 避免 Deepin 15.4 系统 界面卡顿、假死等现象:隐藏自带的“任务栏”,安装轻量级的“任务栏tint2”
使用 Deepin 过程中,发现当点击“任务栏”上面的按钮,尤其是右键单击,选择菜单时,界面很容易卡顿,再也动弹不了. 好吧,,,就不使用自带的“任务栏”了,换成一个轻量级的“任务栏tint2”, 1 ...
- java后台服务器启动脚本
最近由于经常在项目上线或者调试中启动服务,由于要设置环境变量这些,所以为了方便写了个启动脚本,希望能够帮助大家,也算是给自己做个小笔记: example_project_start.sh: # /bi ...
- ASM ClassReader failed to parse class file解决方法
1. 环境信息: Spring 3.2.2, JDK 1.8, Hibernate 3.5.5 2. 运行简单的程序,出现以下错误信息: [2018-05-25 02:36:58,671] Ar ...
- Docker入门实践
Docker是一门很成熟的容器技术,类似虚拟机技术主要用做环境的隔离,方便环境的复制镜像,虚拟机是基于操作系统这一层的,而Docker更加的轻量级,像是“应用”层级的.比如我需要一个MySQL环境.一 ...
- Huginn及环境搭建
博客搬迁至https://blog.wangjiegulu.com RSS订阅:https://blog.wangjiegulu.com/feed.xml Huginn 及环境搭建 什么是 Hugin ...
- css 定位(fixed > absolute > relative)与层级zIndex 的权限认知
原则1: fixed > absolute > relative原则2: zIndex 越高越牛逼,不管你是谁无视身份.原则3: 青出于蓝而胜于蓝,儿子永远比父亲强原则4: 平台很重要. ...
- 关于python中pika模块的问题
工作中经常用到rabbitmq,而用的语言主要是python,所以也就经常会用到python中的pika模块,但是这个模块的使用,也给我带了很多问题,这里整理一下关于这个模块我在使用过程的改变历程已经 ...
- spring事务的传播机制新解
以下是事物的传播机制: @Transactional(propagation=Propagation.REQUIRED)如果有事务, 那么加入事务, 没有的话新建一个(默认情况下)@Transacti ...
- s和t的特殊权限
ls -l 通常会显示r w x权限,分别对应:读,写,执行权限. 但是有时我么会看到,s或t这类权限标识. eg: #include <unistd.h> #include <st ...
- Selenium IDE 基本概念
要学会Selenium不难,难的是首先你懂不懂测试.没有测试的基础知识,没有对测试理论的实践和认知,没有对测试领域的情感和钻研精神,学会了Selenium这个工具对事情也没有实际帮助. 我是一个技术思 ...