题意:

求一个有向图中:

(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. HDU 6183 Color it(动态开点线段树)

    题目原网址:http://acm.hdu.edu.cn/showproblem.php?pid=6183 题目中文翻译: Time Limit: 20000/10000 MS (Java/Others ...

  2. linux虚拟机时间不准的问题

    如果时区不准, 使用tzselect命令(timezone选择),选择北京时间.然后把输出的命令写入/etc/profile.d/time.sh里. 然后用crontab写定时任务,每天执行一次. 3 ...

  3. bzoj 5019 [Snoi2017]遗失的答案

    题面 https://www.lydsy.com/JudgeOnline/problem.php?id=5019 题解 如果L不是G的倍数 答案为0 下面考虑G|L的情况 将G,L质因数分解 设$L= ...

  4. nth Permutation LightOJ - 1060

    nth Permutation LightOJ - 1060 题意:给定一个小写字母组成的字符串,对其中所有字母进行排列(排列组合的排列),将所有生成的排列按字典序排序,求排序后第n个排列. 方法:按 ...

  5. SpringMVC配置文件-web.xml的配置

    SpringMVC配置文件(重点) @Web.xml @核心拦截器(必配) <!-- spring 核心转发器,拦截指定目录下的请求,分配到配置的拦截路径下处理 --> <servl ...

  6. toLocaleSting()

    之前一直忽略了这一方法,直到前天的笔试题,两种方式实现如下功能... 1234567890→1,234,567,890 当时我的思路是这样的:1.字符串反转,插入逗号,再反转 2.求余数,将字符串一分 ...

  7. CentOS 6.9 --Squid代理服务器

    主机名 IP地址  网关   DNS   服务类型  Master eth0:192.168.17.130(VMnet4) eth1:192.168.30.130(NAT) 192.168.30.2 ...

  8. C#实现较为实用的SQLhelper

    第一次写博客,想不到写什么好b( ̄▽ ̄)d ,考虑的半天决定从sqlhelper开始,sqlhelper对程序员来说就像helloworld一样,很简单却又很重要,helloworld代表着程序员萌新 ...

  9. Java基础50题test4—分解质因数

    [分解质因数] 题目:将一个正整数分解质因数.例如:输入 90,打印出 90=2*3*3*5. 程序分析:对 n 进行分解质因数,应先找到一个最小的质数 k,然后按下述步骤完成: (1)如果这个质数恰 ...

  10. ios 设置head请求头,自定义head, read response header

    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager]; manger.securityPolicy = [AFSecurityPo ...