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: ...
随机推荐
- 1.windows下GIT 服务安装
本章介绍简单在windows 安装git 服务方法.服务器端采用的是Bonobo Git Server,一款用ASP.NET MVC开发的Git源代码管理工具,界面简洁,基于Web方式配置,简单易用. ...
- oozie 完整流程实例
Oozie概述: Oozie是一个基于Hadoop工作流引擎,也可以称为调度器,它以xml的形式写调度流程,可以调度mr,pig,hive,shell,jar,spark等等.在实际工作中,遇到对数据 ...
- 管道(Pipelines)模型
Pipeline模型最早被使用在Unix操作系统中.据称,假设说Unix是计算机文明中最伟大的发明,那么,Unix下的Pipe管道就是尾随Unix所带来的还有一个伟大的发明[1].我觉得管道的出现,所 ...
- python:部分内置函数与匿名函数
一.内置函数 1,数据类型:int,bool .......... 2,数据结构:dict,list,tuple,set,str 3,reversed--保留原列表,返回一个反序的迭代器 revers ...
- html题型
1.doctype的意义是什么 这个是有历史背景的,在很久以前,IE有一些自己的渲染模式,最典型的就是盒子模型,包括边距.这就造成了不兼容模式,所以他的意义 1)让浏览器以标准模式渲染 2)让浏览器知 ...
- 行云管家 V4.7产品新特性-国际化版本、支持Oracle的数据库审计、主机密码自动修改策略 发布日期:2018-11-22
行云管家在线体验: 行云管家[官网]-领先的云计算管理平台-云安全,堡垒机,自动化运维 行云管家新手有礼活动: 行云管家新手有礼,新用户1元即可体验专业版-优惠券 发布日期:2018-11-22 ...
- 【luogu P2234 [HNOI2002]营业额统计】 题解
题目链接:https://www.luogu.org/problemnew/show/P2234 本来是一道打算练习splay的题目 发现暴力可以过啊.. #include <iostream& ...
- 学大伟业 Day 4 培训总结
今天讲的全是dp... 不多废话,先看一道经典的模板LIS(最长不下降子序列) 一.LIS 给定一个长度为N的数列,求最长上升子序列 例:1 7 2 8 3 4 答案:1 2 3 4 代码: #inc ...
- Entity Framework 四
实体框架支持三种类型的查询:1)LINQ to Entities,2)Entity SQL,3)Native SQL LINQ方法语法: LINQ查询语法: 实体SQL: 这种可以简单的了解,不必深入 ...
- o'Reill的SVG精髓(第二版)学习笔记——第十章
10.1 裁剪路径 创建SVG文档时,可以通过制定感兴趣区域的宽度和高度建立视口.这会变成默认的裁剪区域,任何绘制在该范围外部的部分都不会显示.你也可以使用<clipPath>元素来建立自 ...