[JSOI2010] 连通数 - 强连通分量,缩点
复习一下手工 tarjan
#include <bits/stdc++.h>
using namespace std;
vector <int> g[2005],scc[2005];
int ind,f[2005],siz[2005],dfn[2005],low[2005],vis[2005],s[2005],bel[2005],top,tot,n,m,t1,t2,t3;
char ch[2005];
void dfs(int p) {
vis[p]=1;
s[++top]=p;
dfn[p]=low[p]=++ind;
for(int i=0;i<g[p].size();i++) {
int q=g[p][i];
if(vis[q]==0) dfs(q), low[p]=min(low[p],low[q]);
else if(vis[q]==1) low[p]=min(low[p],dfn[q]);
}
if(dfn[p]==low[p]) {
++tot;
int q=s[top--];
while(q && q-p) {
scc[tot].push_back(q);
bel[q]=tot;
q=s[top--];
}
if(q) {
scc[tot].push_back(q);
bel[q]=tot;
--top;
}
}
vis[p]=2;
}
vector <int> G[2005];
int main() {
cin>>n;
for(int i=1;i<=n;i++) {
cin>>ch+1;
for(int j=1;j<=n;j++) {
if(ch[j]=='1') g[i].push_back(j);
}
}
for(int i=1;i<=n;i++) {
if(vis[i]==0) {
dfs(i);
}
}
for(int i=1;i<=tot;i++) {
for(int j=0;j<scc[i].size();j++) {
G[i].push_back(bel[scc[i][j]]);
}
}
for(int i=1;i<=tot;i++) {
siz[i]=scc[i].size();
for(int j=0;j<G[i].size();j++)
if(i-G[i][j]) siz[i]+=siz[G[i][j]];
}
int ans = 0;
for(int i=1;i<=tot;i++) ans+=scc[i].size()*siz[i];
cout<<ans<<endl;
}
[JSOI2010] 连通数 - 强连通分量,缩点的更多相关文章
- POJ1236Network of Schools[强连通分量|缩点]
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16571 Accepted: 65 ...
- POJ1236Network of Schools(强连通分量 + 缩点)
题目链接Network of Schools 参考斌神博客 强连通分量缩点求入度为0的个数和出度为0的分量个数 题目大意:N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后 ...
- HD2767Proving Equivalences(有向图强连通分量+缩点)
题目链接 题意:有n个节点的图,现在给出了m个边,问最小加多少边是的图是强连通的 分析:首先找到强连通分量,然后把每一个强连通分量缩成一个点,然后就得到了一个DAG.接下来,设有a个节点(每个节点对应 ...
- UVa11324 The Largest Clique(强连通分量+缩点+记忆化搜索)
题目给一张有向图G,要在其传递闭包T(G)上删除若干点,使得留下来的所有点具有单连通性,问最多能留下几个点. 其实这道题在T(G)上的连通性等同于在G上的连通性,所以考虑G就行了. 那么问题就简单了, ...
- ZOJ3795 Grouping(强连通分量+缩点+记忆化搜索)
题目给一张有向图,要把点分组,问最少要几个组使得同组内的任意两点不连通. 首先考虑找出强连通分量缩点后形成DAG,强连通分量内的点肯定各自一组,两个强连通分量的拓扑序能确定的也得各自一组. 能在同一组 ...
- POJ2553 The Bottom of a Graph(强连通分量+缩点)
题目是问,一个有向图有多少个点v满足∀w∈V:(v→w)⇒(w→v). 把图的强连通分量缩点,那么答案显然就是所有出度为0的点. 用Tarjan找强连通分量: #include<cstdio&g ...
- uva 11324 The Largest Clique(强连通分量缩点+DAG动态规划)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=25&page=sh ...
- poj 2762 Going from u to v or from v to u?(强连通分量+缩点重构图+拓扑排序)
http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: ...
- tarjan算法(强连通分量 + 强连通分量缩点 + 桥(割边) + 割点 + LCA)
这篇文章是从网络上总结各方经验 以及 自己找的一些例题的算法模板,主要是用于自己的日后的模板总结以后防失忆常看看的, 写的也是自己能看懂即可. tarjan算法的功能很强大, 可以用来求解强连通分量, ...
随机推荐
- .net List回收
转 static void Main(string[] args) { List<int> list = new List<int>(); for (int i = 0; i ...
- openlayers6实现webgl点图层渲染效果(附源码下载)
前言:openlayers6推出来的有一段时间,推出来的新特性见:https://github.com/openlayers/openlayers/releases/该版本的主要功能是能够组合具有不同 ...
- c# 异步编程 使用回调函数例子
环境VS2010, 在项目属性中输出类型选择控制台应用程序 运行结果 using System;using System.Collections.Generic;using System.Compon ...
- 笔记-Git基础
git配置 git config --global user.name "xxx" //配置用户名 git config --global user.email "xxx ...
- 【笔记】机器学习 - 李宏毅 - 9 - Keras Demo
3.1 configuration 3.2 寻找最优网络参数 代码示例: # 1.Step 1 model = Sequential() model.add(Dense(input_dim=28*28 ...
- Python学习笔记--协程asyncio
协程的主要功能是单线程并发运行 假设有3个耗时不一样的任务.看看协程的效果. 先来看没有使用协程情况: #!/usr/bin/python3 # -*- coding:utf-8 -*- import ...
- PHP0006:PHP基础--函数2
如果php后面没有任何html代码就可以 删掉后面的 ?> 符号 file_put_content 是覆盖写入文件信息
- memcached的安装、常用命令以及在实际开发中的案例
Memcached注意缺乏安全认证以及安全管制需要将Memcached服务器放置在防火墙(iptables)之后 Linux平台 (CentOS)安装Memcached 安装依赖yum -y inst ...
- [PAT] A1018 Public Bike Management
[思路] 题目生词 figure n. 数字 v. 认为,认定:计算:是……重要部分 The stations are represented by vertices and the roads co ...
- 使用微信扫一扫时获取sign签名
private $appId = '你的APPID'; private $appSecret = '你的APPsecret'; /** * 获取签名信息 * @return array */ publ ...