POJ 2186 tarjan+缩点 基础题
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 37111 | Accepted: 15124 |
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
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <vector>
#include <iomanip>
using namespace std;
const int maxn = 1e5+;
const int maxm = 1e4+;
const int inf = 0x3f3f3f3f;
const double epx = 1e-;
typedef long long ll;
const ll INF = 1e18;
const double pi = acos(-1.0);
int mp[maxn];
struct node
{
int v,next;
}edge[maxn];
int dfn[maxn],low[maxn],index,visit[maxn],cnt,tot;
int point[maxn];
int heads[maxn],stack[maxn],num;
void add(int x,int y)
{
edge[++cnt].next=heads[x];
edge[cnt].v=y;
heads[x]=cnt;
return;
}
void tarjan(int x)
{
dfn[x]=low[x]=++tot;
stack[++index]=x;
visit[x]=;
for(int i=heads[x];i!=-;i=edge[i].next)
{
if(!dfn[edge[i].v])
{
tarjan(edge[i].v);
low[x]=min(low[x],low[edge[i].v]);
}
else if(visit[edge[i].v])
{
low[x]=min(low[x],dfn[edge[i].v]);
}
}
if(low[x]==dfn[x])
{
do{
int temp=stack[index];
point[temp]=num;
visit[temp]=;
index--;
}while(x!=stack[index+]);
num++;
}
return;
}
int main()
{
int n,m;
memset(heads,-,sizeof(heads));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(mp,,sizeof(mp));
scanf("%d%d",&n,&m);
tot=cnt=num=index=;
for(int i=;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
}
for(int i=;i<=n;i++)
if(!dfn[i]) tarjan(i);
int sum=;
for(int i=;i<=n;i++)
{
for(int j=heads[i];j!=-;j=edge[j].next)
{
if(point[i]!=point[edge[j].v])
{
mp[point[i]]=;
}
}
}
int cont=,index;
for(int i=;i<num;i++)
if(mp[i]==)
{
cont++,index=i;
}
if(cont==)
{
int ans=;
for(int i=;i<=n;i++)
if(point[i]==index)
ans++;
cout<<ans<<endl;
}
else
cout<<""<<endl;
return ;
}
推荐两篇博客 https://blog.csdn.net/qq_34374664/article/details/77488976
http://blog.miskcoo.com/2016/07/tarjan-algorithm-strongly-connected-components
POJ 2186 tarjan+缩点 基础题的更多相关文章
- poj 2186(tarjan+缩点)
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 37083 Accepted: 15104 De ...
- poj 2955 Brackets (区间dp基础题)
We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a ...
- POJ 2762 tarjan缩点+并查集+度数
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15494 ...
- POJ - 2186 Popular Cows tarjain模板题
http://poj.org/problem?id=2186 首先求出所有的强连通分量,分好块.然后对于每一个强连通分量,都标记下他们的出度.那么只有出度是0 的块才有可能是答案,为什么呢?因为既然你 ...
- POJ 3694 (tarjan缩点+LCA+并查集)
好久没写过这么长的代码了,题解东哥讲了那么多,并查集优化还是很厉害的,赶快做做前几天碰到的相似的题. #include <iostream> #include <algorithm& ...
- poj 2186 tarjan求强连通分量
蕾姐讲过的例题..玩了两天后才想起来做 貌似省赛之后确实变得好懒了...再努力两天就可以去北京玩了! 顺便借这个题记录一下求强连通分量的算法 1 只需要一次dfs 依靠stack来实现的tarjan算 ...
- poj 2186 (强连通缩点)
题意:有N只奶牛,奶牛有自己认为最受欢迎的奶牛.奶牛们的这种“认为”是单向可传递的,当A认为B最受欢迎(B不一定认为A最受欢迎),且B认为C最受欢迎时,A一定也认为C最受欢迎.现在给出M对这样的“认为 ...
- POJ 2672 Tarjan + 缩点 + 拓扑思想
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17383 ...
- poj 2762(tarjan缩点+判断是否是单链)
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19234 ...
随机推荐
- sdut1650I-Keyboard(dp)
链接 题目大意就相当于 跟你一串字符串 让你截成k段 使总体的值最小 想法是递归的 递归太慢 可以转换为递推的 这样就有可以推出状态方程 dp[i][j] = max(dp[i][j],dp[i-1] ...
- [转]Resolve Team Foundation Version Control conflicts
本文转自:http://msdn.microsoft.com/en-us/library/ms181432.aspx An advantage of using Team Foundation ver ...
- windows8.1专业版 关闭ie11总是已停止工作
该问题通常原因: 1 系统重病毒: 2 系统和安装的软件不兼容导致. 解决方案: 1 杀毒更新至最新进行杀毒,仍未解决,重新安装系统: 2 目前身边人员多数属于该情况: 1 如安装了输入法.迅雷或其它 ...
- web+ admin template,spa管理应用后台,easyui后台正式发布
演示地址:http://admintemplate.webplus.org.cn/ v1.0 (2016/7/27) 扁平化风格 全屏支持 后台管理不使用iframe,全ajax开发 权限管理 商品管 ...
- java 对象流的简单使用
对象的输入输出流的作用: 用于写入对象 的信息和读取对象的信息. 使得对象持久化. ObjectInputStream : 对象输入流 ObjectOutPutStream :对象输出流 ...
- iOS Programming Controlling Animations 动画
iOS Programming Controlling Animations 动画 The word "animation" is derived from a Latin wor ...
- JavaScript——XMLHttpRequest 家族
https://www.zhangxinxu.com/wordpress/2013/10/understand-domstring-document-formdata-blob-file-arrayb ...
- leetcode_654. Maximum Binary Tree
https://leetcode.com/problems/maximum-binary-tree/ 给定数组A,假设A[i]为数组最大值,创建根节点将其值赋为A[i],然后递归地用A[0,i-1]创 ...
- Maven的pom报错的解决方法
如果在MyEclipse里面导入项目,导入不了,如下图 接下来可以点击Import Maven Projects里的Action那一行Resolve Later. 点击Do Not Execute(a ...
- MRC转ARC
转载请注明出处:http://blog.csdn.net/cywn_d/article/details/18222671 1.删除所有retain,release和autorelease. 2.把原来 ...