<题目链接>

题目大意:

有N个学校,每个学校之间单向可以发送软件,现在给你一些学校之间的收发关系。问你下面两个问题:至少要给多少个学校发送软件才能使得最终所有学校都收到软件;至少要多加多少个关系才能使得向任意一个学校发送一套软件,每个学校都能收到软件。 

解题分析:

首先,对该图进行缩点,显然第一问问的就是,缩点后的入度为0的联通块的数量(因为这些点没有入度,必须人为的给它们软件,它们才能接收到软件);第二问,显然就是问至少要加多少条边,使得该图变为强连通图,强连通图有个条件,就是所有的点一定要有出度和入度,所以我们可以让没有出度的点连上没有入度的点,等到出度或者入度为0的点不存在时,再把出度或入度为0的点补完(不能自己连自己,因为题目要求的是最少需要多少条边,所以考虑最优情况),注意,当整张图为连通图时,它的出度和入度均为0,max(1,1)=1,但是实际上不需要补边,所以这种情况需要特判。

 #include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
using namespace std; const int M = 1e5 + ;
int n, m, u, v, tot, top, cnt, col;
struct node {
int v, next;
} edge[M];
int head[M], instack[M], stk[M];
int dfn[M], low[M], belong[M],in[M],out[M];
void init() {
tot = cnt = top = col = ;
memset(stk, , sizeof(stk));
memset(head, -, sizeof(head));
memset(dfn, , sizeof(dfn));
memset(instack, , sizeof(instack));
}
void add(int u, int v) {
edge[tot].v = v,edge[tot].next = head[u];
head[u] = tot++;
}
void tarjan(int u) {
dfn[u] = low[u] = ++col; //col为遍历到该点的编号时间
instack[u] = ; //标记该元素是否在栈里
stk[top++] = u;
for (int i = head[u] ; ~i ; i = edge[i].next) {
int v = edge[i].u;
if (!dfn[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
}
else if (instack[v]) low[u] = min(low[u], dfn[v]);
}
if (dfn[u] == low[u]) {
cnt++; //记录连通块的数量
int tmp;
do{
tmp = stk[--top];
instack[tmp] = ;
belong[tmp] = cnt; //给该连通块中的点染色
} while(tmp != u) ;
}
}
void solve() {
for (int i = ; i <= n ; i++)
if (!dfn[i]) tarjan(i);
}
int main() {
while(scanf("%d", &n) != EOF) {
init();
memset(in, , sizeof(in));
memset(out, , sizeof(out));
for (int i = ; i <= n ; i++) {
int v;
scanf("%d", &v);
while(v) {
add(i, v);
scanf("%d", &v);
}
}
for (int i = ; i <= n ; i++)
if (!dfn[i]) tarjan(i);
for (int i = ; i <= n ; i++) {
for (int j = head[i] ; ~j ; j = edge[j].next) {
if (belong[edge[j].v] != belong[i]) {
in[belong[edge[j].v]]++; //统计每个连通块的出度和入度
out[belong[i]]++;
}
}
}
int sumin = , sumout = ;
for (int i = ; i <= cnt ; i++) { //遍历每个连通块
if (!in[i]) sumin++; //入度为0的连通分量个数
if (!out[i])sumout++; //出度为0的连通分量个数
}
printf("%d\n", sumin);
if (cnt == ) printf("0\n"); //如果该图已经是一个强连通图,只有一个强连通分量,则不需要加边
else printf("%d\n", max(sumin, sumout));
}
return ;
}

2018-09-30

POJ 1236 Network Of Schools 【Targan】+【缩点】的更多相关文章

  1. POJ 1236 Network of Schools Tarjan缩点

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22729   Accepted: 89 ...

  2. POJ 1236 Network of Schools (Tarjan + 缩点)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12240   Accepted: 48 ...

  3. POJ 1236 Network of Schools 连通图缩点

    题目大意:有向图连通图,第一问求至少需要多少个软件才能传输到所有学校,第二问求至少需要增加多少条路使其成为强连通图 题目思路:利用Tarjan算法经行缩点,第一问就是求缩点后入度为0的点的个数(特殊情 ...

  4. POJ 1236 Network of Schools —— (缩点的应用)

    题目大意:有N个学校和一些有向边将它们连结,求: 1.最少需要向几个学校发放软件,使得他们中的每一个学校最终都能够获得软件. 2.最少需要增加几条有向边使得可以从任意一个学校发放软件,使得每一个学校最 ...

  5. POJ 1236 Network of Schools(强连通 Tarjan+缩点)

    POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意:  给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...

  6. Poj 1236 Network of Schools (Tarjan)

    题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个 ...

  7. POJ 1236 Network of Schools(强连通分量)

    POJ 1236 Network of Schools 题目链接 题意:题意本质上就是,给定一个有向图,问两个问题 1.从哪几个顶点出发,能走全全部点 2.最少连几条边,使得图强连通 思路: #inc ...

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

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

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

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

  10. [tarjan] poj 1236 Network of Schools

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

随机推荐

  1. 简化版的AXI-LITE4和配合使用的RTL

    ////////////////////////////////////////////////////////////////////////////////// // // The ZYNQ FI ...

  2. MybatisPlus使用介绍

    创建UserController测试类 package com.cppdy.controller; import org.apache.ibatis.session.RowBounds; import ...

  3. Mycat配置文件详解及全局序列号

    来详细的看看 mycat的配置文件,更多信息请查看:mycat权威指南. schema.xml: Schema.xml 作为 MyCat 中重要的配置文件之一,管理着 MyCat 的逻辑库.表.分片规 ...

  4. LeetCode(97):交错字符串

    Hard! 题目描述: 给定三个字符串 s1, s2, s3, 验证 s3 是否是由 s1 和 s2 交错组成的. 示例 1: 输入: s1 = "aabcc", s2 = &qu ...

  5. LeetCode(65):有效数字

    Hard! 题目描述: 验证给定的字符串是否为数字. 例如:"0" => true" 0.1 " => true"abc" =& ...

  6. 网络编程—tcp

    一.TCP简介 TCP介绍 TCP协议,传输控制协议(英语:Transmission Control Protocol,缩写为 TCP)是一种面向连接的.可靠的.基于字节流的传输层通信协议,由IETF ...

  7. bzoj 1042

    典型的背包+容斥 首先,考虑如果没有个数的限制,那么就是一个完全背包,所以先跑一个完全背包,求出没有个数限制的方案数即可 接下来,如果有个数的限制,那么我们就要利用一些容斥的思想:没有1个超过限制的方 ...

  8. N阶楼梯上楼问题

    N阶楼梯上楼问题 时间限制: 1 Sec  内存限制: 32 MB 题目描述 样例输出 13 #include <stdio.h> int main() { int i, n; long ...

  9. Spring Boot的Listener机制的用法和实现原理详解

    之前在介绍了在spring-boot启动过程中调用runner的原理,今天我们介绍另外一种可以实现相似功能的机制:spring-boot的Listener机制. 通过注册Listener,可以实现对于 ...

  10. Jmeter测试demo

    复制代码,保存为.jmx文件 需要安装插件: JMeterPlugins-ExtrasLibs E:\软件\apache-jmeter-3.0\lib\ext <?xml version=&qu ...