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: ...
随机推荐
- Flask入门模板过滤器与测试器(五)
1 模板引擎之过滤器 概念 : 过滤器本质上是个转换函数,第一个参数是待过滤的变量.如果它有第二个参数,模板中就必须传进去. 过滤器使用管道符| 放在{{ }} Jinja2模板引擎提供了丰富的内置过 ...
- C#发送电子邮件 (异步) z
///验证电子邮件的正则表达式 string emailStr = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([ ...
- sql相同记录取时间最大的信息
- php提示undefined index的几种解决方法
平时用$_post[''],$_get['']获取表单中参数时会出现Notice: Undefined index: -------- 我们经常接收表单POST过来的数据时报Undefined ind ...
- CentOS 7.3 下 Mysql(mariadb)的安装
LNMP的安装中 Nginx的安装很简单,我一般去Nginx官方网站上下载对应版本的rpm包后,上传到终端rpm安装.再此不多赘述. 但是在CentOS7中安装最新的mysql(mariadb)却经常 ...
- pip 安装下载好的tensorflow
pip --default-timeout=100 install C:\Users\Administrator\Downloads\tensorflow-1.12.0-cp37-cp37m-win_ ...
- [SCOI2014]方伯伯运椰子
嘟嘟嘟 01分数规划思维题. 题中要求交通总量不减少,那么如果总量增加的话,总费用就会增加,所以一定不是更优的解.那么总量守恒. 这是不是就想到了网络流?对于每一个节点流入量等于流出量.然后就是很有思 ...
- [18/11/11] java标识符及变量
一.标识符规范 1.必须以字母.下划线 .美元符号开头. 即数字不能作为开头,其它位随便 2.不可以是java关键字(即保留字), 如static .class.new 等 . 注:int 年 ...
- pinyin4j工具类
<!-- 导入pinyin4j --> <dependency> <groupId>com.belerweb</groupId> <artifac ...
- Xshell的使用
1. Xshell 连接 xshell 新建会话,主机地址,填下图中 inner addr 里的地址 然后输入用户名,勾选记住用户名,确定 输入密码 密码输入正确后即可连接成功 ...