POJ1236(KB9-A 强连通分量)
Network of Schools
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 19326 | Accepted: 7598 |
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
Source
//2017-08-20
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector> using namespace std; const int N = ;
int n, in_degree[N], out_degree[N];
bool vis[N];
vector<int> G[N];
vector<int> rG[N];
vector<int> vs;
int cmp[N]; void add_edge(int u, int v){
G[u].push_back(v);
rG[v].push_back(u);
} //input: u 顶点
//output: vs 后序遍历顺序的顶点列表
void dfs(int u){
vis[u] = true;
for(int i = ; i < G[u].size(); i++){
int v = G[u][i];
if(!vis[v])
dfs(v);
}
vs.push_back(u); } //input: u 顶点编号; k 拓扑序号
//output: cmp[] 强连通分量拓扑序
void rdfs(int u, int k){
vis[u] = true;
cmp[u] = k;
for(int i = ; i < rG[u].size(); i++){
int v = rG[u][i];
if(!vis[v])
rdfs(v, k); } } //Strongly Connected Component 强连通分量
//input: n 顶点个数
//output: k 强连通分量数;
int scc(){
memset(vis, , sizeof(vis));
vs.clear();
for(int u = ; u <= n; u++)
if(!vis[u]){
dfs(u);
}
int k = ;
memset(vis, , sizeof(vis));
for(int i = vs.size()-; i >= ; i--)
if(!vis[vs[i]])
rdfs(vs[i], k++);
return k;
} void solve(){
int k = scc();
int ans = ;
memset(in_degree, , sizeof(in_degree));
memset(out_degree, , sizeof(out_degree));
for(int u = ; u <= n; u++){
memset(vis, , sizeof(vis));
for(int i = ; i < G[u].size(); i++){
int v = G[u][i];
if(vis[v])continue;
vis[v] = ;
if(cmp[u] != cmp[v]){
out_degree[cmp[u]]++;
in_degree[cmp[v]]++;
}
}
}
int a = , b = ;
for(int i = ; i < k; i++){
if(in_degree[i] == )a++;
if(out_degree[i] == )b++;
}
ans = max(a, b);
if(k == )ans = ;
printf("%d\n%d\n", a, ans);
} int main()
{
while(scanf("%d", &n)!=EOF){
for(int u = ; u <= n; u++){
G[u].clear();
rG[u].clear();
}
int v;
for(int u =; u <= n; u++){
while(scanf("%d", &v)== && v){
add_edge(u, v);
}
}
solve();
} return ;
}
POJ1236(KB9-A 强连通分量)的更多相关文章
- poj-1236.network of schools(强连通分量 + 图的入度出度)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 27121 Accepted: 10 ...
- [POJ1236]Network of Schools(并查集+floyd,伪强连通分量)
题目链接:http://poj.org/problem?id=1236 这题本来是个强连通分量板子题的,然而弱很久不写tarjan所以生疏了一下,又看这数据范围觉得缩点这个事情可以用点到点之间的距离来 ...
- poj1236 Network of Schools【强连通分量(tarjan)缩点】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4316263.html ---by 墨染之樱花 [题目链接]http://poj.org/pr ...
- poj-1236(强连通分量)
题意:给你n个点,每个点可能有指向其他点的单向边,代表这个点可以把软件传给他指向的点,然后解决两个问题, 1.问你最少需要给几个点,才能使所有点都能拿到软件: 2.问你还需要增加几条单向边,才能使任意 ...
- poj1236 Network of Schools ,有向图求强连通分量(Tarjan算法),缩点
题目链接: 点击打开链接 题意: 给定一个有向图,求: 1) 至少要选几个顶点.才干做到从这些顶点出发,能够到达所有顶点 2) 至少要加多少条边.才干使得从不论什么一个顶点出发,都能到达所有顶点 ...
- POJ1236 Network of Schools —— 强连通分量 + 缩点 + 入出度
题目链接:http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Tot ...
- Proving Equivalences UVALive - 4287(强连通分量 水题)
就是统计入度为0 的点 和 出度为0 的点 输出 大的那一个,, 若图中只有一个强连通分量 则输出0即可 和https://www.cnblogs.com/WTSRUVF/p/9301096.htm ...
- HDU5934 强连通分量
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5934 根据距离关系建边 对于强连通分量来说,只需引爆话费最小的炸弹即可引爆整个强连通分量 将所有的强连通分 ...
- POJ1236Network of Schools[强连通分量|缩点]
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16571 Accepted: 65 ...
随机推荐
- Python MySQL - 进行数据查询
#coding=utf-8 import mysql.connector import importlib import sys # reload(sys) # sys.setdefaultencod ...
- Nginx 负载均衡和反向代理实践
nginx 以哪个配置文件启动 Nginx 负载均衡和反向代理实践 环境介绍 192.168.1.50 在这台主机上配置Nginx 的反向代理,负载均衡,和web1,web1使用的81号端口 1 ...
- 简单理解jQuery中$.getJSON、$.get、$.post、$.ajax用法
在WEB开发中异步请求方式普遍使用,ajax技术减少程序员的工作量,也提升用户交互体验.AJAX的四种异步请求方式都能实现基本需求,闲话不多说,直接切入正题. 1.$.getJSON $.getJSO ...
- XAMPP中MySQL无法启动解决办法
如图 问题出在mysql的路径上,其实报错已经讲得听清楚了 预期应该是这样 结果却是这样 所以解决办法当然就是修改这个路径,出现这个报错原因大多因为之前电脑装过mysql,所以电脑默认启动是原来的my ...
- C++(初学讲解):判断倍数
问题描述输入一个整数,如果是5的倍数,那么输出倍数的值,否则输出NO. 输入描述一个整数. 输出描述输出倍数的值或者NO. 输入示例15 输出示例3 #include <iostream> ...
- odoo开发笔记--自定义server action页面跳转注意
场景描述: 在添加自定义服务器动作 “复制全部”后发现直接创建了新的记录,并且直接进入到form保存完的状态. 如何解决: if yourself_obj_copy: return { 'type': ...
- django views视图函数返回值 return redirect httpresponse总结
django views视图函数返回值 return redirect render httpresponse总结
- (转)centos 7 Tomcat 8.5 的安装及生产环境的搭建调优
原文:https://www.cnblogs.com/linhankbl/articles/9149804.html#top JVM菜鸟进阶高手之路七(tomcat调优以及tomcat7.8性能对比) ...
- php获取全选checkbox多个值
<form name="myform" action="index2.php" method="post"> ...
- psql工具使用(二)
所有psql命令都以 \ 开头 一.使用psql -l查看有哪些数据库: -bash-4.2$ psql -l List of databases Name | Owner | Encodin ...