POJ2186 强连通分量+缩点
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 40234 | Accepted: 16388 |
Description
popular, even if this is not explicitly specified by an ordered pair in the input. Your task is to compute the number of cows that are considered popular by every other cow.
Input
* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular.
Output
Sample Input
3 3
1 2
2 1
2 3
Sample Output
1
Hint
Source
题意:强连通分量缩点图求出度为0的点。
思路:首先图要连通,其次出度为零的强连通分量个数只能为1.
代码:
#include"bits/stdc++.h" #define db double
#define ll long long
#define vl vector<ll>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define rep(i, a, n) for (int i=a;i<n;i++)
#define per(i, a, n) for (int i=n-1;i>=a;i--)
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
const int N = 1e6 + ;
const int mod = 1e9 + ;
const int MOD = ;
const db PI = acos(-1.0);
const db eps = 1e-;
const ll INF = 0x3fffffffffffffff;
int n, m;
int cnt, num, id;
int head[N];
bool ins[N];
int out[N];
int dfn[N], low[N];
int beg[N];
stack<int> s;
struct P {int to, nxt;} e[N]; void add(int u, int v) {
e[cnt].to = v;
e[cnt].nxt = head[u];
head[u] = cnt++;
} void tarjan(int u) {
low[u] = dfn[u] = ++id;
ins[u] = ;
s.push(u);
for (int i = head[u]; ~i; i = e[i].nxt) {
int v = e[i].to;
if (!dfn[v]) tarjan(v), low[u] = min(low[u], low[v]);
else if (ins[v]) low[u] = min(low[u], dfn[v]);
}
if (low[u] == dfn[u]) {
int v;
do {
v = s.top();
s.pop();
ins[v] = ;
beg[v] = num;//缩点
} while (u != v);
num++;
}
} int fa[N];
bool vis[N]; int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
void unio(int x, int y) {
int xx = find(x), yy = find(y);
if (xx != yy) fa[xx] = yy;
}
void init() {
memset(head, -, sizeof(head));
memset(low, , sizeof(low));
memset(dfn, , sizeof(dfn));
memset(ins, , sizeof(ins));
memset(out, , sizeof(out));
memset(beg, , sizeof(beg));
memset(vis,, sizeof(vis));
for (int i = ; i <= n; i++) fa[i] = i;
cnt = num = id = ;
}
int main() {
while (scanf("%d%d", &n, &m) == ) {
init();
for (int i = ; i < m; i++) {
int x, y;
ci(x), ci(y);
add(x, y);
unio(x, y);
}
for (int i = ; i <= n; i++) if (!dfn[i]) tarjan(i);
for (int i = ; i <= n; i++) {
for (int j = head[i]; ~j; j = e[j].nxt) {
int v = e[j].to;
if (beg[i] != beg[v]) out[beg[i]]++;
}
}
int ok = ;
int x = find();
for (int i = ; i <= n; i++)//联通
if (find(i) != x) {
ok = ;
break;
}
int tmp = , cnt = ;
for (int i = ; i <=n; i++) {//强连通分量个数
if (!out[beg[i]]){
if(!vis[beg[i]]) vis[beg[i]]=,cnt++;
tmp++;
}
}
if (cnt==&&ok==) pi(tmp);
else puts("");
}
return ;
}
POJ2186 强连通分量+缩点的更多相关文章
- POJ2186 (强连通分量缩点后出度为0的分量内点个数)
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 27820 Accepted: 11208 De ...
- 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: ...
随机推荐
- html-表单的设计
一.表单的设计 1.注册表单页面 <html> <head> <title>表单的练习</title> <script> function ...
- 对 Canal (增量数据订阅与消费)的理解
概述 canal是阿里巴巴旗下的一款开源项目,纯Java开发.基于数据库增量日志解析,提供增量数据订阅&消费,目前主要支持了MySQL(也支持mariaDB). 起源:早期,阿里巴巴B2B公司 ...
- tensorflow报错 Key Conv/biases not found in checkpoint
可能的解决方法: 删除训练文件夹中的旧模型
- vue-router异步加载组件
export default { routes: [ { path: '/fund', name: 'FundManagement', component: function(resolve) { r ...
- Arcgis for Android 空间数据WKT与JSON描述
点线面数据标准格式 一. 点 WKT: POINT(-118.4 -45.2) JSON: { "x": -118.4, "y": -45.2, "s ...
- 线段树扫描线总结(POJ 1389)
扫描线算是线段树的一个比较特殊的用法,虽然NOIP不一定会考,但是学学还是有用的,况且也不是很难理解. 以前学过一点,不是很透,今天算是搞懂了. 就以这道题为例吧:嘟嘟嘟 题目的意思是在一个二维坐标系 ...
- 【luogu P4231 三步必杀】 题解
题目链接:https://www.luogu.org/problemnew/show/P4231 诶 我很迷啊..这跟树状数组有什么关系啊...拿二阶差分数组过了..? #include <cs ...
- 【题解】洛谷P3166 [CQOI2014] 数三角形(组合+枚举)
洛谷P3166:https://www.luogu.org/problemnew/show/P3166 思路 用组合数求出所有的3个点组合(包含不合法的) 把横竖的3个点共线的去掉 把斜的3个点共线的 ...
- Dependency Injection in ASP.NET Web API 2 (在web api2 中使用依赖注入)
原文:http://www.asp.net/web-api/overview/advanced/dependency-injection 1 什么是依赖注入(Dependency Injection) ...
- java基础(杂记)
java基础夯实(杂记):1:创建实例对象可以通过无参的构造函数然后调用成员变量去初始化属性,也可以自己定义有参构造方法直接初始化属性,当属性为private时我们可以通过getset方法间接访问:2 ...