poj 2186 Popular Cows
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 29908 | Accepted: 12131 |
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
Sample Output
Hint
Source
题解
tarjan一直没学会,今天写了道入门题。主要思路是用tarjan()求出强连通分量,记录每一个强连通分量的出度,出度为0的就是大家都认可的popular cow。在数组问题上卡了很久,后来发现u[i],v[i]要开5*10100才ac。
15701878
ksq2013 | 2186 | Accepted | 1304K | 94MS | C++ | 1969B | 2016-07-11 13:36:28 |
#include<cstdio>
#include<cstring>
#include<iostream>
#define M 10100
using namespace std;
int n,m,u[M*],v[M*],first[M*],mnext[M*],ans[M],outdu[M];
int dfs_clock,scc_cnt,stack_count,my_stack[M*],pre[M],sccno[M],lowlink[M];
void Init()
{
memset(first,-,sizeof(first));
memset(mnext,,sizeof(mnext));
memset(my_stack,,sizeof(my_stack));
memset(lowlink,,sizeof(lowlink));
memset(sccno,,sizeof(sccno));
memset(pre,,sizeof(pre));
memset(ans,,sizeof(ans));
memset(outdu,,sizeof(outdu));
}
void Link()
{
for(int i=;i<=m;i++){
scanf("%d%d",&u[i],&v[i]);
mnext[i]=first[u[i]];
first[u[i]]=i;
}
}
void dfs(int ux)
{
pre[ux]=lowlink[ux]=++dfs_clock;
my_stack[++stack_count]=ux;
int vx;
for(int i=first[ux];i!=-;i=mnext[i]){
vx=v[i];
if(!pre[vx]){
dfs(vx);
lowlink[ux]=min(lowlink[ux],lowlink[vx]);
}
else if(!sccno[vx])
lowlink[ux]=min(lowlink[ux],pre[vx]);
}
if(lowlink[ux]==pre[ux]){
scc_cnt++;
int tmp=;
do{
tmp++;
vx=my_stack[stack_count--];
sccno[vx]=scc_cnt;
}while(vx!=ux);
ans[scc_cnt]=tmp;
}
}
void Tarjan()
{
dfs_clock=scc_cnt=stack_count=;
for(int i=;i<=n;i++)
if(!pre[i])dfs(i);
}
int main()
{
while(~scanf("%d%d",&n,&m)){
Init();
Link();
Tarjan();
if(scc_cnt==){
printf("%d\n",n);
continue;
}
for(int i=;i<=n;i++){
for(int j=first[i];j!=-;j=mnext[j]){
int vx=v[j];
if(sccno[i]!=sccno[vx])
outdu[sccno[i]]++;
}
}
int res=;
for(int i=;i<=scc_cnt;i++)
if(!outdu[i]){
if(!res)res=ans[i];
else {res=;break;}
}
printf("%d\n",res);
}
return ;
}
poj 2186 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. 求 ...
- 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 ...
- tarjan缩点练习 洛谷P3387 【模板】缩点+poj 2186 Popular Cows
缩点练习 洛谷 P3387 [模板]缩点 缩点 解题思路: 都说是模板了...先缩点把有环图转换成DAG 然后拓扑排序即可 #include <bits/stdc++.h> using n ...
- 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: 31815 Accepted: 12927 De ...
- POJ 2186 Popular Cows(强连通)
Popular Cows Time Limit: 2000MS Memo ...
- poj 2186 Popular Cows【tarjan求scc个数&&缩点】【求一个图中可以到达其余所有任意点的点的个数】
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 27698 Accepted: 11148 De ...
随机推荐
- Android Property Animation动画
3.0以前,android支持两种动画模式,tween animation,frame animation,在android3.0中又引入了一个新的动画系统:property animation,这三 ...
- Activity源码简要分析总结
Activity源码简要分析总结 摘自参考书籍,只列一下结论: 1. Activity的顶层View是DecorView,而我们在onCreate()方法中通过setContentView()设置的V ...
- UIApplication是什么
1.UIApplication对象是应用程序的象征 2.每一个应用都有自己的UIApplication对象,而且是单例的(只有一个) 3.通过[UIApplication sharedAppl ...
- Android 手机卫士--自定义控件(获取焦点的TextView)
本文地址:http://www.cnblogs.com/wuyudong/p/5906735.html,转载请注明源地址. 本文将实现标题栏下面的textview中的文字跑马灯的效果,就是将一行文字水 ...
- 布局 position
position : 设置定位方式 跟『定位』相关的有一些属性,最重要的一个是『position』,它主要是设置『定位方式』. 而定位方式最重要的是设置『参照物』. 配合 position 使用的有这 ...
- Eclipse界面设置与常用技巧
建议使用的透视图--视窗组合 Window→Save perspective as 保存当前透视图设置,便于将来不小心打乱后恢复 恢复的方法:Window→reset perspective 定制 ...
- .net 读写记事本文件
这是读取文件的代码 StreamReader myreader = File.OpenText(_filepath);//读取记事本文件 string s = ""; s = my ...
- BFS、DFS与选课问题(拓扑排序)
1选课问题 Leetcode上有这样一道题:有代号0,1,2……n-1的n门课程.其中选择某些课程需要另一些课程作为前提条件.用一组pair来表示这些条件:[1,0],[1,2],表示如果要选修课程1 ...
- HTML5 review
标签: section:适用于独立结构内容,无结构关系(例如article,aside与其他标签可以组合形成具有特定结构关系的标签结构).须具有标题(待考证). menu&nav:menu用于 ...
- 解决easy ui 1.4datebox控件不能清空的问题
用easy ui遇到这个问题,在网上找到了解决方案,不过是1.3.6版本的.现提供1.4版本的修改的具体位置和代码. 我们用的是这个 修改位置:12739行,添加代码: , { text: funct ...