POJ 1236 Network of Schools(tarjan)
Description
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
Output
Sample Input
5
2 4 3 0
4 5 0
0
0
1 0
Sample Output
1
2 题目大意:有n个学校,每个学校能够单向到达某些学校,1.求出最少要给几个学校发软件才能使每个学校都有软件用 2.求出最少需要连接多少条边才能使任意学校出发都能到达其他学校
思路:第一个问的话,我们先求出该图中的所有强连通分量,将每个强连通分量看成一个点,求出入度为0的强连通分量的个数num1即可;第二问则还需要求出强连通分量中出度为0的点的个数num2,最后取max(num1,num2)
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stack> using namespace std;
const int maxn = ;
int low[maxn],dfn[maxn];
int vis[maxn],f[maxn],num[maxn];
int in[maxn],out[maxn];
vector<int>edge[maxn];
int n,cnt,color;//cnt为low数组的节点数
stack<int>Q;
void tarjan(int u)
{
low[u] = dfn[u] = ++cnt;
vis[u] = ;
Q.push(u);
for(int i=;i<edge[u].size();i++){
int t = edge[u][i];
if(!dfn[t]){
tarjan(t);
low[u] =min(low[u],low[t]);
}else if(vis[t])
low[u] = min(low[u],dfn[t]);
}
if(dfn[u]==low[u]){
vis[u] = ;
f[u] = ++color;//染色缩点
while((Q.top()!=u) && Q.size()){
f[Q.top()] = color;
vis[Q.top()] = ;
Q.pop();
}
Q.pop();
}
}
void init()
{
memset(low,,sizeof(low));
memset(dfn,,sizeof(dfn));
memset(vis,,sizeof(vis));
memset(num,,sizeof(num));
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(f,,sizeof(f));
cnt = ;color=;
}
int main()
{
while(scanf("%d",&n)!=EOF){
init();
for(int i=;i<=n;i++)edge[i].clear();
for(int x,i=;i<=n;i++){
while(scanf("%d",&x)&&x)
edge[i].push_back(x);
}
for(int i=;i<=n;i++)
if(!dfn[i])
tarjan(i);
for(int i=;i<=n;i++){
for(int j=;j<edge[i].size();j++){
int v = edge[i][j];
if(f[i]!=f[v]){//若不属于同一个强连通分量
in[f[v]]++;
out[f[i]]++;
}
}
}
int ans1=,ans2=;
for(int i=;i<=color;i++){
if(in[i]==)ans1++;
if(out[i]==)ans2++;
}
if(color==)printf("1\n0\n");
else printf("%d\n%d\n",ans1,max(ans1,ans2));
}
return ;
}
POJ 1236 Network of Schools(tarjan)的更多相关文章
- POJ 1236 Network of Schools (Tarjan)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22745 Accepted: 89 ...
- POJ 1236 Network of Schools(tarjan)题解
题意:一个有向图.第一问:最少给几个点信息能让所有点都收到信息.第二问:最少加几个边能实现在任意点放信息就能传遍所有点 思路:把所有强连通分量缩成一点,然后判断各个点的入度和出度 tarjan算法:问 ...
- POJ 1236 Network of Schools(Tarjan缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16806 Accepted: 66 ...
- poj 1236 Network of Schools(tarjan+缩点)
Network of Schools Description A number of schools are connected to a computer network. Agreements h ...
- POJ 1236 Network of Schools(tarjan求强连通分量+思维)
题目链接:http://poj.org/problem?id=1236 题目大意: 给你一个网络(有向图),有两个任务: ①求出至少同时需要几份副本可以使得整个网络都获得副本 ②至少添加多少信息表(有 ...
- POJ 1236 Network of Schools(tarjan算法 + LCA)
这个题目网上有很多答案,代码也很像,不排除我的.大家的思路应该都是taijan求出割边,然后找两个点的LCA(最近公共祖先),这两个点和LCA以及其他点构成了一个环,我们判断这个环上的割边有几条,我们 ...
- POJ 1236 Network of Schools (tarjan算法+缩点)
思路:使用tarjan求强连通分量并进行缩点,判断所有入度为0的点,这个点就是必须要给予文件的点,分别计算出度,入度为零的点的个数,取二者的最大值就是把这个图变成强连通需要加的边数. 一个取值需要讨论 ...
- poj 1236 Network of Schools(连通图)
题目链接:http://poj.org/problem?id=1236 题目大意:有一些学校,学校之间可以进行收发邮件,给出学校的相互关系,问:1.至少 要向这些学校发送多少份才能使所有的学校都能获得 ...
- POJ 1236.Network of Schools (强连通)
首先要强连通缩点,统计新的图的各点的出度和入度. 第一问直接输出入度为0的点的个数 第二问是要是新的图变成一个强连通图,那么每一个点至少要有一条出边和一条入边,输出出度和入度为0的点数大的那一个 注意 ...
随机推荐
- SSM+Maven+IDEA增删改查
开发工具 IntelliJ IDEA Apache-tomcat-9.0 JDK 1.8 MySQL 8.0.11 Maven 3.5.4 IDEA+Maven搭建项目骨架 1. 新建Maven项目: ...
- Android手机控制电脑撸出HelloWorld
最近在开发一个远程办公的软件. 昨天在手机调通,并且成功通过手机打开电脑上的Eclipse撸出来一个HelloWorld. 也许不久的将来, 下班后,拿着手机在家写代码了.工作时间直接变成24/24 ...
- java reference(转)
http://blog.163.com/xubin_3@126/blog/static/112987702200962211145825/ 在Java中的引用类型,是指除了基本的变量类型之外的所有类型 ...
- pycharm 2017 序列号失效问题解决(2016-2017版本都有效)
pycharm 序列号失效问题解决 this license BIG3CLIK6F has been cancelled 具体如下: 对,没错,这个激活码本来可以使用到2018年的,但是,忽然间 ...
- Nginx教程(三) Nginx日志管理 (转)
Nginx教程(三) Nginx日志管理 1 日志管理 1.1 Nginx日志描述 通过访问日志,你可以得到用户地域来源.跳转来源.使用终端.某个URL访问量等相关信息:通过错误日志,你可以得到系统某 ...
- NSArray 查询数组中的对象
1.NSString 对象 NSArray *array =@["123", @"234" , @"345"]; NSPredicate ...
- Directx11教程(45) alpha blend(2)
原文:Directx11教程(45) alpha blend(2) 在myTutorialD3D11_40中,我们在场景中再添加一个box,并把box放在水里,实现半透明的效果.如下图所示: ...
- JavaScript--tab栏切换效果
tab栏切换效果: <!DOCTYPE html> <html> <head lang="en"> <meta charset=" ...
- hdu 1728 逃离迷宫 BFS加优先队列 DFS()
http://acm.hdu.edu.cn/showproblem.php?pid=1728 题意就是能否在规定的转弯次数内从起点走到终点.刚走时那步方向不算. 只会bfs(),但想到这题需要记录转弯 ...
- s3c6410时钟初始化
今天自己写bootloader做时钟初始化时遇到的问题,特记录下来.为了方便理解,我大部分都有截图, 在此我先说明下,图均来自数据手冊.也希望看了本篇文章的同志多多參看数据手冊才干理解的更加透 ...