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: ...
随机推荐
- springMVC通过ajax传参到后台
JSON对象和JSON字符串 在SpringMVC环境中,@RequestBody接收的是一个Json对象的字符串,而不是一个Json对象.然而在ajax请求往往传的都是Json对象,用 JSON.s ...
- Facet constraits error: Spring 4.1 requires Java 1.6 or newer.
问题来源: 在高版本的myeclipse,同步低版本的myeclipse提交的项目,可能会出现配置不一致. 问题描述: spring4.1不支持jdk1.6 注:在下载项目到本地的时候,myeclip ...
- Java传引用问题
Java传引用问题 使用Java调用方法时,可以传值,也可以传引用.下面说说两者的区别: 1.传值 传值中的"值"类型是指java的8大基本类型(基础知识,不知道 ...
- 日期的压缩存储daybits
问题: 存储一个日期的序列,例如保存用户一年的登录时间序列,20140201,20130102这样两个日期,如果用INT那么需要八个字节,用STRING就更多了. 解决: 通过bit来存储一天,具体的 ...
- POJ-3273 Monthly Expense---最小化最大值
题目链接: https://cn.vjudge.net/problem/POJ-3273 题目大意: 给N个数,划分为M个块(不得打乱数顺序).找到一个最好的划分方式,使得块的和的最大值 最小 解题思 ...
- 面向对象编程(OOP)、面向组件编程(COP)、面向方面编程(AOP)和面向服务编程(SOP)
http://blog.csdn.net/hjf19790118/article/details/6919265 1.什么是面向对象编程(Object-Oriented Programming)? 面 ...
- python:类的基本特征------继承、多态与封装
一.继承 1,什么是继承 继承是一种创建新类的方式,在python中,新建的类可以继承一个或多个父类,父类又可称为基类或超类,新建的类称为派生类或子类 python中类的继承分为:单继承和多继承 cl ...
- Github 上一些关于PHP的开源项目
Github 上一些关于PHP的开源项目,总有你喜欢的那一款 Awesome PHP Package Management Package Management Related Frameworks ...
- python+requests实现接口测试 - get与post请求使用
简介:Requests 是用Python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库.它比 urllib 更加方便,可以节约我们大量的工作,完全满足 ...
- Maven Dependencies missing jar 解决
在导入SVN项目之后发现Maven里面的pom.xml报错. 发现是Maven Dependencies 里面的jar包不完整. 我试过手动加入jar,但是不能成功,然后就又试着添加dependecn ...