[poj] 2618 popular cows
这是一个强连通分量板子题。
a thinks b is popular 即为a到b有一条边,要求被所有牛popular的牛的个数。
所求为对图进行强连通分量缩点后,没有出度的强连通分量里的点数(这样的强连通分量只能有一个)。
#include<cstdio>
#include<algorithm>
#include<stack>
#define N 10010
#define M 50010
using namespace std;
int n,m,head[N],dfn[N],low[N],cnt=1,t,sum,bel[N],num[N],out[N],ans;
bool instk[N];
stack <int> stk;
struct hhh
{
int to,next;
}edge[M];
int read()
{
int ans=0,fu=1;
char j=getchar();
for (;(j<'0' || j>'9') && j!='-';j=getchar()) ;
if (j=='-') fu=-1,j=getchar();
for (;j>='0' && j<='9';j=getchar()) ans*=10,ans+=j-'0';
return ans*fu;
}
void add(int u,int v)
{
edge[cnt].to=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void Tarjan(int x)
{
dfn[x]=low[x]=++t;
stk.push(x);
instk[x]=1;
int v;
for (int i=head[x];i;i=edge[i].next)
{
v=edge[i].to;
if (!dfn[v])
{
Tarjan(v);
low[x]=min(low[x],low[v]);
}
else if (instk[v]) low[x]=min(low[x],dfn[v]);
}
if (dfn[x]==low[x])
{
sum++;
do
{
v=stk.top();
stk.pop();
instk[v]=0;
bel[v]=sum;
num[sum]++;
}while(v!=x);
}
}
int main()
{
n=read();
m=read();
for (int i=1,a,b;i<=m;i++)
{
a=read();
b=read();
add(a,b);
}
for (int i=1;i<=n;i++)
if (!dfn[i]) Tarjan(i);
for (int i=1;i<=n;i++)
for (int j=head[i],v;j;j=edge[j].next)
{
v=edge[j].to;
if (bel[i]!=bel[v]) out[bel[i]]++;
}
for (int i=1;i<=sum;i++)
{
if (!out[i] && ans)
{
ans=0;
break;
}
else if (!out[i]) ans=num[i];
}
printf("%d",ans);
return 0;
}
[poj] 2618 popular cows的更多相关文章
- 强连通分量分解 Kosaraju算法 (poj 2186 Popular Cows)
poj 2186 Popular Cows 题意: 有N头牛, 给出M对关系, 如(1,2)代表1欢迎2, 关系是单向的且能够传递, 即1欢迎2不代表2欢迎1, 可是假设2也欢迎3那么1也欢迎3. 求 ...
- tarjan缩点练习 洛谷P3387 【模板】缩点+poj 2186 Popular Cows
缩点练习 洛谷 P3387 [模板]缩点 缩点 解题思路: 都说是模板了...先缩点把有环图转换成DAG 然后拓扑排序即可 #include <bits/stdc++.h> using n ...
- poj 2186 Popular Cows (强连通分量+缩点)
http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
- POJ 2186 Popular Cows (强联通)
id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 655 ...
- poj 2186 Popular Cows 【强连通分量Tarjan算法 + 树问题】
题目地址:http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Sub ...
- POJ 2186 Popular Cows(Targin缩点)
传送门 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31808 Accepted: 1292 ...
- poj 2186 Popular Cows
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 29908 Accepted: 12131 De ...
- [强连通分量] POJ 2186 Popular Cows
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31815 Accepted: 12927 De ...
- POJ 2186 Popular Cows(强连通)
Popular Cows Time Limit: 2000MS Memo ...
随机推荐
- noip2018 洛谷 P5020 货币系统
关键: 要使m最小,(m,b)中的数不能用(n,a)中的数表示出来 对于 3 19 10 6 19=10+3+3+3 6=3+3 只有3 和 10 不能被(n,a)中的数表示 所以m=2 只需要 ...
- mysql 创建用户、授权、修改密码
以下操作都要在mysql所在机器操作 一.创建用户 CREATE USER 'dog'@'localhost' IDENTIFIED BY '123456'; 或 insert into mysql. ...
- Windows Server 2012 搭建DHCP及远程路由访问
1.1 基础环境信息 1.2 DHCP与远程访问服务器角色安装 1.服务器管理器—>仪表板—>添加角色和功能,出现添加角色和功能向导,点击下一步 2.选择安装类型为基于角色或基 ...
- Java中的二进制运算出错问题
问题: 最近在做Java web项目中需要计算金额总和,在这里出现了一个问题是我以前没有关注到的: System.out.println(2.0-1.1); 执行时候的console中打印输出的是 0 ...
- php面向对象(2)值传递
PHP中值传递方式,2中 值传递:传递的时候,拷贝的是数据本身.默认都是值传递 结果:传递完成,有了2份同样的数据,且2个变量“相互独立”,不会相互影响 引用传递:传递的时候,拷贝的是引用关系(数据的 ...
- Redis------Set集合类型
set是string类型的无序集合 类比:你的朋友不能超过2的32次方-1个元素 基本添加删除操作 取并集 取交集 取差集 注意:每个元素的各个元素不能重复 应用场合:qq好友推荐‘ TomFri 的 ...
- [CodeForces948B]Primal Sport(数论)
Description 题目链接 Solution 设f(x)为x的最大质因子 那么由题意易得\(X_1\)的范围在\([X_2-f(X_2)+1,X2]\) 同理\(X_0\)的范围在\([X_1- ...
- SSM框架的简单搭建
转:https://blog.csdn.net/zhshulin/article/details/37956105 Spring+SpringMVC+MyBatis spring : 4. ...
- Android面试收集录14 Android进程间通信方式
一.使用 Intent Activity,Service,Receiver 都支持在 Intent 中传递 Bundle 数据,而 Bundle 实现了 Parcelable 接口,可以在不同的进程间 ...
- cocos2d-x 3.0的入门程序:helloworld
看过了这么多不同方向的应用,发现很多程序入门都是helloworldhelloworld是所有程序员的绝对初恋 先看一下程序的运行结果吧 然后就是他的工程代码 工程的目录有两个 Classes:程序中 ...