[BZOJ] 1051: [HAOI2006]受欢迎的牛
2017-10-07 18:16
381 查看
1051: [HAOI2006]受欢迎的牛
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6342 Solved: 3322
[Submit][Status][Discuss]
Description
每一头牛的愿望就是变成一头最受欢迎的牛。现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎。 这 种关系是具有传递性的,如果A认为B受欢迎,B认为C受欢迎,那么牛A也认为牛C受欢迎。你的任务是求出有多少头 牛被所有的牛认为是受欢迎的。Input
第一行两个数N,M。 接下来M行,每行两个数A,B,意思是A认为B是受欢迎的(给出的信息有可能重复,即有可 能出现多个A,B)Output
一个数,即有多少头牛被所有的牛认为是受欢迎的。
Sample Input
3 31 2
2 1
2 3
Sample Output
1HINT
100%的数据N<=10000,M<=50000
Source
Analysis
首先强连通分量缩点,于是这个图肯定成为一个DAG
出度为零的强连通分量就是答案啦
当然,如果有多个点为答案,那么就是错的,受欢迎的牛只有0头
为啥?
#include<cstdio> #include<iostream> #define maxn 1000000 using namespace std; int n,m,de[maxn],ret,ans; int dfn[maxn],low[maxn],TIM,stack[maxn],pos,COL,buck[maxn],color[maxn]; bool vis[maxn]; struct edge{ int from,u,v; }e[maxn],E[maxn]; int tot,first[maxn],toT,firsT[maxn]; void insert(int u,int v){ tot++; e[tot].from = first[u]; e[tot].u = u; e[tot].v = v; first[u] = tot; } void inserT(int u,int v){ toT++; E[toT].from = firsT[u]; E[toT].u = u; E[toT].v = v; firsT[u] = toT; } void tarjan(int now){ dfn[now] = low[now] = ++TIM; stack[pos++] = now; vis[now] = true; for(int i = first[now];i;i = e[i].from){ int v = e[i].v; if(!dfn[v]){ tarjan(v); low[now] = min(low[now],low[v]); }else if(vis[v]) low[now] = min(low[now],dfn[v]); }if(dfn[now] == low[now]){ COL++; while(stack[pos-1] != now && pos){ buck[COL]++; color[stack[pos-1]] = COL; vis[stack[--pos]] = false; }color[now] = COL; vis[now] = false; pos--; buck[COL]++; } } int main(){ scanf("%d%d",&n,&m); for(int i = 1;i <= m;i++){ int x,y; scanf("%d%d",&x,&y); insert(x,y); }for(int i = 1;i <= n;i++) if(!dfn[i]) tarjan(i); for(int i = 1;i <= tot;i++){ int u = e[i].u,v = e[i].v; if(color[u] != color[v]){ inserT(color[u],color[v]); de[color[u]]++; } } for(int i = 1;i <= COL;i++){ if(!de[i]) ret++,ans = buck[i]; } if(ret > 1) cout << 0; else cout << ans; return 0; }qwq一遍A那才叫爽!
相关文章推荐
- 【bzoj 1051】 [HAOI2006]受欢迎的牛 强联通分量
- bzoj 1051: [HAOI2006]受欢迎的牛 tarjan缩点
- 【BZOJ 1051】[HAOI2006]受欢迎的牛
- bzoj1051: [HAOI2006]受欢迎的牛
- [BZOJ]1051: [HAOI2006]受欢迎的牛 强连通
- BZOJ 1051: [HAOI2006]受欢迎的牛 强连通缩点
- 【Bzoj1051】 [HAOI2006]受欢迎的牛
- BZOJ 1051: [HAOI2006]受欢迎的牛( tarjan )
- [BZOJ1051] [HAOI2006]受欢迎的牛(强连通)
- bzoj1051: [HAOI2006]受欢迎的牛
- [BZOJ 1051][HAOI2006]受欢迎的牛:强连通分量
- BZOJ1051 [HAOI2006] 受欢迎的牛
- bzoj1051 [HAOI2006]受欢迎的牛
- bzoj 1051: [HAOI2006]受欢迎的牛
- 洛谷 P2341 BZOJ 1051 [HAOI2006]受欢迎的牛
- bzoj1051: [HAOI2006]受欢迎的牛(tarjan强连通分量)
- 【连通分量】BZOJ 1051:[HAOI2006]受欢迎的牛
- [BZOJ]1051: [HAOI2006]受欢迎的牛
- [BZOJ1051] [HAOI2006] 受欢迎的牛 - tarjan强连通分量
- BZOJ1051 [HAOI2006]受欢迎的牛(强连通分量+缩点)