Network of Schools
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 13804   Accepted: 5507

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
2 4 3 0
4 5 0
0
0
1 0

Sample Output

1
2

Source

 
题意:

N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输,问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件。2,至少需要添加几条传输线路(边),使任意向一个学校发放软件后,经过若干次传送,网络内所有的学校最终都能得到软件。

也就是:给定一个有向图,求:

1) 至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点

2) 至少要加多少条边,才能使得从任何一个顶点出发,都能到达全部顶点

思路:先求出所有连通分量,将每个连通分量缩成一点,则形成一个有向无环图DAG。为题1的答案就是DAG中入度为0的点个数。问题2等价于在DAG中最少加几条边才能变成强连通。

要为每个入度为0的点加入边,为每个出度为0的点加出边,假设有n个入度为0的点,m个出度为0的点,则答案一定是min(n, m)。另外需要注意的是如果整个图只有一个强连通分支的时候,即缩点后只有一个点,则不需要加边,输出0。

/*
ID: LinKArftc
PROG: 1236.cpp
LANG: C++
*/ #include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define randin srand((unsigned int)time(NULL))
#define input freopen("input.txt","r",stdin)
#define debug(s) cout << "s = " << s << endl;
#define outstars cout << "*************" << endl;
const double PI = acos(-1.0);
const double e = exp(1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
typedef long long ll; const int maxn = ;
const int maxm = ; struct Edge {
int v, next;
} edge[maxm]; int tot, head[maxn]; void init() {
tot = ;
memset(head, -, sizeof(head));
} void addedge(int u, int v) {
edge[tot].v = v;
edge[tot].next = head[u];
head[u] = tot ++;
} int n, m; int dfn[maxn], low[maxn], ins[maxn], belong[maxn];
int scc, Time;
stack <int> st;
vector <int> vec[maxn]; void tarjan(int u) {
dfn[u] = low[u] = ++ Time;
int v;
st.push(u);
ins[u] = true;
for (int i = head[u]; i + ; i = edge[i].next) {
v = edge[i].v;
if (!dfn[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
} else if (ins[v]) low[u] = min(low[u], low[v]);
}
if (low[u] == dfn[u]) {
scc ++;
do {
v = st.top();
st.pop();
ins[v] = false;
vec[scc].push_back(v);
belong[v] = scc;
} while (u != v);
}
} int indeg[maxn], outdeg[maxn]; int main() {
//input;
int v;
while (~scanf("%d", &n)) {
init();
for (int i = ; i <= n; i ++) {
while (~scanf("%d", &v) && v) {
addedge(i, v);
}
}
while (!st.empty()) st.pop();
for (int i = ; i <= n; i ++) vec[i].clear();
memset(dfn, , sizeof(dfn));
memset(ins, , sizeof(ins));
Time = ;
scc = ;
for (int i = ; i <= n; i ++) {
if (!dfn[i]) tarjan(i);
}
memset(indeg, , sizeof(indeg));
memset(outdeg, , sizeof(outdeg));
for (int u = ; u <= n; u ++) {
for (int i = head[u]; i + ; i = edge[i].next) {
v = edge[i].v;
if (belong[u] == belong[v]) continue;
outdeg[belong[u]] ++;
indeg[belong[v]] ++;
}
}
int incnt = , outcnt = ;
for (int i = ; i <= scc; i ++) {
if (indeg[i] == ) incnt ++;
if (outdeg[i] == ) outcnt ++;
}
printf("%d\n", incnt);
if (scc == ) printf("0\n");
else printf("%d\n", max(incnt, outcnt)); } return ;
}

POJ1236 (强连通分量缩点求入度为0和出度为0的分量个数)的更多相关文章

  1. POJ 1236 Network Of Schools (强连通分量缩点求出度为0的和入度为0的分量个数)

    Network of Schools A number of schools are connected to a computer network. Agreements have been dev ...

  2. POJ 1236 Network of Schools (强连通分量缩点求度数)

    题意: 求一个有向图中: (1)要选几个点才能把的点走遍 (2)要添加多少条边使得整个图强联通 分析: 对于问题1, 我们只要求出缩点后的图有多少个入度为0的scc就好, 因为有入度的scc可以从其他 ...

  3. Tarjan缩点求入度为零的点的个数问题

    Description: 一堆人需要联系,但如果x 可以联系 y,你联系了x就不用联系y了,你联系一个人都会有固定的花费,问你最小联系多少人,和最小花费 Solution: Tarjan缩点,求出缩点 ...

  4. POJ1236 强连通 (缩点后度数的应用)

    题意:       一些学校有一个发送消息的体系,现在给你一些可以直接发送消息的一些关系(单向)然后有两个问题 (1) 问你至少向多少个学校发送消息可以让所有的学校都得到消息 (2) 问至少加多少条边 ...

  5. poj 3177 Redundant Paths【求最少添加多少条边可以使图变成双连通图】【缩点后求入度为1的点个数】

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11047   Accepted: 4725 ...

  6. POJ1236Network of Schools(强连通分量 + 缩点)

    题目链接Network of Schools 参考斌神博客 强连通分量缩点求入度为0的个数和出度为0的分量个数 题目大意:N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后 ...

  7. POJ 2186 Popular Cows(强连通分量缩点)

    题目链接:http://poj.org/problem?id=2186 题目意思大概是:给定N(N<=10000)个点和M(M<=50000)条有向边,求有多少个“受欢迎的点”.所谓的“受 ...

  8. 缩点+出入度 poj1236

    题目链接:https://vjudge.net/contest/219056#problem/H 题意:先输入n,代表接下来有n个点,接下来n行,第i行里面的数(假设是)a,b...0(到0表示结束) ...

  9. 【强连通分量缩点】poj 1236 Network of Schools

    poj.org/problem?id=1236 [题意] 给定一个有向图,求: (1)至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点 (2)至少要加多少条边,才能使得从任何一个顶点出发,都 ...

随机推荐

  1. Python学习笔记(二)一一一字典总结

    创建方式:1 直接创建     newDictonary={‘key’:'value',} 2 列表转字典(dict函数) 3 基本操作:len 返回总数 dictionary[k]  返回k对应的值 ...

  2. (三)宇宙大战 Space Battle -- 场景SCENE切换、UserDefaults统计分数、Particle粒子效果

    此<宇宙大战 Space Battle>SpirteKit手机游戏教程共分为三系列: (一)宇宙大战 Space Battle -- 新建场景Scene.精灵节点.Particle粒子及背 ...

  3. Tomcat突然用开发工具启动不起来,只报了个红色的警告,没有其他任何异常

    碰到这个问题是,是因为我的catalina.bat文件做了配置修改,导致与工具这边的启动设置起了冲突 下面这个是我在Catalina.bat中新增的配置,删掉这个就可以了 set JAVA_OPTS= ...

  4. Java中大数的使用与Java入门(NCPC-Intergalactic Bidding)

    引入 前几天参加湖南多校的比赛,其中有这样一道题,需要使用高精度,同时需要排序,如果用c++实现的话,重载运算符很麻烦,于是直接学习了一发怎样用Java写大数,同时也算是学习Java基本常识了 题目 ...

  5. 输出1-n的全排(递归C++)

    [问题描述] 输出1到n之间所有不重复的排列,即1到n的全排,要求所产生的任一数列不含有重复的数字. [代码展示] #include<iostream>using namespace st ...

  6. 基于语音转录的ted演讲推荐

    论文地址:https://arxiv.org/abs/1809.05350v1 二.  实现 我们从Kaggle[6]中获取了TED演讲数据集,其中包括2400个TED演讲的数据,包括标题.演讲者.标 ...

  7. 深度学习anchor的理解

    摘抄与某乎 anchor 让网络学习到的是一种推断的能力.网络不会认为它拿到的这一小块 feature map 具有七十二变的能力,能同时从 9 种不同的 anchor 区域得到.拥有 anchor ...

  8. POJ 3860 Fruit Weights(数学+最长路径 or 最短路径)

    Description Have you ever thought about comparing the weight of fruits? That’s what you should do in ...

  9. Java 实现一个带提醒的定时器

    定时闹钟预览版EXE下载链接:https://files.cnblogs.com/files/rekent/ReadytoRelax_jar.zip 功能说明: 实现了一个休息提醒器,用户首先设定一个 ...

  10. winform timer时间间隔小于执行时间

    如果SetTimer的时间间隔为t,其响应事件OnTimer代码执行一遍的时间为T,且T>t.这样,一次未执行完毕,下一次定时到,这时候程序会如何执行? 可能的情况:1.丢弃还未执行的代码,开始 ...