BZOJ1051:受欢迎的牛(并查集 / Tarjan)
1051: [HAOI2006]受欢迎的牛
Time Limit: 10 Sec Memory Limit: 162 MB
Submit: 8161 Solved: 4460
Description
Input
Output
一个数,即有多少头牛被所有的牛认为是受欢迎的。
Sample Input
1 2
2 1
2 3
Sample Output
HINT
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
using namespace std; const int N = ,M = ;
int n,m,tot;
int head[N],f[N],sum[N],vis[N];
map<int,map<int,int> > mp;
struct Edge{
int u,v,next ;
}e[M];
void adde(int u,int v){
e[++tot].u=u;e[tot].v=v;
e[tot].next=head[u];
head[u]=tot;
}
int find(int x){
return f[x]==x ? f[x] : f[x]=find(f[x]);
}
int main(){
cin>>n>>m;
int st=;
memset(head,-,sizeof(head));
for(int i=;i<=N-;i++) f[i]=i,sum[i]=;
for(int i=,u,v;i<=m;i++){
scanf("%d%d",&u,&v);
if(!mp[u][v]){
mp[u][v]=;
adde(u,v);
int fx=find(u),fy=find(v);
if(fx!=fy){
f[fx]=fy;
sum[fy]+=sum[fx];
if(sum[fy]==n) st=fy;
}
}
}
int cnt =;
if(!st)puts("");
else{
queue<int> q;
q.push(st);vis[st]=;
while(!q.empty()){
int u=q.front();q.pop();
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(!vis[v]){
vis[v]=;
q.push(v);
cnt++;
}
}
}
printf("%d\n",cnt);
}
return ;
}
再来说一下tarjan。
先用tarjan缩点,然后重新构图,找到出度为0的点那么这里面所有的牛都是被所有牛认为受欢迎的。
注意一下构图后不连通的情况就好了。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <stack>
using namespace std; const int N = ,M = ;
int n,m,tot,num,T;
int head[N],dfn[N],low[N],vis[N],scc[N];
struct Edge{
int u,v,next ;
}e[M];
void adde(int u,int v){
e[++tot].u=u;e[tot].v=v;
e[tot].next=head[u];head[u]=tot ;
}
stack <int> s;
void Tanjan(int u){
dfn[u]=low[u]=++T;vis[u]=;
s.push(u);
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(!vis[v]){
Tanjan(v);
low[u]=min(low[u],low[v]);
}else{
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u]){
num++;int now;
do{
now = s.top();s.pop();
scc[now]=num;
}while(!s.empty() && now!=u);
}
}
int main(){
scanf("%d%d",&n,&m);
memset(head,-,sizeof(head));
for(int i=,u,v;i<=m;i++){
scanf("%d%d",&u,&v);
adde(u,v);
}
for(int i=;i<=n;i++){
if(!vis[i]) Tanjan(i);
}
int out[N]={},in[N]={};
for(int u=;u<=n;u++){
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(scc[u]!=scc[v]){
out[scc[u]]++;
in[scc[v]]++;
}
}
}
int cnt=,ans=,tag;
for(int i=;i<=num;i++) if(!out[i]) cnt++,tag=i;
if(cnt==){
for(int i=;i<=n;i++) if(scc[i]==tag) ans++;
printf("%d",ans);
}else puts("");
return ;
}
BZOJ1051:受欢迎的牛(并查集 / Tarjan)的更多相关文章
- BZOJ1051 受欢迎的牛
http://www.lydsy.com/JudgeOnline/problem.php?id=1051 Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A ...
- p2341&bzoj1051 受欢迎的牛
传送门(洛谷) 传送门(bzoj) 题目 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 种关系是具有传递性的,如果A认为B受欢迎,B认为C ...
- bzoj1051受欢迎的牛(Tarjan)
1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4776 Solved: 2542 Description ...
- HDU ACM 2586 How far away ?LCA->并查集+Tarjan(离线)算法
题意:一个村子有n个房子,他们用n-1条路连接起来,每两个房子之间的距离为w.有m次询问,每次询问房子a,b之间的距离是多少. 分析:近期公共祖先问题,建一棵树,求出每一点i到树根的距离d[i],每次 ...
- 关于并查集的路径压缩(Path Compress)优化
之前在CSDN看到一篇很受欢迎的讲解并查集的博文,其中自然用到了路径压缩: int pre[1000]; int find(int x){ int root = x; while(pre[root]! ...
- 【bzoj1051】 [HAOI2006]受欢迎的牛 tarjan缩点判出度算点数
[bzoj1051] [HAOI2006]受欢迎的牛 2014年1月8日7450 Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B ...
- 【并查集缩点+tarjan无向图求桥】Where are you @牛客练习赛32 D
目录 [并查集缩点+tarjan无向图求桥]Where are you @牛客练习赛32 D PROBLEM SOLUTION CODE [并查集缩点+tarjan无向图求桥]Where are yo ...
- 【BZOJ1051】[HAOI2006]受欢迎的牛
[BZOJ1051][HAOI2006]受欢迎的牛 题面 bzoj 洛谷 题解 假如\(A\)喜欢\(B\)就连一条\(A\)到\(B\)的边 然后缩点,如果图不连通就\(Impossible\) 否 ...
- 牛客练习赛16 C 任意点【并查集/DFS/建图模型】
链接:https://www.nowcoder.com/acm/contest/84/C 来源:牛客网 题目描述 平面上有若干个点,从每个点出发,你可以往东南西北任意方向走,直到碰到另一个点,然后才可 ...
随机推荐
- django开发傻瓜教程-3-celery异步处理
Ref: https://www.jianshu.com/p/6f8576a37a3e https://blog.csdn.net/Demo_3/article/details/78119951 ht ...
- poj2230 欧拉回路
http://poj.org/problem?id=2230 Description Bessie's been appointed the new watch-cow for the farm. E ...
- 【Keras案例学习】 多层感知机做手写字符分类(mnist_mlp )
from __future__ import print_function # 导入numpy库, numpy是一个常用的科学计算库,优化矩阵的运算 import numpy as np np.ran ...
- luogu2387 [NOI2014]魔法森林
这题和水管局长很像,枚举 \(a\) 的边然后维护关于 \(b\) 的最小生成树就可以了. 1A呐>_< #include <algorithm> #include <i ...
- 解决上传app store卡在正在通过iTunes Store鉴定
打开终端输入代码即可 cd ~ mv .itmstransporter/ .old_itmstransporter/ "/Applications/Xcode.app/Contents/Ap ...
- 圣思源Java视频36节练习源码分享(自己的190+行代码对比老师的39行代码)
题目: * 随机生成50个数字(整数),每个数字范围是[10,50],统计每个数字出现的次数 * 以及出现次数最多的数字与它的个数,最后将每个数字及其出现次数打印出来, * 如果某个数字出现次数为0, ...
- ES5新增数组方法(4):every
检查数组元素的每个元素是否符合条件. // 数组中的元素全部满足指定条件返回true let arr = [1, 3, 5, 7, 9]; console.log(arr.every((value, ...
- 西门子S7-200 SMART在win10环境下,使用虚拟机进行网络通信问题一二
原来的笔记本光荣退休,新买了小米笔记本17150.有个项目需要使用西门子S7-200 SMART,结果碰到了很多悲催的事情,新系统下的各种问题. 先贴下计算机配置,如下: 阶段一:安装问题 (1)在w ...
- phpcms v9手机门户配置方法
一.确定一个域名作为你手机wap站点的访问域名,例如:http://m.tezhengzong.com. 接下来在域名管理系统中简析这个域名到你的服务器地址. 二.修改\caches\configs\ ...
- io学习-相关文章
文章:IO编程 地址:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143 ...