Popular Cows-POJ2186Tarjan
| Time Limit: 2000MS | Memory Limit: 65536K | |
Description
Since popularity is transitive, if A thinks B is popular and B thinks C is popular, then A will also think that C is
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头奶牛,给定m对关系(A,B),表示A奶牛仰慕B奶牛。计算牧群中被其他奶牛仰慕的奶牛的数目。
思路:将n头奶牛的m个关系将会构建一个有向图,在图中强连通分量中的任意奶牛一定是被分量中的其他奶牛仰慕。所以问题就转化为在图中将强连通分量进行缩点,在形成的新图中,如果一个强连通分量集合的出度为零,说明这个集合被其他集合仰慕,而不仰慕其他的集合,所以如果在新图中集合出度为零的数目不大于1,则为出度为零集合中奶牛的数目,如果大于1,则出度为零集合之间没有仰慕关系的,所以结果为0.
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm> using namespace std; const int Max = 10100; typedef struct node
{
int v; int next; }Line; vector<int>G[Max]; Line Li[Max*5]; int Head[Max],top; int dfn[Max],low[Max],pre[Max]; int num[Max],Num,Du[Max]; int dep; bool vis[Max]; int ans; stack<int>S; void AddEdge(int u,int v)
{
Li[top].v=v ; Li[top].next=Head[u]; Head[u]=top++;
} void Tarjan(int u)// Tarjan求强连通分量
{
dfn[u]=low[u]=dep++; S.push(u); for(int i=Head[u];i!=-1;i=Li[i].next)
{
if(dfn[Li[i].v]==-1)
{
Tarjan(Li[i].v); low[u]=min(low[u],low[Li[i].v]);
}
else
{
low[u]=min(low[u],dfn[Li[i].v]);
}
} if(low[u]==dfn[u])//强连通分量的根节点
{ while(!S.empty())
{
int v = S.top(); S.pop(); pre[v] = Num;//给分量集合标号 G[Num].push_back(v);//记录集合对应的点 num[Num]++; if(v==u)
{
break;
}
} Num++;
} } int main()
{ int n,m; while(~scanf("%d %d",&n,&m))
{
top = 0; memset(Head,-1,sizeof(Head)); int u,v; for(int i=0;i<m;i++)
{
scanf("%d %d",&u,&v); AddEdge(u,v);
} memset(dfn,-1,sizeof(dfn)); memset(num,0,sizeof(num)); for(int i =0;i<=n;i++)
{
G[i].clear();
} dep = 0;Num = 0; for(int i=1;i<=n;i++)
{
if(dfn[i]==-1)
{
Tarjan(i);
}
} memset(Du,0,sizeof(Du)); for(int i=0; i<Num ;i++) //判断强连通分量的出度
{
memset(vis,false,sizeof(vis)); for(int k=0;k<G[i].size();k++)
{
for(int j=Head[G[i][k]]; j!=-1;j=Li[j].next)
{
if(i!=pre[Li[j].v])
{
if(!vis[pre[Li[i].v]])//由于不同的强连通分量之间相连的边可能不止一条,所以要判断是不是已经统计过。
{
vis[pre[Li[i].v]]=true; Du[i]++;
}
}
}
}
} ans = 0; int ant = 0; for(int i=0;i<Num;i++)//如果超过一个的出度为零的强连通分量,则这些连通分量就不能被其他的牛都仰慕。
{
if(Du[i]==0)
{
ans+=num[i]; ant++; }
} printf("%d\n",ant<=1?ans:0); } return 0;
}
Popular Cows-POJ2186Tarjan的更多相关文章
- POJ 2186 Popular Cows(Targin缩点)
传送门 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31808 Accepted: 1292 ...
- POJ2186 Popular Cows [强连通分量|缩点]
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31241 Accepted: 12691 De ...
- poj 2186 Popular Cows
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 29908 Accepted: 12131 De ...
- [强连通分量] 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 (强连通分量+缩点)
http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
- poj 2186 Popular Cows【tarjan求scc个数&&缩点】【求一个图中可以到达其余所有任意点的点的个数】
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 27698 Accepted: 11148 De ...
- POJ2186 Popular Cows 【强连通分量】+【Kosaraju】+【Tarjan】+【Garbow】
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 23445 Accepted: 9605 Des ...
- POJ 2186 Popular Cows (强联通)
id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 655 ...
- 强连通分量tarjan缩点——POJ2186 Popular Cows
这里的Tarjan是基于DFS,用于求有向图的强联通分量. 运用了一个点dfn时间戳和low的关系巧妙地判断出一个强联通分量,从而实现一次DFS即可求出所有的强联通分量. §有向图中, u可达v不一定 ...
随机推荐
- scala的面向对象编程
1.scala的简单编程 2.构造方法 辅助构造函数是在主构造函数没有的情况下,执行的构造函数. 3.object的介绍 4.半生类和半生对象 5.半生的案例程序(半生类可以调用半生) 6.apply ...
- 毕设1--利用Java实现网页的模板功能技术---简要了解
首先,关于我对自己的毕业设计题目的理解,其中没有接触过的技术有怎么用Java实现将原有的Word的模板上传到网页中,在网页中进行相关操作.之所以把这部分放在一开始来进行了解是因为没有接触过这一方面,比 ...
- Sping
- Selenium2学习-041-chromedriver:org.openqa.selenium.WebDriverException: unknown error: cannot determine loading status from unexpected alert open
今天在写WebDriver处理弹出框(alert.confirm.prompt)演示实例脚本分发给朋友时,在其执行时未能成功执行,对应的部分错误详情如下: org.openqa.selenium.We ...
- Python模块应用 (linecache)
linecache linecache是专门支持读取大文件,而且支持行式读取的函数库. linecache 预先把文件读入缓存起来,后面如果你访问该文件的话就不再从硬盘读取.对于大文件的读取效率还不错 ...
- centos7 搭建GlusterFS
centos7 搭建GlusterFS 转载http://zhaijunming5.blog.51cto.com/10668883/1704535 实验需求:4台机器安装GlusterFS组成一个集群 ...
- Windows Azure - Troubleshooting & Debugging: Role Recycling
每年总会碰到几次Role Recycling,处理完记录下 :) 1. 和往常一样先排查系统日志,修复异常.> 没效果 :( 2. 排查Event Viewer中的Logs,没有发现比较奇怪Lo ...
- sql中datetime 和 timestamp
datetime 1.允许为空值,可以自定义值,系统不会自动修改其值. 2.不可以设定默认值,所以在不允许为空值的情况下,必须手动指定datetime字段的值才可以成功插入数据. 3.虽然不可以设定默 ...
- linux---------------centos6.4安装完了以后敲ifconfig,没有局域网ip。解决如下
1.vim /etc/sysconfig/network-scripts/ifcfg-eth0 进入linux然后进入这个文件里面如下: DEVICE=eth0HWADDR=00:0C:29:92:F ...
- tcpreplay,tcprewrite的使用---张子芳
[关键字]: tcpreplay, tcpprep, tcprewrite, libpcap, winpcap, linux, windows, cygwin[摘要]: 本文总结了tcpreplay的 ...