POJ1236-Network of Schools(Tarjan + 缩点)
题意:给定一张有向图,问最少选择几个点能遍历全图。以及最少加入几条边使得有向图成为一个强连通图。
思路:对于有向图而言,首先求出有几个强连通分量,之后将每一个强连通分量缩点,形成DAG。本题开头第一句就说图是连通的了。
之后想要遍历整张图的话。仅仅要找出入度为0的点有几个,而加入边的数量就取决于全部点的出入度大小。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <stack>
#include <algorithm> using namespace std; const int MAXN = 105; vector<int> g[MAXN];
stack<int> s;
int pre[MAXN], lowlink[MAXN], sccno[MAXN], dfs_clock, scc_cnt;
int n, in[MAXN], out[MAXN]; void tarjan(int u) {
pre[u] = lowlink[u] = ++dfs_clock;
s.push(u);
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (!pre[v]) {
tarjan(v);
lowlink[u] = min(lowlink[u], lowlink[v]);
}
else if (!sccno[v])
lowlink[u] = min(lowlink[u], pre[v]);
}
if (lowlink[u] == pre[u]) {
scc_cnt++;
int x = -1;
while (x != u) {
x = s.top();
s.pop();
sccno[x] = scc_cnt;
}
}
} void find_scc() {
memset(pre, 0, sizeof(pre));
memset(sccno, 0, sizeof(sccno));
memset(lowlink, 0, sizeof(lowlink));
dfs_clock = scc_cnt = 0;
for (int i = 1; i <= n; i++)
if (!pre[i])
tarjan(i);
} int main() {
while (scanf("%d", &n) != EOF) {
int u;
for (int i = 1; i <= n; i++) {
g[i].clear();
while (scanf("%d", &u) && u)
g[i].push_back(u);
} find_scc();
if (scc_cnt == 1) {
printf("1\n0\n");
continue;
}
else {
memset(in, 0, sizeof(in));
memset(out, 0, sizeof(out));
for (int u = 1; u <= n; u++) {
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (sccno[u] != sccno[v]) {
in[sccno[v]]++;
out[sccno[u]]++;
}
}
}
int a = 0, b = 0;
for (int i = 1; i <= scc_cnt; i++) {
if (in[i] == 0) a++;
if (out[i]== 0) b++;
}
int ans = max(a, b);
printf("%d\n%d\n", a, ans);
}
}
return 0;
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
POJ1236-Network of Schools(Tarjan + 缩点)的更多相关文章
- POJ1236 - Network of Schools tarjan
Network of Schools Time Limit: 1000MS Memory Limi ...
- POJ 1236 Network of Schools Tarjan缩点
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22729 Accepted: 89 ...
- POJ 1236 Network of Schools (Tarjan + 缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12240 Accepted: 48 ...
- P2746 [USACO5.3]校园网Network of Schools tarjan 缩点
题意 给出一个有向图,A任务:求最少需要从几个点送入信息,使得信息可以通过有向图走遍每一个点B任务:求最少需要加入几条边,使得有向图是一个强联通分量 思路 任务A,比较好想,可以通过tarjan缩点, ...
- [poj1236]Network of Schools(targin缩点SCC)
题意:有N个学校,从每个学校都能从一个单向网络到另外一个学校.1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件.2:至少需要添加几条边,使任意向一个学校发放软件后,经过若干次 ...
- P2746 [USACO5.3]校园网Network of Schools [tarjan缩点]
题目描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学校都会给其它的一些学校分发软件(称作“接受学校”).注意即使 BB 在 AA 学校的分发列表中,AA 也不一定在 BB 学校的列表中. ...
- poj1236 Network of Schools(SCC缩点+结论推导)
第一问简单不讲. 第二问简化后问题是给一张DAG求最少添加几条边使得DAG变成一个SCC.首先所有中间点(有入度有出度)肯定直接顺着走到无出度点,所以肯定是无出度点连向无入度点. 把无入度点作为点集S ...
- P2746 [USACO5.3]校园网Network of Schools// POJ1236: Network of Schools
P2746 [USACO5.3]校园网Network of Schools// POJ1236: Network of Schools 题目描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学 ...
- poj1236 Network of Schools【强连通分量(tarjan)缩点】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4316263.html ---by 墨染之樱花 [题目链接]http://poj.org/pr ...
- POJ1236 Network of Schools (强连通)(缩点)
Network of Schools Time Limit: 1000MS ...
随机推荐
- 排查一般MySQL性能问题
排查一般MySQL性能问题,通常要记录下面几项信息: 1.mysql> show processlist; 2.mysql> show engine innodb status\G 3.表 ...
- 【35.29%】【codeforces 557C】Arthur and Table
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 关于win10输入法ctrl+shift+f和idea组合键冲突的解决办法。
先Ctrl+F,按住Ctrl,再按Shift+F. 因为win10的输入法热键无法关闭(在后期的版本中好像可以了,不过没更新),在IEDA中ctrl+shift+f组合键没法使用,可以按如下按键组合使 ...
- jquery-9 京东和酒仙网左侧导航如何实现
jquery-9 京东和酒仙网左侧导航如何实现 一.总结 一句话总结:布局的话多用定位,由底往上一层层的来布. 1.如何实现导航向div的平滑滑动? 右侧div和左侧的li一定要放在一起 127 &l ...
- centos-mirrors
http://mirrors.aliyun.com/centos/7.2.1511/os/x86_64/Packages/ http://mirrors.aliyun.com/centos/7.2.1 ...
- MySQL建立双向主备复制server配置方法
1.环境描写叙述 serverA(主) 192.85.1.175 serverB(从) 192.85.1.176 Mysql版本号:5.1.61 系统版本号:System OS:ubuntu 10.1 ...
- 工具类与工具函数 —— NextPrime
求大于某数的下一个素数: static int NextPrime (int N) { if (N % 2 == 0) ++N; int i; for (; ; N += 2){ for (i = 3 ...
- Python将被加入高考科目?你怎么看?
今天看到这样的一则新闻:不禁感叹,人工智能这股风来的太快,已经掀起全民学习Python的浪潮. 2017年中观察:看上去这个大纲内容基本是这样了,但是实行年份可能要往后推了,不在2017年执行了(据说 ...
- [Angular] Difference between Providers and ViewProviders
For example we have a component: class TodoList { private todos: Todo[] = []; add(todo: Todo) {} rem ...
- matplotlib 可视化 —— cmap(colormap)
color example code: colormaps_reference.py - Matplotlib 2.0.0 documentation 由其文档可知,在 colormap 类别上,有如 ...