poj2186 Popular Cows --- 强连通
给一个有向图,问有多少结点是其它全部结点都能够到达的。
等价于,在一个有向无环图上,找出度为0 的结点。假设出度为0的结点仅仅有一个,那么这个就是答案。假设大于1个。则答案是0。
这题有环。所以先缩点。
求唯一出度为0的强连通分量。
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<iostream>
#define inf 0x3f3f3f3f
using namespace std;
#define M 10010//图中点数
int sta[M],top;
bool vis[M];
int dfn[M];
int low[M];
int ccnt; //有向图强连通分量个数
int id;
vector<int> e[M];
vector<int> part[M];//每一个联通块的组成
int inpart[M];//每一个原图上的点在哪个联通块里
int n,m,out[M]; void tarjan(int x)
{
int i,j;
dfn[x]=low[x]=id++;
vis[x]=1;
sta[++top]=x;
for(i=0;i<e[x].size();i++)
{
j=e[x][i];
if(dfn[j]==-1)
{
tarjan(j);
low[x]=min(low[x],low[j]);
}
else if(vis[j])
low[x]=min(low[x],dfn[j]);
}
if(dfn[x]==low[x])
{
do
{
j=sta[top--];
vis[j]=0;
part[ccnt].push_back(j);
inpart[j]=ccnt;
}while(j!=x);
ccnt++;
}
} void solve(int n)
{
memset(sta,-1,sizeof sta);
memset(vis,0,sizeof vis);
memset(dfn,-1,sizeof(dfn));
memset(low,-1,sizeof(low));
top=ccnt=id=0;
for(int i=1;i<=n;i++)
if(dfn[i]==-1)
tarjan(i);
} int main()
{
int i,j,a,b,c;
while(~scanf("%d%d",&n,&m))
{
for(i=0;i<=n;i++)
{
part[i].clear();
e[i].clear();
}
while(m--)
{
scanf("%d%d",&a,&b);
e[a].push_back(b);
}
solve(n);
memset(out,0,sizeof out);
for(i=1;i<=n;i++)
{
for(j=0;j<e[i].size();j++)
{
a=e[i][j];
if(inpart[a]!=inpart[i])
out[inpart[i]]++;
}
}
int ans=0,flag=0;
for(i=0;i<ccnt;i++)
{
if(out[i]==0)
{
flag++;
ans+=part[inpart[i]].size();
}
if(flag>1)//大于一个出度为0的点 则没有符合条件的答案
{
ans=0;
break;
}
}
printf("%d\n",ans);
}
return 0;
}
poj2186 Popular Cows --- 强连通的更多相关文章
- POJ2186 Popular Cows [强连通分量|缩点]
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31241 Accepted: 12691 De ...
- POJ2186 Popular Cows 强连通分量tarjan
做这题主要是为了学习一下tarjan的强连通分量,因为包括桥,双连通分量,强连通分量很多的求法其实都可以源于tarjan的这种方法,通过一个low,pre数组求出来. 题意:给你许多的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 ...
- poj 2186 Popular Cows (强连通分量+缩点)
http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
- POJ-2186 Popular Cows,tarjan缩点找出度为0的点。
Popular Cows 题意:一只牛崇拜另外一只牛,这种崇拜关系可以传导.A->B,B->C =>A->C.现在给出所有的关系问你有多少牛被其他所有的牛都崇拜. 思路:就是一 ...
- 【Tarjan缩点】POJ2186 Popular Cows
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 35644 Accepted: 14532 De ...
- poj2186 Popular Cows 题解——S.B.S.
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 29642 Accepted: 11996 De ...
随机推荐
- windows下使用mingw编译python扩展模块
环境: 1.python2.7.2 2.mingw20120426 说明: PYTHON_HOME:不做特殊说明的话,默认代表python的安装路径. MINGW_HOME:不做特殊说明的话,默认代表 ...
- 只有电信3G是公网ip。
只有电信3G是公网ip,其它网络拿到是内部网.
- poj3429(有错)
不知道为什么错.. /************************************************************************* > File Name: ...
- LeetCode-001 Two Sum
[题目] Given an array of integers, find two numbers such that they add up to a specific target number. ...
- 根据自己的需要适度使用Web开发框架
软件系统发展到今天已经很复杂了,特别是服务器端软件,涉及到的知识,内容,问题太多.Web开发框架能够帮我们大大减少工作量,但是我们应该如何正确看待Web开发框架,并且如何去使用他们呢? 对框架的依赖 ...
- oracle update语句的几点写法
update两表关联的写法包括字查询 1.update t2 set parentid=(select ownerid from t1 where t1.id=t2.id); 2. update tb ...
- VMware vCenter Converter 使用,Error code: 2147549183 (0x8000FFFF) 解决方案
在实际生产环境中,为了节省成本,会将实体机转换为虚拟机,从而虚拟化. 在公司数据中心环境的中跑VMware vCenter ConverterStandalone Client 4.3,将其中一台on ...
- mantis 中文统计报表乱码问题解决办法
mantis 中文报表乱码问题 1.安装mantisTB 1.2.17:a.安装插件:管理-->插件管理-->安装MantisGraph(Mantis图表 1.0) 插件b.修改配置文件: ...
- Database Initialization Parameters for Oracle E-Business Suite Release 12 (文档 ID 396009.1)
In This Document Section 1: Common Database Initialization Parameters For All Releases Section 2: Re ...
- leetcode Merge K sorted Lists python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...