POJ 1236 Network of Schools (强连通分量缩点求度数)
题意:
求一个有向图中:
(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 (强连通分量缩点求度数)的更多相关文章
- POJ 1236 Network Of Schools (强连通分量缩点求出度为0的和入度为0的分量个数)
Network of Schools A number of schools are connected to a computer network. Agreements have been dev ...
- POJ1236 Network of Schools —— 强连通分量 + 缩点 + 入出度
题目链接:http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Tot ...
- POJ 1236 Network of Schools(Tarjan缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16806 Accepted: 66 ...
- Network of Schools(强连通分量缩点(邻接表&矩阵))
Description A number of schools are connected to a computer network. Agreements have been developed ...
- poj 1236 Network of Schools(tarjan+缩点)
Network of Schools Description A number of schools are connected to a computer network. Agreements h ...
- Network of Schools(强连通分量+缩点) (问添加几个点最少点是所有点连接+添加最少边使图强连通)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13801 Accepted: 55 ...
- poj~1236 Network of Schools 强连通入门题
一些学校连接到计算机网络.这些学校之间已经达成了协议: 每所学校都有一份分发软件的学校名单("接收学校"). 请注意,如果B在学校A的分发名单中,则A不一定出现在学校B的名单中您需 ...
- POJ 1236 Network of Schools(强连通 Tarjan+缩点)
POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意: 给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...
- POJ 1236 Network of Schools(强连通分量)
POJ 1236 Network of Schools 题目链接 题意:题意本质上就是,给定一个有向图,问两个问题 1.从哪几个顶点出发,能走全全部点 2.最少连几条边,使得图强连通 思路: #inc ...
随机推荐
- 51Nod 1116 K进制下的大数(暴力枚举)
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> us ...
- PostgreSQL - 修改默认端口号
升级PostgreSQL遇到的问题 之前将PostgreSQL从9.5升级到了10.3版本,安装时将端口设置成了5433,(默认是5432),后来发现在使用psql来restore db会发生语法错误 ...
- 506 Relative Ranks 相对名次
给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予 “金牌”,“银牌” 和“ 铜牌”("Gold Medal", "Silve ...
- ML——决策树模型
决策树模型 优点:高效简单.易于理解,可以处理不相关特征. 缺点:容易过拟合,训练集在特征上是完备的 决策树过程:特征选择.划分数据集.构建决策树.决策树剪枝 决策树选择最优的划分特征,将数据集按照最 ...
- Web API 实体显示注释
我看园子里关于Web API的注释都是关于方法的,并没有显示实体注释的方法,今天花了一些时间搞了一下 其实注释的显示就是根据类库的XML文档文件生成的. 首先你要将所用到的类库生成XML文档文件: 在 ...
- hash 【模板】
hash 功能: hash一般用于快速判断两个或多个字符串是否匹配. 实现 : 想一想,如果比较两个数子的话是很方便的很快,那么我们把整个字符串看成一个大数. 它是base进制的len位数.但 ...
- 在input标签里只能输入数字
<input type='text' onkeyup="(this.v=function(){this.value=this.value.replace(/[^0-9-]+/,''); ...
- AJPFX谈Java 性能优化之基本类型 vs 引用类型
★名词定义 先明确一下什么是“基本类型”,什么是“引用类型”. 简单地说,所谓基本类型就是 Java 语言中如下的8种内置类型: booleancharbyteshortintlongfloatdou ...
- [ CQOI 2018 ] 异或序列
\(\\\) Description 给出一个长为 \(n\) 的数列 \(A\) 和 \(k\),多次询问: 对于一个区间 \([L_i,R_i]\),问区间内有多少个不为空的子段异或和为 \(k\ ...
- k-window的关闭与打开设置
// 打开弹框窗口 public showKwinDow() { const that = this as any; // 设置窗口居中 that.$refs['setAddEdit'].widget ...