POJ 1236 Network of Schools(强联通缩点)
来源:互联网 发布:js设置cookie作用域 编辑:程序博客网 时间:2024/05/16 17:36
题意:给定一些学校的网络,有向的,u可以到v,但是v不一定可以到u,按照实际情况这些学校能互相联系应该是个连通图(我猜的)。现在问最少要给多少个学校发出消息才能使得全部学校都能收到信息。还问了如果想要这些学校能互相联系,要加上多少条有向边。
解法:第一问就是裸的tarjan算法,直接缩点找一找入度为0的强连通分量即可。
第二问不是很明白,别人给出的结论就是直接取max(入度为0的强联通分量数目,出度为0的强联通分量数目)。
特判只有一个强联通分量的情况,输出1 0。
代码如下:
#include<iostream>#include<cstdio>#include<utility>#include<stack>#include<algorithm>#include<cstring>using namespace std;const int maxn = 105;int n;int dfn[maxn], low[maxn], key[maxn], in[maxn], getin[maxn], getout[maxn];int tot = 0, scc = 0;bool G[maxn][maxn], G2[maxn][maxn];stack <int> sta;void tarjan(int u) {dfn[u] = low[u] = tot++;key[u] = 1;sta.push(u);for(int v = 1; v <= n; v++) {if(!G[u][v])continue;if(dfn[v] == -1) {tarjan(v);low[u] = min(low[u], low[v]);} else if(key[v] == 1) {low[u] = min(low[u], dfn[v]);}}if(dfn[u] == low[u]) {while(!sta.empty()) {int v = sta.top();sta.pop();in[v] = scc;key[v] = 2;if(u == v)break;}scc++;}}int main() {#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endifscanf("%d", &n);for(int i = 1; i <= n; i++) {while(1) {int tmp;scanf("%d", &tmp);if(!tmp)break;G[i][tmp] = true;}}memset(dfn, -1, sizeof(dfn));tot = 0;for(int i = 1; i <= n; i++) {if(dfn[i] == -1) {tarjan(i);}}for(int i = 1; i <= n; i++) {for(int j = 1; j <= n; j++) {if(in[i] == in[j])continue;if(G[i][j]) {G2[in[i]][in[j]] = 1;getin[in[j]]++;getout[in[i]]++;}}}int ans1 = 0, ans2 = 0;for(int i = 0; i < scc; i++) {if(getin[i] == 0)ans1++;if(getout[i] == 0)ans2++;}if(scc == 1)printf("1\n0\n");elseprintf("%d\n%d\n", ans1, max(ans1, ans2));return 0;}
阅读全文
0 0
- POJ 1236 Network of Schools 强联通分量 + 缩点
- poj 1236 Network of Schools (强联通分量+缩点)
- POJ 1236 Network of Schools(强联通缩点)
- Network of Schools 缩点+强联通
- 文章标题 POJ 1236 : Network of Schools (强联通分量+缩点)
- Network of Schools+强联通分量+POJ
- POJ 1236 Network of Schools强连通分量缩点
- POJ 1236 Network of Schools (强连通分量+缩点)
- POJ 1236 Network of Schools 强连通分量+缩点
- poj 1236 Network of Schools (强连通分量+缩点)
- POJ-1236 Network of Schools 强连通+缩点
- poj 1236 Network of Schools(强连通分量+缩点)
- poj 1236 Network of Schools (强连通分支缩点)
- poj 1236 Network of Schools(强连通分量缩点)
- POJ-1236-Network of Schools【强连通分量】【缩点】
- POJ-1236 Network of Schools(强连通+缩点)
- POJ 1236 Network of Schools(强连通分量,缩点)
- poj-1236-Network of Schools-强联通分量
- 动态规划解决找零钱问题
- XML和JSON的区别
- mybatis map方式inset返回主键
- android自定义属性
- stm32 iap 跳到app后不进中断问题
- POJ 1236 Network of Schools(强联通缩点)
- jquery 循环获得数字并用逗号进行拼接,同事后台进行循环取出
- 如何正确使用光纤跳线?
- Android animation帧动画
- SFP光模块
- Servlet文件上传
- 4ipnet——完善的网络解决方案
- 打开光网时代的大门
- 易天光通信引进新测试设备啦!