题意:

求一个有向图中:

(1)要选几个点才能把的点走遍

(2)要添加多少条边使得整个图强联通

分析:

对于问题1, 我们只要求出缩点后的图有多少个入度为0的scc就好, 因为有入度的scc可以从其他地方到达。

对于问题2, 每个入度为0的scc, 都可以补一条边可以变成强连通图, 每个出度为0的scc, 也可以补一条边使其变成强连通图。 所以答案就是max(入度为0scc个数,出度为0scc个数)。

#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<map>
#include<stack>
#include<vector>
#include<algorithm>
#include<cmath>
#define mem(a) memset(a, 0, sizeof(a))
#define rep(i,a,b) for(int i = a; i < b; i++)
#define _rep(i,a,b) for(int i = a; i <= b; i++)
using namespace std;
const int maxn = ;
vector<int> G[maxn];
int n, m, Index = , scc_cnt = ;
int dfn[maxn], low[maxn], vis[maxn], scc[maxn];
int in_deg[maxn], out_deg[maxn];
stack<int> s;
void tarjan(int u){
dfn[u] = Index;
low[u] = dfn[u];
Index++; vis[u] = ;
s.push(u);
for(int i = ; i < G[u].size(); i++){
int v = G[u][i];
if(!dfn[v]){
tarjan(v);
low[u] = min(low[v], low[u]); }else if(vis[v]){
low[u] = min(low[u], dfn[v]);
}
}
if(dfn[u] == low[u]){
vis[u] = ;
scc[u] = scc_cnt;
int t;
for(;;){
int t = s.top(); s.pop();
scc[t] = scc_cnt;
vis[t] = ;
if(t == u) break;
}
scc_cnt++;
}
}
int main(){
while(~scanf("%d", &n)){
_rep(i,,n) G[i].clear();
mem(dfn), mem(low), mem(vis), mem(scc), mem(in_deg), mem(out_deg);
Index = , scc_cnt = ;
_rep(i,,n){
int v;
while(scanf("%d", &v) && v){
G[i].push_back(v);
m++;
}
} _rep(i,,n){
if(!dfn[i]) tarjan(i);
}
_rep(u,,n) rep(i,,G[u].size()){//缩点,枚举每条边, 如果不在同一个scc说明这是新图中的一条边
int v = G[u][i];
if(scc[u] != scc[v]){
out_deg[scc[u]]++, in_deg[scc[v]]++;
}
}
int _0in = , _0out = ; //入度为0的scc , 出度为0的scc
rep(i,,scc_cnt){
if(in_deg[i] == ) _0in++;
if(out_deg[i] == ) _0out++;
}
if(scc_cnt > )
printf("%d\n%d\n", _0in, max(_0in,_0out)); //
else printf("1\n0\n");//特判一下只有一个scc的情况, 那么只用选一个点
}
return ;
}

POJ 1236 Network of Schools (强连通分量缩点求度数)的更多相关文章

  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. POJ1236 Network of Schools —— 强连通分量 + 缩点 + 入出度

    题目链接:http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Tot ...

  3. POJ 1236 Network of Schools(Tarjan缩点)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16806   Accepted: 66 ...

  4. Network of Schools(强连通分量缩点(邻接表&矩阵))

    Description A number of schools are connected to a computer network. Agreements have been developed ...

  5. poj 1236 Network of Schools(tarjan+缩点)

    Network of Schools Description A number of schools are connected to a computer network. Agreements h ...

  6. Network of Schools(强连通分量+缩点) (问添加几个点最少点是所有点连接+添加最少边使图强连通)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13801   Accepted: 55 ...

  7. poj~1236 Network of Schools 强连通入门题

    一些学校连接到计算机网络.这些学校之间已经达成了协议: 每所学校都有一份分发软件的学校名单("接收学校"). 请注意,如果B在学校A的分发名单中,则A不一定出现在学校B的名单中您需 ...

  8. POJ 1236 Network of Schools(强连通 Tarjan+缩点)

    POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意:  给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...

  9. POJ 1236 Network of Schools(强连通分量)

    POJ 1236 Network of Schools 题目链接 题意:题意本质上就是,给定一个有向图,问两个问题 1.从哪几个顶点出发,能走全全部点 2.最少连几条边,使得图强连通 思路: #inc ...

随机推荐

  1. 跟我一起玩Win32开发(12):使用控件——单选按钮

    今天,咱们还是接着玩“控件斗地主”,这是我原创的超级游戏,有益身心健康,玩一朝,十年少. 哦,对,脑细胞极速运动了一下,想起了一个问题,这个破问题虽然网上有很多种解决方案,但是,并没有让所有人都解决问 ...

  2. 浅谈web前端性能优化

    前端性能优化: 一.尽可能减少前端http请求. 1.合并优化脚本文件和css文件. 2.同种类型的背景图片尽量放在一起,用css控制显示. 二.使用浏览器缓存. 如果能强制浏览器缓存在本地,将会降低 ...

  3. Python转载

    让Python的经验更多一点 Python while 1 和 while True 速度比较 Python %s和%r的区别

  4. A Dangerous Maze LightOJ - 1027

    这题意真是... 题意:你在一个迷宫里,有一些门,每个门有一个参数x,如果为正表明你进入门后可以花x的时间出去,如果为负表明你进入门后可以花-x的时间回到出发的地方.每次回到出发的地方之后,不能记得之 ...

  5. pkill 和 pgrep总结

    查看进程ID和方便kill进程 pgrep -d 指定分隔符 pgrep -d ' ' -u root 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 -u p ...

  6. Hadoop工作流--ChainMapper/ChainReducer?(三)

    不多说,直接上干货! Hadoop的ChainMapper和ChainReducer使用案例(链式处理) 什么是ChainMapper/ChainReducer?

  7. zoj3772Calculate the Function(矩阵+线段树)

    链接 表达式类似于斐波那契 但是多了一个变量 不能用快速幂来解 不过可以用线段树进行维护 对于每一个点够一个2*2的矩阵 1 a[i] 1  0   这个矩阵应该不陌生 类似于构造斐波那契的那个数列 ...

  8. ubuntu中mysql安装失败

    在ubuntu中mysql安装失败后,卸载重新安装还是安装失败,之后找了资料说是卸载的不干净,然后进行下面操作,重新安装成功. 解决办法如下: sudo rm /var/lib/mysql/ -Rsu ...

  9. match,location,history

    哇,平常写路由时基本就是简单的按照组件给的示例写,从来没有考虑为什么,又遇见了路由相关的问题,先记录一下问题,好好捋一下,哎,好香要个大佬来带带我呀,每次遇到问题要解决好久 问题: 判断是否登录之后跳 ...

  10. iOS html格式解析

    使用TFHpple解析html https://github.com/topfunky/hpple 前期准备工作 引入静态库文件 添加库文件的 header search paths(注意,必须选中 ...