HAOI2006受欢迎的牛——缩点
题目:http://poj.org/problem?id=2186
本题是缩点模板题,将强连通分量缩成一个点,从而形成一个有向无环图,当仅有一个出度为0的点时答案即此点的大小,否则无解。
代码如下:
#include<iostream>
#include<cstdio>
using namespace std;
int n,m,a,b,dfn[10005],low[10005],ct,cnt[10005],hp[10005],p,s[10005],ans,tim;
int col[10005],cr,rd[10005],cd[10005],siz[10005];
bool vis[10005],lin[10005][10005],fl[10005];
struct N{
int hd;int to;int next;
}edge[50005];
void add(int x,int y)
{
ct++;
edge[ct].hd=x;
edge[ct].to=y;
edge[ct].next=cnt[x];
cnt[x]=ct;
}
void tarjan(int x)
{
vis[x]=1;
hp[++p]=x;
dfn[x]=++tim;low[x]=dfn[x];
for(int i=cnt[x];i;i=edge[i].next)
{
int v=edge[i].to;
if(!dfn[v])
{
tarjan(v);
low[x]=min(low[x],low[v]);
}
else if(vis[v])
{
low[x]=min(low[x],low[v]);
}
}
if(dfn[x]==low[x])
{
cr++;
vis[x]=0;
int k=0;
while(hp[p]!=x)
{
vis[hp[p]]=0;
col[hp[p]]=cr;
p--;
k++;
}
col[x]=cr;
p--;k++;
s[cr]=k;
siz[cr]=k;
}
}
void ser(int nw,int jl)
{
s[nw]+=jl;
if(!cd[nw])return;
int k=0;
for(int i=1;i<=n;i++)
{
if(k==cd[nw])break;
if(lin[nw][i])
{
if(!fl[nw])ser(i,s[nw]);//不是jl+s[nw]!
else ser(i,jl);
k++;
}
}
fl[nw]=1;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
add(a,b);
}
for(int i=1;i<=n;i++)
if(!dfn[i])tarjan(i);
for(int i=1;i<=ct;i++)
{
int u=edge[i].hd;
int v=edge[i].to;
if(col[u]!=col[v])
lin[col[u]][col[v]]=1,rd[col[v]]++,cd[col[u]]++;
}
int kt=0;int bh;
for(int i=1;i<=cr;i++)
if(!cd[i])kt++,bh=i;
if(kt==1)printf("%d",siz[bh]);
else printf("0");
// for(int i=1;i<=cr;i++)
// if(!rd[i])ser(i,0);
// for(int i=1;i<=n;i++)
// if(s[col[i]]>=n)ans++;//>=
// printf("%d",ans);
return 0;
}
其中注释掉的部分以及dfs的部分是不使用上述结论的暴力枚举;
但有一出不解:即第96行判断条件若是“==n”则有些有解的情况输出0导致WA,不知何故。
HAOI2006受欢迎的牛——缩点的更多相关文章
- BZOJ 1051: [HAOI2006]受欢迎的牛 缩点
1051: [HAOI2006]受欢迎的牛 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/ ...
- [Luogu P2341] [HAOI2006]受欢迎的牛 (缩点+bitset)
题面 传送门:https://www.luogu.org/problemnew/show/P2341 Solution 前排提示,本蒟蒻做法既奇葩又麻烦 我们先可以把题目转换一下. 可以把一头牛喜欢另 ...
- HAOI2006 受欢迎的牛 缩点
不难分析出我们就是要求是否有唯一一个出度为0的强连通分量. Code: #include<cstdio> #include<stack> #include<algorit ...
- bzoj 1051: [HAOI2006]受欢迎的牛 tarjan缩点
1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2092 Solved: 1096[Submit][Sta ...
- P2341 [HAOI2006]受欢迎的牛(tarjan+缩点)
P2341 [HAOI2006]受欢迎的牛 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的“喜欢”是可以传递的 ...
- [Bzoj1051][HAOI2006]受欢迎的牛(缩环)
1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6676 Solved: 3502[Submit][Sta ...
- 洛谷P2341 [HAOI2006]受欢迎的牛 (Tarjan,SCC缩点)
P2341 [HAOI2006]受欢迎的牛|[模板]强连通分量 https://www.luogu.org/problem/P2341 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就 ...
- 【bzoj1051】 [HAOI2006]受欢迎的牛 tarjan缩点判出度算点数
[bzoj1051] [HAOI2006]受欢迎的牛 2014年1月8日7450 Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B ...
- 【模板】Tarjan缩点,强连通分量 洛谷P2341 [HAOI2006]受欢迎的牛 [2017年6月计划 强连通分量01]
P2341 [HAOI2006]受欢迎的牛 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的“喜欢”是可以传递的 ...
随机推荐
- FileOutPutStream 的写操作
package xinhuiji_day07; import java.io.File;import java.io.FileNotFoundException;import java.io.File ...
- html中文件类型的accept属性有哪些
*.3gpp audio/3gpp, video/3gpp 3GPP Audio/Video *.ac3 audio/ac3 AC3 Audio *.asf allpication/vnd.ms-as ...
- JVM完全指南
JVM完全指南 一:虚拟机内存图解 JAVA程序运行与虚拟机之上,运行时需要内存空间.虚拟机执行JAVA程序的过程中会把它管理的内存划分为不同的数据区域方便管理. 虚拟机管理内存数 ...
- MySQL CREATE TRIGGER (1)
CREATE TRIGGER语法 CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW ...
- python 基础 4.1 函数的参数
#/usr/bin/python #coding=utf-8 #@Time :2017/10/24 9:09 #@Auther :liuzhenchuan #@File :函数的参数.py # ...
- python 基础 2.8 python练习题
python 练习题: #/usr/bin/python #coding=utf-8 #@Time :2017/10/26 9:38 #@Auther :liuzhenchuan #@File ...
- Linq Group By 多个字段
var counts = dal.QueryStatisticsCount(condition); var result = from p in counts group p by new { Auc ...
- iOS 10 权限配置大全
<!-- 相册 --> <key>NSPhotoLibraryUsageDescription</key> <string>App需要您的同意,才能访问 ...
- 我的Android进阶之旅------>Ubuntu下不能识别Android设备的解决方法
Bus 001 Device 006: ID 1b20:0c81 MStar Semiconductor, Inc. 今天不知道Ubuntu发了什么疯,昨天还用的好好的,今天就突然不能识别我 ...
- 流畅python学习笔记:第二十章:属性描述符:
在前面一章中介绍了@property的用法,但是存在一个问题,如果我有多个属性想转变成property特性,那不是针对每个都需要实现一个 @propery.setter 和 @property.get ...