POJ1236 (强连通分量缩点求入度为0和出度为0的分量个数)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 13804 | Accepted: 5507 |
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
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的分量个数)的更多相关文章
- 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可以从其他 ...
- Tarjan缩点求入度为零的点的个数问题
Description: 一堆人需要联系,但如果x 可以联系 y,你联系了x就不用联系y了,你联系一个人都会有固定的花费,问你最小联系多少人,和最小花费 Solution: Tarjan缩点,求出缩点 ...
- POJ1236 强连通 (缩点后度数的应用)
题意: 一些学校有一个发送消息的体系,现在给你一些可以直接发送消息的一些关系(单向)然后有两个问题 (1) 问你至少向多少个学校发送消息可以让所有的学校都得到消息 (2) 问至少加多少条边 ...
- poj 3177 Redundant Paths【求最少添加多少条边可以使图变成双连通图】【缩点后求入度为1的点个数】
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11047 Accepted: 4725 ...
- POJ1236Network of Schools(强连通分量 + 缩点)
题目链接Network of Schools 参考斌神博客 强连通分量缩点求入度为0的个数和出度为0的分量个数 题目大意:N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后 ...
- POJ 2186 Popular Cows(强连通分量缩点)
题目链接:http://poj.org/problem?id=2186 题目意思大概是:给定N(N<=10000)个点和M(M<=50000)条有向边,求有多少个“受欢迎的点”.所谓的“受 ...
- 缩点+出入度 poj1236
题目链接:https://vjudge.net/contest/219056#problem/H 题意:先输入n,代表接下来有n个点,接下来n行,第i行里面的数(假设是)a,b...0(到0表示结束) ...
- 【强连通分量缩点】poj 1236 Network of Schools
poj.org/problem?id=1236 [题意] 给定一个有向图,求: (1)至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点 (2)至少要加多少条边,才能使得从任何一个顶点出发,都 ...
随机推荐
- java---解析XML文件,通过反射动态将XML内容封装到一个类中
本博客讲的XML解析,使用的是dom4j. 首先建立一个maven项目,在dom.xml中引入相应的dom4j的版本.作者下载的是热度很高的1.6.1版本.maven的使用在这里不做详细讲解. 引入成 ...
- 解决Unbuntu终端菱形乱码问题
原因:安装时为了学习方便选择中文安装,其字符编码相关配置如下(在/etc/default/locale中) LANG="Zh_CN.UTF-8 "LANGUAGE="zh ...
- Spring实战第九章学习笔记————保护Web应用
保护Web应用 在这一章我们将使用切面技术来探索保护应用程序的方式.不过我们不必自己开发这些切面----我们将使用Spring Security,一种基于Spring AOP和Servlet规范的Fi ...
- C语言运算符(注意事项)
1.C语言取余注意事项:% a.求余.模运算符(%)时要求两数必须是整型数据. b.取余的结果,是取决于被除数 (不管除数是正数 还是 负数,模的符号与被除数的符号相同). 例:8÷2=4 ...
- 学习materialize
<div class="container"> <div class="row"> </div> <div cla ...
- Julia 学习笔记(一):数组
个人向,只会记录一些需要注意的点. 前言 学习 Julia 已经有一段时间了,但是进步缓慢.这一方面是最近代码写得少,一方面是 Julia 学习资料少.中文资料更少,但也有我没做笔记的缘故导致学习效率 ...
- git - work flow
git status – Make sure your current area is clean. git pull – Get the latest version from the remote ...
- POJ 3860 Fruit Weights(数学+最长路径 or 最短路径)
Description Have you ever thought about comparing the weight of fruits? That’s what you should do in ...
- (转)MongoDB numa系列问题三:overcommit_memory和zone_reclaim_mode
内核参数overcommit_memory : 它是 内存分配策略 可选值:0.1.2.0:表示内核将检查是否有足够的可用内存供应用进程使用:如果有足够的可用内存,内存申请允许:否则,内存申请失败,并 ...
- js 给某个div增加class 样式(三种方式)
第一种: el.setAttribute('class','abc'); <!DOCTYPE HTML> <HTML> <HEAD> <meta c ...