Popular Cows
传送门(poj):http://poj.org/problem?id=2186
(bzoj):http://www.lydsy.com/JudgeOnline/problem.php?id=1051
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 33482 | Accepted: 13638 |
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
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
#define N 10009
struct Edge
{
int x,y,next;
Edge(int x=,int y=,int next=):
x(x),y(y),next(next){}
}edge[N*];
int sumedge,n,m,x,y,tim,top,sumclr,sumedge2,js,ans;
int head[N],dfn[N],low[N],Stack[N],color[N],cnt[N],out[N],head2[N];
bool vis[N],instack[N];
void ins(int x,int y)
{
edge[++sumedge]=Edge(x,y,head[x]);
head[x]=sumedge;
}
void ins2(int x,int y)
{
edge[++sumedge2]=Edge(x,y,head2[x]);
head2[x]=sumedge2;
}
map<int,bool>Map[N];
void tarjan(int x)
{
dfn[x]=low[x]=++tim;
vis[x]=;instack[x]=;Stack[++top]=x;
for(int u=head[x];u;u=edge[u].next)
if(instack[edge[u].y])
low[x]=min(low[x],dfn[edge[u].y]);
else
if(!vis[edge[u].y])
{
tarjan(edge[u].y);
low[x]=min(low[x],low[edge[u].y]);
}
else
{
}
if(dfn[x]==low[x])
{
sumclr++;
color[x]=sumclr;
cnt[sumclr]++;
while(Stack[top]!=x)
{
color[Stack[top]]=sumclr;//染色
instack[Stack[top]]=;
top--;
cnt[sumclr]++;//记录这个连通块里牛的个数
}
instack[x]=;
top--;
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
scanf("%d%d",&x,&y);
ins(x,y);
}
for(int i=;i<=n;i++)
{
if(!vis[i])tarjan(i);
top=;
}
for(int i=;i<=n;i++)
for(int u=head[i];u;u=edge[u].next)
if(color[i]!=color[edge[u].y])
if(Map[color[i]].find(color[edge[u].y])==Map[color[i]].end()) //缩点
{
Map[color[i]][color[edge[u].y]]=true;
ins2(color[i],color[edge[u].y]);
out[color[i]]++;
}
int cnnt=;
for(int i=;i<=sumclr;i++)
{
if(out[i]==)//度为0意味着所有牛都欢迎他
{
js++;
ans=i;
cnnt+=cnt[i];//加上这个连通块里牛的个数
}
}
if(js>||js==)puts("");//如果有大于1个的度为0的点,说明这个图不是连通的。
else
printf("%d",cnnt);
return ;
}
Popular Cows的更多相关文章
- 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不一定 ...
随机推荐
- 在安装mysql数据库的过程中,显示msvcp100.dll丢失?则么办?
方案一:重装操作系统为windows10专业版 方案二:问题: 解答: 报错原因是VC运行库不全或者没有安装导致,百度搜索VC集合下载安装, 链接:https://pan.baidu.com/s/1U ...
- 电路分析二-------基尔霍夫定律KCL和KVL
1.先了解几个名词 (1)支路----一个二端原件视为一条支路--图中6个二端原件所以有6条支路. (2)结点----两条或以上的支路连接的点. d,e可以看做一个结点. (3).回路----- (4 ...
- cocos2d-x3.6 生成带类图的离线文档
我的博客:http://blog.csdn.net/dawn_moon cocos2d-x的官网有点慢,并且最新3.6的在线API文档居然没有了类图,不知道什么原因,之前2.2.6都是有的. 只是能够 ...
- ExtJS4.2.1与Spring MVC实现Session超时控制
假设你的项目使用ExtJS作为表现层.你会发现,SESSION超时控制将是一个问题. 本文将就自己的经验.来解决这一问题.当然,解决这个问题并不是仅仅有一种方法,我仅仅是提出我的方法. 首先.做超时控 ...
- docker centos yum 源
aliyun yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.rep ...
- python-安装 pip
https://pip.pypa.io/en/stable/installing/ wget https://bootstrap.pypa.io/get-pip.py python get-pip.p ...
- JVM 性能优化, Part 4: C4 垃圾回收
ImportNew注:本文是JVM性能优化 系列-第4篇.前3篇文章请参考文章结尾处的JVM优化系列文章.作为Eva Andreasson的JVM性能优化系列的第4篇,本文将对C4垃圾回收器进行介绍. ...
- linux基础part2
linux基础 一.linux基础命令 1.pwd:用来显示当前目录位置 2.cd:用来切换目录位置.(eg:cd...cd../...cd-.cd~) 3.ls:用来查看目录或文件信息(eg:ls ...
- 3.16课·········C#小结
//附加//C#源码,被C#编译器,编译成执念代码(IL)//int16=short.....±32000//int32=int.......±21亿//int64=long......±922亿亿3 ...
- Python——轻量级web服务器flask的学习
前言: 根据工程需要,开始上手另一个python服务器---flask,flask是一个轻量级的python服务器,简单易用.将我的学习过程记录下来,有新的知识会及时补充. 记录只为更好的分享~ 正文 ...