poj-1236.network of schools(强连通分量 + 图的入度出度)
Network of Schools
Description 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 Sample Output 1 Source |
/*************************************************************************
> File Name: poj-1236.network_of_schools.cpp
> Author: CruelKing
> Mail: 2016586625@qq.com
> Created Time: 2019年09月04日 星期三 19时53分40秒
本题大意:给定一个有向图,第一问是让你找出一些点,使得从这些点出发,可以到达图中的所有结点,输出结点数,第二问是问你在图中添加多少条边可以使得从任一点出发都可以访问到图中的其他所有结点.
本题思路:很典型的连通图问题,考虑第一问,求出图中所有的强连通分量,缩点之后建立新图,图中入度为0的点即是这些点,考虑第二问,由于求得的强连通分量都是可以相互到达的,因此我们只需要解决生成的新图的连通性问题,
也就是添加最少的边使得新图形成一个强连通分量,那最优的思路就是选一个入度为零的点让其他所有出度为零的点都指向他,或者选一个出度为零的点,让他指向每个入度为零的点,所以答案就是出度为零和入度为零间的最大值,需要特判的
是,如果原图就是一个强连通分量,那么就不需要加边,所求的的新图应该是一个点,所以这个答案需要特判,切记以上判断结点的出度入度都是求原图的出度和入度.
************************************************************************/ #include <cstdio>
#include <cstring>
using namespace std; const int maxn = + , maxm = * / + ;
int n;
struct Edge {
int from, to, next;
} edge[maxm], edge1[maxm];
int head[maxn], tot;
int low[maxn], dfn[maxn], stack[maxn], belong[maxn];
int Index, top;
int scc;
bool instack[maxn];
bool indegree[maxn];
bool outdegree[maxn]; int max(int a, int b) {
return a > b ? a : b;
} void init() {
tot = ;
memset(head, -,sizeof head);
} void addedge(int u, int v) {
edge[tot] = (Edge){u, v, head[u]}; head[u] = tot ++;
} void tarjan(int u) {
int v;
low[u] = dfn[u] = ++ Index;
stack[top ++] = u;
instack[u] = true;
for(int i = head[u]; ~i; i = edge[i].next) {
v = edge[i].to;
if(!dfn[v]) {
tarjan(v);
if(low[u] > low[v]) low[u] = low[v];
} else if(instack[v] && low[u] > dfn[v]) low[u] = dfn[v];
}
if(low[u] == dfn[u]) {
scc ++;
do {
v = stack[-- top];
instack[v] = false;
belong[v] = scc;
} while(v != u);
}
} void solve() {
memset(dfn, , sizeof dfn);
memset(instack, false, sizeof instack);
Index = scc = top = ;
for(int i = ; i <= n; i ++)
if(!dfn[i]) {
tarjan(i);
}
} bool vis[maxn]; int main() {
memset(indegree, false, sizeof indegree);
memset(outdegree, false, sizeof outdegree);
init();
scanf("%d", &n);
int x;
for(int i = ; i <= n; i ++) {
while(scanf("%d", &x) && x)
addedge(i, x);
}
solve();
for(int i = ; i <= n; i ++)
for(int k = head[i]; ~k; k = edge[k].next)
if(belong[i] != belong[edge[k].to]) {
indegree[belong[edge[k].to]] = true;
outdegree[belong[edge[k].from]] = true;
}
int in0 = , out0 = ;
for(int i = ; i <= scc; i ++) {
if(!indegree[i]) in0 ++;
if(!outdegree[i]) out0 ++;
}
out0 = max(in0, out0);
if(scc == ) out0 = ;
printf("%d\n%d\n", in0, out0);
return ;
}
poj-1236.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 ...
- POJ 1236 Network of Schools (强连通分量缩点求度数)
题意: 求一个有向图中: (1)要选几个点才能把的点走遍 (2)要添加多少条边使得整个图强联通 分析: 对于问题1, 我们只要求出缩点后的图有多少个入度为0的scc就好, 因为有入度的scc可以从其他 ...
- 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 强连通入门题
一些学校连接到计算机网络.这些学校之间已经达成了协议: 每所学校都有一份分发软件的学校名单("接收学校"). 请注意,如果B在学校A的分发名单中,则A不一定出现在学校B的名单中您需 ...
- POJ 1236 Network of Schools(强连通分量)
POJ 1236 Network of Schools 题目链接 题意:题意本质上就是,给定一个有向图,问两个问题 1.从哪几个顶点出发,能走全全部点 2.最少连几条边,使得图强连通 思路: #inc ...
- POJ 1236 Network of Schools(强连通 Tarjan+缩点)
POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意: 给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...
- Poj 1236 Network of Schools (Tarjan)
题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个 ...
- poj 1236 Network of Schools(又是强连通分量+缩点)
http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- [tarjan] poj 1236 Network of Schools
主题链接: http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K To ...
随机推荐
- Spring 跨重定向请求传递数据
在处理完POST请求后, 通常来讲一个最佳实践就是执行一下重定向.除了其他的一些因素外,这样做能够防止用户点击浏览器的刷新按钮或后退箭头时,客户端重新执行危险的POST请求. 在控制器方法返回的视图名 ...
- 【NOIP2017模拟6.25】小W的动漫
题目 小W最近迷上了日本动漫,每天都有无数部动漫的更新等着他去看,所以他必须将所有的动漫排个顺序,当然,虽然有无数部动漫,但除了1号动漫,每部动漫都有且仅有一部动漫是它的前传(父亲),也就是说,所有的 ...
- 【NOIP2016提高A组模拟9.15】Math
题目 分析 因为\((-1)^2=1\), 所以我们只用看\(\sum_{j=1}^md(i·j)\)的值模2的值就可以了. 易证,一个数x,只有当x是完全平方数时,d(x)才为奇数,否则为偶数. 那 ...
- 定时器的写法 winform
private void timer1_Tick(object sender, EventArgs e) { if ( tttttflag) { Action action = SyncCompany ...
- MoreExecutors工具类使用
MoreExecutors是guava提供的工具类,是对jdk自带的Executors工具类的扩展,主要方法如下: 1.addDelayedShutDown()方法的两个重载: public stat ...
- NOIP2018 D1T3赛道修建
题目链接:Click here Solution: 最小值最大,考虑二分一个答案\(k\) 考虑在子树内先匹配,最后传递一个值给自己的父亲(因为每条边只能用一次,所以一颗子树最多传递一个值) 那么我们 ...
- CodeForces 1200D White Lines
cf题面 Time limit 1500 ms Memory limit 262144 kB 解题思路 官方题解 1200D - White Lines Let's consider a single ...
- Linux宝塔面板FTP无法连接的解决办法,跳坑实例
宝塔面板的ftp无法使用解决 先检查这些内容 1.注意内网IP和外网IP 2.检查ftp服务是否启动 (面板首页即可看到) 3.检查防火墙20端口 ftp 21端口及被动端口39000 - 40000 ...
- javascript二叉树
javascript中的二叉树一(binary tree) 毕业也快2年了,毕业之后就没有认真写过博客了,都是那里学习一下,这里弄一下.学习了也不做笔记,过后就忘记了.我对这种状态打从心里是讨厌的. ...
- 第四周总结 & 实验报告(二)
第四周课程总结 一.String类 1.实例化 (1)直接赋值 public class Xxxx{ public static void main(String args[]){ String a ...