【luogu P2341 [HAOI2006]受欢迎的牛】 题解
题解报告:https://www.luogu.org/problemnew/show/P2341
我们把图中的强连通分量缩点,然后只有出度为0的牛是受欢迎的,这样如果出度为0的牛只有一个,说明受所有牛欢迎。否则出度为0只是受一些牛欢迎。
#include <stack>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 100000 + 10;
struct edge{
int next, to, from, len;
}e[maxn<<2];
int head[maxn], cnt;
int n, m, color[maxn], tong[maxn], du[maxn], num, tim, ans, tmp;
int dfn[maxn], low[maxn];
bool vis[maxn];
stack<int> s;
void add(int u, int v)
{
e[++cnt].from = u;
e[cnt].to = v;
e[cnt].next = head[u];
head[u] = cnt;
}
void tarjan(int x)
{
dfn[x] = low[x] = ++tim;
s.push(x); vis[x] = 1;
for(int i = head[x]; i != -1; i = e[i].next)
{
int v = e[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])
{
color[x] = ++num;
vis[x] = 0;
while(s.top() != x)
{
color[s.top()] = num;
vis[s.top()] = 0;
s.pop();
}
s.pop();
}
}
int main()
{
memset(head, -1, sizeof(head));
scanf("%d%d",&n,&m);
for(int i = 1; i <= m; i++)
{
int u, v;
scanf("%d%d",&u,&v);
add(u,v);
}
for(int i = 1; i <= n; i++)
if(!dfn[i]) tarjan(i);
for(int i = 1; i <= n; i++)
{
for(int j = head[i]; j != -1; j = e[j].next)
{
int v = e[j].to;
if(color[v] != color[i])
{
du[color[i]]++;
}
}
tong[color[i]]++;
}
for(int i = 1; i <= num; i++)
{
if(du[i] == 0)
{
tmp++;
ans = tong[i];
}
}
if(tmp == 0)
{
printf("0");
return 0;
}
if(tmp == 1)
{
printf("%d",ans);
return 0;
}
if(tmp > 1)
{
printf("0");
return 0;
}
}
【luogu P2341 [HAOI2006]受欢迎的牛】 题解的更多相关文章
- [Luogu P2341] [HAOI2006]受欢迎的牛 (缩点+bitset)
题面 传送门:https://www.luogu.org/problemnew/show/P2341 Solution 前排提示,本蒟蒻做法既奇葩又麻烦 我们先可以把题目转换一下. 可以把一头牛喜欢另 ...
- #P2341 [HAOI2006]受欢迎的牛 题解
题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的“喜欢”是可以传递的——如果A喜 欢B,B喜欢C,那么A也喜欢C ...
- Luogu P2341 [HAOI2006]受欢迎的牛 SCC缩点
把强连通分量缩点,如果有且仅有一个出度为0的强连通分量,那么答案就是他的size:如果有多个入度为0的,那么没有明星牛. #include<cstdio> #include<iost ...
- Luogu P2341 [HAOI2006]受欢迎的牛
这道题应该也是经典的SCC题了吧 印象中不知道在在班里上课的时候在紫书,ACM竞赛的那些书上看到多少次(有点奇怪) 首先思路很明显,就是要找出有多少个点,以它们为起点可以遍历整个图 首先考虑一种情况, ...
- 洛谷 P2341 [HAOI2006]受欢迎的牛 题解
今天学了强连通分量的Tarjan算法,做了这道类似于板子题的题(尽管我调了1.5h).主要的思路是用Tarjan缩点之后,求每个点的入度(实际上是出度,因为我是反着连边的).如果 有且只有一个点的入度 ...
- P2341 [HAOI2006]受欢迎的牛(更完)
P2341 [HAOI2006]受欢迎的牛 题解 tarjan 缩点板子题 如果 A 稀饭 B,那就 A 向 B 连边,构造出一个有向图 如果这个有向图里有强连通分量,也就说明这个强连通分量里的所有奶 ...
- 洛谷P2341 [HAOI2006]受欢迎的牛 (Tarjan,SCC缩点)
P2341 [HAOI2006]受欢迎的牛|[模板]强连通分量 https://www.luogu.org/problem/P2341 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就 ...
- 洛谷 P2341 [HAOI2006]受欢迎的牛 解题报告
P2341 [HAOI2006]受欢迎的牛 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的"喜欢&q ...
- [Luogu 2341] HAOI2006 受欢迎的牛
[Luogu 2341] HAOI2006 受欢迎的牛 智能推的水题,一看是省选题就给做了,做一半才发现 Tarjan 算法忘干净了. Tarjan 求出SCC,算出每一个 SCC 包含原图的点数(s ...
随机推荐
- [转]how to use both JDK 7 and JDK 8 in one build
Note: This article is original from https://gist.github.com/aslakknutsen/9648594 JDK 8 Released Most ...
- BNU 20950 ——沉重的货物 —————— · 最短路、最短边最大化」
沉重的货物 Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java class name: ...
- nyoj 1197——你会加吗?——————【快速幂、分治】
你会加吗? 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 给出两个整数A和N,计算(A + A^2 + A^3 + …… + A^(N - 1) + A^N)% 6 ...
- IE9下JQuery发送ajax请求失效
最近在做项目的时候,测试PC端网页,在IE9下会失效,不能正常的发送POST请求,经过仔细的排查,发现是IE9下JQuery发送ajax存在跨域问题. 目前有两种解决方案: 解决方案一: 设置浏览 ...
- python搭建本地服务器
python搭建本地服务器 python3以上版本 'python3 -m http.server 8000' 默认是8000端口,可以指定端口,打开浏览器输入http://127.0.0.1:800 ...
- ef linq 查询某时间段内数据 踩的坑
var now = DateTime.Now;var list =db.Jinbi_TypeLimit.Where(x => x.IsAvailable && x.JinbiTy ...
- jquery.rotate.js可选抽奖次数和中奖内容的转盘抽奖demo
需求: 最多可以抽奖5次,而且,每次只会中“2000元理财金”或者“谢谢参与”,其它的不会抽中(哈哈,果然都是套路). 效果如下: 一.页面结构: <div class="g-cont ...
- Eclipse org.eclipse.compare plug-in
Plug-in metedata tell eclipse runtime kernel how to create a expected object and set its perperties, ...
- Format - DateTime
1. Long Date/Short Date/Long Time/Short Time,可以在系统的“Region and Language”中找到相应设置: 2. ISO Format/Local ...
- conversion vs recommendation
conversion vs recommendation: http://markdisomma.com/2011/06/16/conversation-vs-recommendation/