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. SerializeField和Serializable

    Serialize功能 Unity3D 中提供了非常方便的功能可以帮助用户将 成员变量 在Inspector中显示,并且定义Serialize关系. 简单的说,在没有自定义Inspector的情况下所 ...

  2. [cb] Assetbundle打包(一)

    一.简介 Unity的Assetbundle是Unity Pro提供的功能. 理解:Asset 资源,资产:Bundle :包,一批,捆:字面上的意思,就是把资源打包. 在项目中怎么使用Assetbu ...

  3. 关于MySql全文索引

    从 Mysql 4.0 开始就支持全文索引功能,但是 Mysql 默认的最小索引长度是 4.如果是英文默认值是比较合理的,但是中文绝大部分词都是2个字符,这就导致小于4个字的词都不能被索引,全文索引功 ...

  4. 关于audio元素在实际项目中遇到的问题总结

    在ios高版本的微信浏览器下(ios10.0以上),audio标签如果添加autoplay属性的话.导致的问题是:通过二维码扫码第一次进入没有问题,第二次扫码进入之后直接卡死在loading页面. 解 ...

  5. es安装

    1,安装java(至少1.8) yum install -y java java -version 在/etc/profile追加: JAVA_HOME=/usr/java/jdk1..0_45 PA ...

  6. sql截取

    String sql2="select count(*) from t_testuser where substr(INSETTIME,1,10)=to_char(sysdate,'yyyy ...

  7. Caffe学习系列(14):初识数据可视化

    //   首先将caffe的根目录作为当前目录,然后加载caffe程序自带的小猫图片,并显示. 图片大小为360x480,三通道 In [1]: import numpy as np import m ...

  8. 一个Eclipse代码显示主题

  9. Linux内核分析——期末总结

    Linux内核学习总结 首先非常感谢网易云课堂这个平台,让我能够在课下学习,课上加强,体会翻转课堂的乐趣.孟宁老师的课程循序渐进,虽然偶尔我学习地不是很透彻,但能够在后续的课程中进一步巩固学习,更加深 ...

  10. Linux第五次学习笔记

    处理器体系结构 Y86指令集体系结构 定义一个指令集体系结构 ,包括定义各种状态元素.指令集和它们的编码.一组编程规范和异常事件处理. 程序员可见的状态 Y86程序中的每条指令都会读取或修改处理器状态 ...