Network of Schools(强连通分量+缩点) (问添加几个点最少点是所有点连接+添加最少边使图强连通)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 13801 | Accepted: 5505 | 
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
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
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
题解:
- /题意:
 - //给你一个N个点的有向图,问1,最少连接几个点可以直接或间接 连接到所有点。
 - // 2,最少增加几条边使图强连通。
 - //思路SCC + 缩点后。统计出入度为0的SCC数sumin和出度为0的SCC数sumout。
 - //答案一就是sumin,答案二就是max(sumin, sumout)。注意只有一个SCC时,输出1 0。
 
代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<stack>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=;
vector<int>vec[MAXN];
stack<int>S;
int dfn[MAXN],low[MAXN],Instack[MAXN],in[MAXN],out[MAXN];
int dfs_blocks,scc,sccon[MAXN];
void targin(int u,int fa){
S.push(u);
Instack[u]=;
dfn[u]=low[u]=++dfs_blocks;
for(int i=;i<vec[u].size();i++){
int v=vec[u][i];
if(!dfn[v]){
targin(v,u);
low[u]=min(low[u],low[v]);
}
else if(Instack[v]){
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u]){
scc++;
while(){
int v=S.top();
S.pop();
Instack[v]=;
sccon[v]=scc;
if(u==v)break;
}
}
}
int main(){
int N;
while(~scanf("%d",&N)){
scc=;
for(int i=;i<MAXN;i++)vec[i].clear();
while(!S.empty())S.pop();
mem(in,);mem(out,);mem(Instack,);
mem(dfn,);mem(low,);mem(sccon,);
for(int i=;i<=N;i++){
int x;
while(scanf("%d",&x),x!=){
vec[i].push_back(x);
}
}
for(int i=;i<=N;i++){
if(!dfn[i]){
targin(i,-);
}
}
for(int i=;i<=N;i++){
for(int j=;j<vec[i].size();j++){
int v=vec[i][j];
if(sccon[i]!=sccon[v])in[sccon[v]]++,out[sccon[i]]++;
}
}
// printf("%d\n",scc);
int numin=,numout=;
if(scc==){
puts("1\n0");continue;
}
for(int i=;i<=scc;i++){
if(in[i]==)numin++;
if(out[i]==)numout++;
}
printf("%d\n%d\n",numin,max(numin,numout));
}
return ;
}
Network of Schools(强连通分量+缩点) (问添加几个点最少点是所有点连接+添加最少边使图强连通)的更多相关文章
- POJ 1236 Network Of Schools (强连通分量缩点求出度为0的和入度为0的分量个数)
		
Network of Schools A number of schools are connected to a computer network. Agreements have been dev ...
 - Network of Schools(强连通分量缩点(邻接表&矩阵))
		
Description A number of schools are connected to a computer network. Agreements have been developed ...
 - POJ1236 Network of Schools —— 强连通分量 + 缩点 + 入出度
		
题目链接:http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Tot ...
 - POJ 1236 Network of Schools (强连通分量缩点求度数)
		
题意: 求一个有向图中: (1)要选几个点才能把的点走遍 (2)要添加多少条边使得整个图强联通 分析: 对于问题1, 我们只要求出缩点后的图有多少个入度为0的scc就好, 因为有入度的scc可以从其他 ...
 - POJ1236Network of Schools[强连通分量|缩点]
		
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16571 Accepted: 65 ...
 - poj-1236.network of schools(强连通分量 + 图的入度出度)
		
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 27121 Accepted: 10 ...
 - [IOI1996] USACO Section 5.3 Network of Schools(强连通分量)
		
nocow上的题解很好. http://www.nocow.cn/index.php/USACO/schlnet 如何求强连通分量呢?对于此题,可以直接先用floyd,然后再判断. --------- ...
 - POJ1236 Network of Schools (强连通分量,注意边界)
		
A number of schools are connected to a computer network. Agreements have been developed among those ...
 - 【强连通分量缩点】poj 1236 Network of Schools
		
poj.org/problem?id=1236 [题意] 给定一个有向图,求: (1)至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点 (2)至少要加多少条边,才能使得从任何一个顶点出发,都 ...
 
随机推荐
- hibernate 及缓存机制
			
hibernate 是一个持久层的框架,经常访问物理数据库,为了降低应用程序访问物理数据库的频次, 从而提升性能, hibernate缓存机制分为: 一类是session 级缓存,二是sessionF ...
 - 基于HTTP协议下载文件的实现
			
最近在开发文件下载的程序,该程序是基于HTTP开发的. 首先是了解了文件传输到客户端的大概格式,然后分析该格式,实现写入文件的功能. 自己构造的HTTP包如下: GET /*********.rar ...
 - xhprof
			
#官网下载 http://pecl.php.net/package/xhprof tar zxf xhprof-0.9.2.tgz cd xhprof-0.9.2/extension/ sud ...
 - 关于gradle /Users/xxxx/Documents/workspace/fontmanager/.gradle/2.2.1/taskArtifacts/cache.properties (No such file or directory)报错办法
			
转自:http://www.cnblogs.com/raomengyang/p/4367620.html Android Studio报错: What went wrong: java.io.Fi ...
 - CCNP路由实验(1) -- EIGRP
			
EIGRP(Enhanced Interior Gateway Routing Protocol,增强型内部网关路由协议)是Cisco公司开发的一个平衡混合型路由协议,它融合了距离向量和链路状态两种路 ...
 - Java 初学者帮助文档以及基础教程
			
一下午的时间,大致看了一下Java的文档,进一步熟悉了Java的大体框架和结构,整理了一下有用的资源. 帮助文档: JSE 8 API 英文版 在线HTML格式:http://docs.oracle. ...
 - iOS判断机型
			
#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> typedef NS_OPTIONS(NSInteger,D ...
 - Primefaces 中e.offset(...)问题的处理
			
问题 在使用Primefaces构建的页面中,原来好好的页面莫名奇异的出现下拉框不能显示数据且点击没有反应的情况.后来通过firefox发现其JS抛出了一个e.offset(...)错误 解决方法 经 ...
 - iOS 中 Touch ID得使用方法
			
iPhone 5S公布以后,iOS设备基本都有集成Touch ID.而这个功能对自己的App也是一个非常好的扩展,关于Touch ID的使用方法.大致例如以下, Swift: 引入LocalAuthe ...
 - c语言中重要函数
			
gets函数,从标准输入读取一行文本,一行输入由一串字符组成,以一个换行符结尾: gets函数丢弃换行符,并在该行的末尾存储一个NUL字符(类似‘\0’), 然后返回一个非NULL值. 当gets函数 ...