【poj2186】 Popular Cows
http://poj.org/problem?id=2186 (题目链接)
题意
给出一个n个点m条边的有向图,求其中没有出度强连通分量所包含的点有几个
Solution
其实这道题的题解已经在“题意”中给出了= =,先Tarjan跑出强连通分量,之后模拟给缩点后的图连边(其实并不用真的连边),来统计缩点后每个节点的出度。输出出度为0的强连通分量所包含的点即可,若有多个强连通分量出度为0,输出0(不要问我为什么,有时候事情就是这么不讲道理)。
代码
// poj2186
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<set>
#define MOD 1000000007
#define inf 2147483640
#define LL long long
#define free(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout);
using namespace std;
inline LL getint() {
LL x=0,f=1;char ch=getchar();
while (ch>'9' || ch<'0') {if (ch=='-') f=-1;ch=getchar();}
while (ch>='0' && ch<='9') {x=x*10+ch-'0';ch=getchar();}
return x*f;
} const int maxn=50010;
struct edge {int to,next;}e[maxn<<2];
int f[maxn],dfn[maxn],low[maxn],head[maxn],s[maxn],pos[maxn],cnts[maxn];
int ind,cnt,n,m,top,tot; void insert(int u,int v) {
e[++cnt].to=v;e[cnt].next=head[u];head[u]=cnt;
}
void Tarjan(int u) {
dfn[u]=low[u]=++ind;
s[++top]=u;
f[u]=1;
for (int i=head[u];i;i=e[i].next) {
if (!dfn[e[i].to]) {
Tarjan(e[i].to);
low[u]=min(low[u],low[e[i].to]);
}
else if (f[e[i].to]) low[u]=min(low[u],dfn[e[i].to]);
}
if (dfn[u]==low[u]) {
tot++;int j;
do {
j=s[top--];
pos[j]=tot;
cnts[tot]++;
f[j]=0;
}while (j!=u);
}
}
int main() {
while (scanf("%d%d",&n,&m)!=EOF) {
top=0;cnt=0;ind=0;tot=0;
for (int i=1;i<=n;i++) dfn[i]=low[i]=head[i]=cnts[i]=pos[i]=0;
for (int i=1;i<=m;i++) {
int x,y;
scanf("%d%d",&x,&y);
insert(x,y);
}
for (int i=1;i<=n;i++) if (!dfn[i]) Tarjan(i);
cnt=0;for (int i=1;i<=n;i++) f[i]=0;
for (int i=1;i<=n;i++)
for (int j=head[i];j;j=e[j].next)
if (pos[i]!=pos[e[j].to]) f[pos[i]]++;
int ans=0;
for (int i=1;i<=tot;i++) if (!f[i]) ans++;
if (ans>1) printf("0\n");
else {
ans=0;
for (int i=1;i<=tot;i++) if (!f[i]) ans+=cnts[i];
printf("%d\n",ans);
}
}
return 0;
}
【poj2186】 Popular Cows的更多相关文章
- 【2186】Popular Cows(强连通分支及其缩点)
id=2186">[2186]Popular Cows(强联通分支及其缩点) Popular Cows Time Limit: 2000MS Memory Limit: 65536 ...
- 【图论】Popular Cows
[POJ2186]Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 34752 Accepted: ...
- 【POJ3621】Sightseeing Cows 分数规划
[POJ3621]Sightseeing Cows 题意:在给定的一个图上寻找一个环路,使得总欢乐值(经过的点权值之和)/ 总时间(经过的边权值之和)最大. 题解:显然是分数规划,二分答案ans,将每 ...
- 【POJ2182】Lost Cows
[POJ2182]Lost Cows 题面 vjudge 题解 从后往前做 每扫到一个点\(i\)以及比前面小的有\(a[i]\)个数 就是查询当前的第\(a[i]+1\)小 然后查询完将这个数删掉 ...
- 【POJ2186】受牛仰慕的牛
受牛仰慕的牛(popular cows) 每头牛都有一个梦想:成为一个群体中最受欢迎的名牛!在一个有N(1<=N<=10,000)头牛的牛群中,给你M(1<=M<=50,00 ...
- 【POJ 2186】Popular Cows
http://poj.org/problem?id=2186 tarjan求强连通分量. 因为SD省选用WinXP+Cena评测而且不开栈,所以dfs只好写手动栈了. 写手动栈时思路清晰一点应该是不会 ...
- 【POJ3621】Sightseeing Cows
Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8331 Accepted: 2791 ...
- 【USACO】Milking Cows
Three farmers rise at 5 am each morning and head for the barn to milk three cows. The first farmer b ...
- 【USACO】Strolling Cows
Strolling Cows 给定有 \(n\) 个点 \(n\) 条边的有向图,每个点的出度都为 \(1\),求图中的最大环. 显然入度为 \(0\) 的点不可能为最大环上的点,所以考虑删点. 然后 ...
随机推荐
- [转] 国外程序员整理的 C++ 资源大全
关于 C++ 框架.库和资源的一些汇总列表,由 fffaraz 发起和维护. 内容包括:标准库.Web应用框架.人工智能.数据库.图片处理.机器学习.日志.代码分析等. 标准库 C++标准库,包括了S ...
- maya获取邻接顶点的一个问题
maya网格数据结构允许"非流形"的存在,于是,这种数据结构无法按顺序给出一个点的邻接顶点. 于是,MItMeshVertex::getConnectedVertices函数返回的 ...
- FusionCharts或其它flash的div图层总是浮在最上层? (转)
div的图层由div的style中的z-index来决定,z-index是层垂直屏幕的坐标,0最小,越大的话位置越靠上. 由于FusionCharts的图表都放在div中,如果页面还有其他的div,将 ...
- /etc/profile和~/.bash_profile的区别
/etc/profile是全局的,是私有的 /etc/profile用于整个系统所有用户, ~/.bash_profile, ~/.profile和~/.bashrc 用于各个用户,这里的" ...
- GIT在Linux上的安装和使用简介
GIT最初是由Linus Benedict Torvalds为了更有效地管理Linux内核开发而创立的分布式版本控制软件,与常用的版本控制工具如CVS.Subversion不同,它不必服务器端软件支持 ...
- Managing the Lifecycle of a Service
service的生命周期,从它被创建开始,到它被销毁为止,可以有两条不同的路径: A started service 被开启的service通过其他组件调用 startService()被创建. 这种 ...
- C#获取枚举描述代码
public class MusterEnum { /// 获取枚举的描述信息 /// </summary> /// <param name="e">传入枚 ...
- 使用JQuery能做什么(zz)
jQuery库为Web脚本编程提供了通用(跨浏览器)的抽象层,使得它几乎适用于任何脚本编程的情形.jQuery通常能为我们提供以下功能: 1.方便快捷获取DOM元素 如果使用纯JavaScript的方 ...
- [CareerCup] 2.3 Delete Node in a Linked List 删除链表的节点
2.3 Implement an algorithm to delete a node in the middle of a singly linked list, given only access ...
- LeetCode-Count Univalue Subtrees
Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...