Network of Schools(POJ1326+有向图进行缩点)
题目链接:http://poj.org/problem?id=1236
题目:


题意:对于n个学校,对于一个系统传给某个学校,那么他会传给他得支援学校。从第二开始,每行给你多个数字,表示第i个学校可以支援这些学校,以0结尾。问你一个新软件至少要给多少个学校,如果任意传给某个学校都能传给其他学校需要建多少条支援关系。
思路:tarjan进行缩点,重新建图,对新建得有向无环图统计一下出度和入度,第一问答案就是入度为0的数量,第二问则是max(入度为0的个数,出度为0的个数)。
代码实现如下:
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;;
typedef pair<int, int> pii;
typedef unsigned long long ull; #define lson i<<1
#define rson i<<1|1
#define bug printf("*********\n");
#define FIN freopen("D://code//in.txt", "r", stdin);
#define debug(x) cout<<"["<<x<<"]" <<endl;
#define IO ios::sync_with_stdio(false),cin.tie(0); const double eps = 1e-;
const int mod = ;
const int maxn = + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f; int n, tot, x, cnt, num, top;
int head[maxn], c[maxn];
int in[maxn], out[maxn], vis[maxn];
int dfn[maxn], low[maxn], stc[maxn]; void init() {
tot = cnt= num = top = ;
memset(c, , sizeof(c));
memset(in, , sizeof(in));
memset(out, , sizeof(out));
memset(vis, , sizeof(vis));
memset(stc, , sizeof(stc));
memset(dfn, , sizeof(dfn));
memset(low, , sizeof(low));
memset(head, -, sizeof(head));
} struct edge {
int v, next;
}ed[maxn*maxn]; void addedge(int u, int v) {
ed[tot].v = v;
ed[tot].next = head[u];
head[u] = tot++;
} void tarjan(int x) {
dfn[x] = low[x] = ++num;
stc[++top] = x, vis[x] = ;
for(int i = head[x]; ~i; i = ed[i].next) {
int y = ed[i].v;
if(!dfn[y]) {
tarjan(y);
low[x] = min(low[x], low[y]);
} else if(vis[y]) {
low[x] = min(low[x], low[y]);
}
}
if(dfn[x] == low[x]) {
cnt++; int y;
do {
y = stc[top--]; vis[y] = ;
c[y] = cnt;
}while(x != y);
}
} int main() {
//FIN;
scanf("%d", &n);
init();
for(int i = ; i <= n; i++) {
while() {
scanf("%d", &x);
if(x == ) break;
addedge(i, x);
}
}
for(int i = ; i <= n; i++) {
if(!dfn[i]) {
tarjan(i);
}
}
for(int i = ; i <= n; i++) {
for(int j = head[i]; ~j; j = ed[j].next) {
int y = ed[j].v;
if(c[i] == c[y]) continue;
out[c[i]]++;
in[c[y]]++;
}
}
if(cnt == ) {
printf("1\n0\n");
return ;
}
int ans1 = , ans2 = ;
for(int i = ; i <= cnt; i++) {
if(in[i] == ) ans1++;
if(out[i] == ) ans2++;
}
printf("%d\n%d\n", ans1, max(ans1, ans2));
return ;
}
Network of Schools(POJ1326+有向图进行缩点)的更多相关文章
- POJ 1236 Network of Schools(强连通 Tarjan+缩点)
POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意: 给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...
- POJ 1236 Network of Schools (有向图的强连通分量)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9073 Accepted: 359 ...
- poj 1236 Network of Schools(又是强连通分量+缩点)
http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- poj1236 Network of Schools ,有向图求强连通分量(Tarjan算法),缩点
题目链接: 点击打开链接 题意: 给定一个有向图,求: 1) 至少要选几个顶点.才干做到从这些顶点出发,能够到达所有顶点 2) 至少要加多少条边.才干使得从不论什么一个顶点出发,都能到达所有顶点 ...
- poj 1236 Network of Schools(强连通、缩点、出入度)
题意:给出一个有向图.1:问至少选出多少个点,才能沿有向边遍历所有节点.2:问至少加多少条有向边,使原图强连通. 分析:第一个问题,缩点后找所有树根(入度为0).第二个问题,分别找出入度为0和出度为0 ...
- POJ 1236 Network of Schools (tarjan算法+缩点)
思路:使用tarjan求强连通分量并进行缩点,判断所有入度为0的点,这个点就是必须要给予文件的点,分别计算出度,入度为零的点的个数,取二者的最大值就是把这个图变成强连通需要加的边数. 一个取值需要讨论 ...
- Network of Schools POJ - 1236(强连通+缩点)
题目大意 有N个学校,这些学校之间用一些单向边连接,若学校A连接到学校B(B不一定连接到A),那么给学校A发一套软件,则学校B也可以获得.现给出学校之间的连接关系,求出至少给几个学校分发软件,才能使得 ...
- poj 1236 Network of Schools (强连通分量+缩点)
题目大概: 每个学校都可以把软件复制好,交给它名单上的学校. 问题A:把软件复制成几份,然后交给不同的学校,所有学校才能够都有软件. 问题B:添加几条边,能使得这个图变成强连通图. 思路: 找出所有的 ...
- POJ - 1236 Network of Schools(有向图的强连通分量)
d.各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输, 问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件. 问题2:至少需要添加几条传输线 ...
随机推荐
- noauth authentication required redis
解决方案: 这是出现了认证的问题,是因为设置了认证密码. 127.0.0.1:6379> auth "yourpassword" 例如:
- C语言100例02 PHP版(练习)
问题: 企业发放的奖金根据利润提成. 利润(I)低于或等于10万元时,奖金可提10%: 利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%: 20万到 ...
- [STL] vector基本用法
vector的数据安排以及操作方式,与array非常相似.两者的唯一区别在于空间的运用的灵活性.array是静态空间,一旦配置了就不能改变.vector是动态空间,随着元素的加入,它的内部机制会自行扩 ...
- Kubernetes初探 :总体概述及使用示例
Kubernetes是Google开源的容器集群管理系统.它构建于docker技术之上,为容器化的应用提供资源调度.部署运行.服务发现.扩容缩容等整一套功能,本质上可看作是基于容器技术的mini-Pa ...
- [洛谷P4139]上帝与集合的正确用法
题目大意:多次询问,每次给你$p$询问$2^{2^{2^{\dots}}}\bmod p$ 题解:扩展欧拉定理,求出$\varphi(p)$即可.因为$2^{2^{2^{\dots}}}>> ...
- 分享几款常用的API/文档浏览器
1.Dash 支持平台:Mac iOS 官网:https://kapeli.com/dash 2.Zeal 支持平台:Linux Windows 官网:https://zealdocs.org/ G ...
- MySQL安装出现“不是内部或外部命令,也不是可执行程序”等一系列问题的解决方案
MySQL安装出现“不是内部或外部命令,也不是可执行程序” 一.这是应该是环境变量处问题了,设置如下: 1)右击我的电脑选择“属性”,找到“高级系统设置” 2)在系统属性下,选择“高级”中的“环境变量 ...
- MyBatis之二级缓存
二级缓存与一级缓存区别:二级缓存的范围更大,多个sqlSession可以共享一个UserMapper的二级缓存区域. 每一个mapper都有一个自己的二缓存区域(按namespace区分),两个map ...
- 【队列】【P2827】【NOIP2016D2T3】蚯蚓
传送门 Description 本题中,我们将用符号 $\lfloor c \rfloor$ 表示对 $c$ 向下取整,例如:$\lfloor 3.0 \rfloor = \lfloor 3.1 \r ...
- SpringMVC配置详解(转)
要想灵活运用Spring MVC来应对大多数的Web开发,就必须要掌握它的配置及原理. 一.Spring MVC环境搭建:(Spring 2.5.6 + Hibernate 3.2.0) 1. jar ...