POJ 2186.Popular Cows (强连通)
强连通缩点,统计入度为1的缩点后的点的个数
个数1的话输出这个强连通分量的点的数量
否则输出0;
code
/*
Kosaraju算法,无向图的强连通分量,时间复杂度O(n+m)
思路:
按照图G的深度遍历序列,在G的反图上进行深搜
能够搜到的点集就是一个强联通分量
*/ #include <iostream>
#include <cstring>
using namespace std;
const int INF = 10009;
//链接表,偶数边为原图,奇数边为反图
struct node {
int v, ne;
} edge[100009];
/*
scc是强连通子图的个数
dfn为深度遍历序列(逆序即反图的拓扑排序)
vis为访问标记,sum记录每个强连通分量的节点数
*/
int head[INF], dfn[INF], vis[INF], sum[INF], n, m, scc, cnt = 1, tol;
void adde (int u, int v) {
edge[++cnt].v = v;
edge[cnt].ne = head[u];
head[u] = cnt;
}
void dfs (int k) {
vis[k] = 1;
for (int i = head[k]; i != 0; i = edge[i].ne)
if ( (i & 1) == 0 && !vis[edge[i].v])
dfs (edge[i].v);
dfn[++tol] = k;
}
void ndfs (int k) {
vis[k] = scc, sum[scc]++;
for (int i = head[k]; i != 0; i = edge[i].ne)
if ( (i & 1) && !vis[edge[i].v])
ndfs (edge[i].v);
}
void Kosaraju() {
for (int i = 1; i <= n; i++)
if (!vis[i]) dfs (i);
memset (vis, 0, sizeof vis);
for (int i = n; i > 0; i--)
if (!vis[dfn[i]]) scc++, ndfs (dfn[i]);
}
int make() {
int deg[INF] = {0};
//由反图统计每个强联通点的有无出度
for (int i = 3; i <= cnt; i += 2) {
if (vis[edge[i].v] == vis[edge[i ^ 1].v]) continue;
deg[vis[edge[i].v]]++;
}
int j, t = 0;
for (int i = 1; i <= scc; i++)
if (deg[i] == 0) j = i, t++;
if (t == 1) return sum[j];
return 0;
}
int main() {
int x, y;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> x >> y;
adde (x, y), adde (y, x);
}
Kosaraju();
cout << make();
return 0;
}
POJ 2186.Popular Cows (强连通)的更多相关文章
- poj 2186 Popular Cows (强连通分量+缩点)
http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
- POJ 2186 Popular Cows(强连通分量缩点)
题目链接:http://poj.org/problem?id=2186 题目意思大概是:给定N(N<=10000)个点和M(M<=50000)条有向边,求有多少个“受欢迎的点”.所谓的“受 ...
- POJ 2186 Popular Cows --强连通分量
题意:给定一个有向图,问有多少个点由任意顶点出发都能达到. 分析:首先,在一个有向无环图中,能被所有点达到点,出度一定是0. 先求出所有的强连通分支,然后把每个强连通分支收缩成一个点,重新建图,这样, ...
- POJ 2186 Popular Cows 强连通分量模板
题意 强连通分量,找独立的块 强连通分量裸题 #include <cstdio> #include <cstdlib> #include <cstring> #in ...
- 强连通分量分解 Kosaraju算法 (poj 2186 Popular Cows)
poj 2186 Popular Cows 题意: 有N头牛, 给出M对关系, 如(1,2)代表1欢迎2, 关系是单向的且能够传递, 即1欢迎2不代表2欢迎1, 可是假设2也欢迎3那么1也欢迎3. 求 ...
- poj 2186 Popular Cows 【强连通分量Tarjan算法 + 树问题】
题目地址:http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Sub ...
- POJ 2186 Popular Cows (强联通)
id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 655 ...
- tarjan缩点练习 洛谷P3387 【模板】缩点+poj 2186 Popular Cows
缩点练习 洛谷 P3387 [模板]缩点 缩点 解题思路: 都说是模板了...先缩点把有环图转换成DAG 然后拓扑排序即可 #include <bits/stdc++.h> using n ...
- [强连通分量] POJ 2186 Popular Cows
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31815 Accepted: 12927 De ...
- POJ 2186 Popular Cows(强连通)
Popular Cows Time Limit: 2000MS Memo ...
随机推荐
- jQuery EasyUI + struts2.3 + mongoDB 列表查询翻页JAVA样例
列表界面: 主要实现方式:前台组合json格式查询条件,提交至后台解析处理 一.前台搜索脚本 String.prototype.replaceAll = function (s1, s2) { ...
- winphone 开发学习笔记(2)
导航 NavigationService.Navigate(new Uri("xxxx.xaml",UriKind.Relative)) xxx表示要跳转的目标页面 页面和页面导航 ...
- MySQL安装没有弹出配置向导
安装MySQL过程中一切都正常只是没有弹出MySQL配置向导对话框,即出现"launch the MySQL Instance Configuration Wizard" fini ...
- Unity3d shader之次表面散射(Subsurface Scattering)
次表面散射是一种非常常用的效果,可以用在很多材质上如皮肤,牛奶,奶油奶酪,番茄酱,土豆等等 初衷是想做一个牛奶shader的,但后来就干脆研究了sss这是在vray上的次表面散射效果 这是本文在un ...
- Mac 系统显示和隐藏文件的方法
1. 代码法: 显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool true 隐藏Mac隐藏文件的命令:defau ...
- GPG error: the public key is not available
GPG error: The following signatures couldn't be verified because the public key is not available I h ...
- 【PHP】php+txt实现网页计数器(限IP统计方式和不限IP统计方式)
一般的网页计数器制作实现思路:首先设定存放统计数据的文件(counter.txt)——读取文件中的内容存入字符串——自加操作——以写入方式打开文件写入数据——从文件中输出统计数据——关闭文件. 代码: ...
- 定制Centos系统(基于6.x)
1.条件准备: 按照需求,最小化安装Centos原生系统 在安装后的系统中找到/root/install.log与/root/anaconda-ks.cfg文件 ...
- 关于开发环境 git 重新部署
apps 开发机器 多次因为升级出现无法登陆 下面就重新部署 流程做笔记 1 备份 根目录下的 那一堆shell 和 Cache/data 下的系统配置 2 shell : su www ...
- [PWA] sw-precache
Link to CodeLab In this codelab, we'll retrace those steps but this time we'll use a tool called sw- ...