poj2186 Popular Cows 题解——S.B.S.
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 29642 | Accepted: 11996 |
Description
popular, even if this is not explicitly specified by an ordered pair in the input. Your task is to compute the number of cows that are considered popular by every other cow.
Input
* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular.
Output
Sample Input
3 3
1 2
2 1
2 3
Sample Output
1
Hint
Source
有n只牛,牛A认为牛B很牛,牛B认为牛C很牛。
给你M个关系(谁认为谁牛),求大家都认为它很牛的牛有几只。
p.s.如果牛A认为牛B很牛,牛B认为牛C很牛。那么我们就认为牛A认为牛C很牛。
/*
Problem: poj 2186
OJ: POJ
User: S.B.S.
Time: 297 ms
Memory: 1468 kb
Length: 2420 b
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<iomanip>
#include<cassert>
#include<climits>
#include<functional>
#include<bitset>
#include<vector>
#include<list>
#include<map>
#define maxn 10001
#define F(i,j,k) for(int i=j;i<=k;i++)
#define M(a,b) memset(a,b,sizeof(a))
#define FF(i,j,k) for(int i=j;i>=k;i--)
#define inf 0x3f3f3f3f
#define maxm 50001
#define mod 998244353
//#define LOCAL
using namespace std;
int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m;
struct EDGE
{
int from;
int to;
int next;
}edge[maxm];
int head[maxn];
int dfn[maxn],low[maxn],stack1[maxn];
int num[maxn],du[maxn],vis[maxn];
int tim,tp,dcnt,sum;
int cnt,time=,top,cut,tot;
inline void addedge(int u,int v)
{
edge[tot].from=u;
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot;
tot++;
}
inline void dfs(int u,int fa)
{
dfn[u]=time;
low[u]=time;
time++;
vis[u]=;
stack1[top]=u;
top++;
for(int i=head[u]; i!=-; i=edge[i].next)
{
int v=edge[i].to;
if(!vis[v]){
dfs(v,u);
low[u]=min(low[u],low[v]);
}
else if(vis[v]){
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u]){
cut++;
while(top>&&stack1[top]!=u)
{
top--;
vis[stack1[top]]=;
num[stack1[top]]=cut;
}
}
}
int main()
{
std::ios::sync_with_stdio(false);//cout<<setiosflags(ios::fixed)<<setprecision(1)<<y;
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif
cin>>n>>m;
M(head,-);
F(i,,m-){
int a,b;
cin>>a>>b;
addedge(a,b);
}
F(i,,n) if(!vis[i]) dfs(i,);
F(i,,n)for(int j=head[i];j!=-;j=edge[j].next){
if(num[i]!=num[edge[j].to]){
du[num[i]]++;
}
}
int x;sum=;
F(i,,cut)
if(!du[i]){
sum++;
x=i;
}
if(sum==){
sum=;
F(i,,n) if(num[i]==x) sum++;
cout<<sum<<endl;
}
else cout<<""<<endl;
return ;
}
poj 2186
poj2186 Popular Cows 题解——S.B.S.的更多相关文章
- POJ2186 Popular Cows 题解 强连通分量入门题
题目链接:http://poj.org/problem?id=2186 题目大意: 每头牛都想成为牛群中的红人. 给定N头牛的牛群和M个有序对(A, B),(A, B)表示牛A认为牛B是红人: 该关系 ...
- POJ2186 Popular Cows 题解 强连通分量
题目链接:http://poj.org/problem?id=2186 题目大意: 每头牛都想成为牛群中的红人. 给定N头牛的牛群和M个有序对(A, B),(A, B)表示牛A认为牛B是红人: 该关系 ...
- 强连通分量tarjan缩点——POJ2186 Popular Cows
这里的Tarjan是基于DFS,用于求有向图的强联通分量. 运用了一个点dfn时间戳和low的关系巧妙地判断出一个强联通分量,从而实现一次DFS即可求出所有的强联通分量. §有向图中, u可达v不一定 ...
- 洛谷——P2341 [HAOI2006]受欢迎的牛//POJ2186:Popular Cows
P2341 [HAOI2006]受欢迎的牛/POJ2186:Popular Cows 题目背景 本题测试数据已修复. 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所 ...
- POJ2186 Popular Cows 【强连通分量】+【Kosaraju】+【Tarjan】+【Garbow】
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 23445 Accepted: 9605 Des ...
- 【Tarjan缩点】POJ2186 Popular Cows
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 35644 Accepted: 14532 De ...
- POJ2186 Popular Cows [强连通分量|缩点]
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31241 Accepted: 12691 De ...
- POJ-2186 Popular Cows,tarjan缩点找出度为0的点。
Popular Cows 题意:一只牛崇拜另外一只牛,这种崇拜关系可以传导.A->B,B->C =>A->C.现在给出所有的关系问你有多少牛被其他所有的牛都崇拜. 思路:就是一 ...
- POJ2186:Popular Cows——题解
http://poj.org/problem?id=2186 题面纯英文--就不粘题面了. 简单的说,就是将图强连通分量缩点,然后拓扑一下. 为了避免拓扑,我们可以反向存图,然后查入度为0的点即可. ...
随机推荐
- Git & GitHub 学习
学习资料: Git版本控制软件结合GitHub从入门到精通常用命令学习手册:http://www.ihref.com/read-16369.html 官方中文手册:http://git-scm.com ...
- 使用matlab表示“段数不确定”的分段函数
示例函数: 分段函数f(x)的段数为数组a的长度减1,在表达f(x)时,不能直接使用a的长度5-1=4. 方法1: 先计算每个间隔点的函数值f(a2),f(a3),f(a4),再循环表示f(x). f ...
- 【WPF】Behavior的使用
如何将一个行为附加到某个元素上呢?我们可以通过自定义一个Behavior! 我们首先看一下IAttachedObject接口,Behavior默认继承之这个接口 // 摘要: // 供可以附加到另一个 ...
- promise总结
new Promise( // 执行器 function (resolve, reject) { //一段耗时很长的异步操作 resolve(); //数据处理完成 reject(); //数据处理出 ...
- nyoj 211&&poj 3660
Cow Contest 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, ...
- FastReport.Net使用:[4]分组
1.绘制报表标题和栏首. 2.设置报表栏,为数据区添加一个分组 3.右键分组页眉,在其右键菜单中选择“编辑”,显示分组编辑对话框. 设置分组条件,可直接通过下拉菜单选择Table表中的[学号]列:也能 ...
- redis_NoSql入门概述
一.为什么要使用Nosql 今天我们可以通过第三方平台(如:google,facebook)可以很容易的访问和抓取数据.用户的个人信息,社交网络,地理位置,用户生成的数据和用户操作日志已经成倍的增加. ...
- jni java C/C++ 相互调用
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha java 中 声明 一个 native 方法 用javah命令 生成 包含 nativ ...
- JxBrowser概述与简单应用
Q:JxBrowser是什么? JxBrowser是一个跨平台的Java库,允许将基于Google Chromium的Web浏览器组件集成到Java Swing / AWT / JavaFX应用程序中 ...
- Mysql插入数据时,报错this is incompatible with sql_mode=only_full_group_by
Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'inform ...