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. Android系列之UI组件----Menu菜单

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  2. Android性能测试工具Emmagee介绍

    Emmagee介绍 Emmagee是监控指定被测应用在使用过程中占用机器的CPU.内存.流量资源的性能测试小工具.该工具的优势在于如同windows系统性能监视器类似,它提供的是数据采集的功能,而行为 ...

  3. 验证码生成的c语言库

    http://www.open-open.com/lib/view/open1324534929968.html

  4. action中result没有值

    action中result没有值,访问action会输出action中的所有数据,输出类型为.action类型 .

  5. Scrapy

    Scrapy 从Python的Urllib.Urlllib2到scrapy,当然,scrapy的性能且效率是最高的,自己之前也看过一些资料,在此学习总结下. Scrapy介绍 关于scrapy scr ...

  6. C#使用IrisSkin2.dll美化WinForm程序界面

    一.添加控件IrisSkin2.dll. 方法:         1.右键“工具箱”.“添加选项卡”,取名“皮肤”.         2.右键“皮肤”,“选择项”弹出对话框        3.点击“浏 ...

  7. IBatisNet基础组件

    DomSqlMapBuilder DomSqlMapBuilder,其作用是根据配置文件创建SqlMap实例.可以通过这个组件从Stream, Uri, FileInfo, or XmlDocumen ...

  8. Chrome调试工具简单介绍

    作为前端开发者都知道,快捷键F12可以打开chrome调试工具.firefox可以打开firebug工具.“工欲善其事,必先利其器”,对调试工具的掌握,能大大提高我们调试代码的效率.因为我平常chro ...

  9. JS 之DOM range对象

    DOM范围 DOM中的range对象是DOM2中新定义的接口.通过这个对象可以选择文档中的某个区域,而不必考虑节点的界限. 创建范围 document.createRange()创建一个范围,这个范围 ...

  10. Activiti系列——如何在eclipse中安装 Activiti Designer插件

    这两天在评估jbpm和Activiti,需要安装一个Activiti Designer插件试用一下. 一.在线安装 从<Activiti实战>了解到可以通过如下方式安装 打开Eclipse ...