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的更多相关文章

  1. 【2186】Popular Cows(强连通分支及其缩点)

    id=2186">[2186]Popular Cows(强联通分支及其缩点) Popular Cows Time Limit: 2000MS   Memory Limit: 65536 ...

  2. 【图论】Popular Cows

    [POJ2186]Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 34752   Accepted: ...

  3. 【POJ3621】Sightseeing Cows 分数规划

    [POJ3621]Sightseeing Cows 题意:在给定的一个图上寻找一个环路,使得总欢乐值(经过的点权值之和)/ 总时间(经过的边权值之和)最大. 题解:显然是分数规划,二分答案ans,将每 ...

  4. 【POJ2182】Lost Cows

    [POJ2182]Lost Cows 题面 vjudge 题解 从后往前做 每扫到一个点\(i\)以及比前面小的有\(a[i]\)个数 就是查询当前的第\(a[i]+1\)小 然后查询完将这个数删掉 ...

  5. 【POJ2186】受牛仰慕的牛

    受牛仰慕的牛(popular cows)  每头牛都有一个梦想:成为一个群体中最受欢迎的名牛!在一个有N(1<=N<=10,000)头牛的牛群中,给你M(1<=M<=50,00 ...

  6. 【POJ 2186】Popular Cows

    http://poj.org/problem?id=2186 tarjan求强连通分量. 因为SD省选用WinXP+Cena评测而且不开栈,所以dfs只好写手动栈了. 写手动栈时思路清晰一点应该是不会 ...

  7. 【POJ3621】Sightseeing Cows

    Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8331   Accepted: 2791 ...

  8. 【USACO】Milking Cows

    Three farmers rise at 5 am each morning and head for the barn to milk three cows. The first farmer b ...

  9. 【USACO】Strolling Cows

    Strolling Cows 给定有 \(n\) 个点 \(n\) 条边的有向图,每个点的出度都为 \(1\),求图中的最大环. 显然入度为 \(0\) 的点不可能为最大环上的点,所以考虑删点. 然后 ...

随机推荐

  1. Hill密码

    希尔密码(Hill Password)是运用基本矩阵论原理的替换密码,由Lester S. Hill在1929年发明.每个字母当作26进制数字:A=, B=, C=... 一串字母当成n维向量,跟一个 ...

  2. jquery给元素添加样式表的方法

    //1.获取和设置样式 $("#tow").attr("class")获取ID为tow的class属性 $("#two").attr(&qu ...

  3. Cursor的各种效果

    总结之后的Cursor的各种效果: http://sandbox.runjs.cn/show/bbwoyn0c http://css-cursor.techstream.org/ 源代码如下: < ...

  4. ZooKeeper学习第二期--ZooKeeper安装配置

    一.Zookeeper的搭建方式 Zookeeper安装方式有三种,单机模式和集群模式以及伪集群模式. ■ 单机模式:Zookeeper只运行在一台服务器上,适合测试环境:■ 伪集群模式:就是在一台物 ...

  5. 【原创】Junit4详解一:Junit总体介绍

    Junit是一个可编写重复测试的简单框架,是基于Xunit架构的单元测试框架的实例.Junit4最大的改进是大量使用注解(元数据),很多实际执行过程都在Junit的后台做完了,而且写test case ...

  6. WCF与ASMX Web服务差异比较[译]

    First of all, it needs to understand that WCF Service provides all the capabilities of .NET web serv ...

  7. canvas学习笔记:小小滴公式,大大滴乐趣

    声明:本文为原创文章,如需转载,请注明来源WAxes,谢谢! 最近想弄一个网页,把自己学HTML5过程中做的部分DEMO放上去做集合,但是,如果就仅仅做个网页把所有DEMO一个一个排列又觉得太难看了. ...

  8. 你的C#代码是怎么跑起来的(一)

    写了那么多C#代码,大家有没有想过自己写的代码编译后的可执行文件内部是什么样子,是怎样在系统上运行的? 编译成exe,然后双击exe文件运行,这中间到底发生了些什么呢,这篇先来剖析下exe内部的样子: ...

  9. 如何自学 Android 编程?

    最近知乎上有网友问我怎么自学Android,其实说实在的,我学的也一塌糊涂,当然在学习过程也积累了一些知识,对于以前没接触过Android的朋友,或者刚入门Android 的朋友,这篇文章作为入门,那 ...

  10. Scala之Map,Tuple

    /** * 1,默认情况下Map构造的是不可变的集合,里面的内容不可修改,一旦修改就变成新的Map,原有的Map内容保持不变: * 2,Map的实例是调用工厂方法模式apply来构造Map实例,而需要 ...