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的点即可. ...
随机推荐
- php简明学习教程
1.变量 <?php //变量声明(php变量无需单独创建,变量会在第一次赋值时创建) $a = 1; //弱类型(php变量会根据其值自动转换为相应的数据类型) $a = "a&qu ...
- bzoj 1132 几何
思路:我刚开始算三角形的方法是原点叉积三条边,然后计算每条边向量积的贡献,但是对于同一条线上的点 有时候没有办法抵消掉..... 看网上的思路是对于一个三角形的面积通过两条边的叉积获得,然后枚举一个点 ...
- Recursion in Java
Recursion in Java 递归无出口 public class RecursionExample1 { public static void p() { System.out.println ...
- HDU 6029 Graph Theory
贪心. 每次找到后面最近的一个能连边的连边. #include <bits/stdc++.h> using namespace std; ; int T,n,k; ],b[],u[]; i ...
- html5一些容易忽略的细节
最近由于经常写前端,所以系统性的看了一下html5页面的基础信息,虽然以前写了很久的html代码,但是其中的一些细节还是容易被忽略,所以这里一起整理一下. 在html5中,空元素结尾处的空格和斜杠是可 ...
- java项目日志系统的总结
目录 日志系统归类以及关系 日志的三个组件 slf4j的使用 项目中构建日志系统 使用例子 日志系统归类以及关系 常用的日志框架: slf4j.logback .log4j.log4j2.JUL(ja ...
- Can you find it? HDU - 2141 (二分查找)
Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate ...
- [BZOJ4554][TJOI2016&&HEOI2016]游戏(匈牙利)
4554: [Tjoi2016&Heoi2016]游戏 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 857 Solved: 506[Sub ...
- JZYZOJ1998 [bzoj3223] 文艺平衡树 splay 平衡树
http://172.20.6.3/Problem_Show.asp?id=1998 平衡树区间翻转的板子,重新写一遍,给自己码一个板子. #include<iostream> #incl ...
- python开发_tkinter
Tkinter模块("Tk 接口")是Python的标准Tk GUI工具包的接口.Tk和Tkinter可以在大多数的Unix平台下使用, 同样可以应用在Windows和Macint ...