题面

题解

\(Tarjan\)缩点后统计每个点的出度。

如果有多个点出度为\(0\),就直接输出\(0\),否则输出出度为\(0\)的强连通分量里的点数。

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#define gI gi
#define itn int
#define File(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout) using namespace std; inline int gi()
{
int f = 1, x = 0; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
return f * x;
} int n, m, tot, head[100003], nxt[100003], ver[100003], vis[100003];
int x, y, dfn[100003], low[100003], sta[100003], sy[100003], ys[100003];
int num, cnt, sum, ans, kok;
int cd[100003]; inline void add(int u, int v)
{
ver[++tot] = v, nxt[tot] = head[u], head[u] = tot;
} void Tarjan(int u)//Tarjan缩点
{
dfn[u] = low[u] = ++num; sta[++cnt] = u; vis[u] = 1;
for (int i = head[u]; i; i = nxt[i])
{
int v = ver[i];
if (!dfn[v])
{
Tarjan(v);
low[u] = min(low[u], low[v]);
}
else if (vis[v]) low[u] = min(low[u], dfn[v]);
}
if (dfn[u] == low[u])
{
int y;
++kok;
do
{
y = sta[cnt--];
vis[y] = 0;
sy[y] = kok;
++ys[kok];//统计强连通分量里点的个数
} while (y != u);
}
} int main()
{
//File("P2341");
n = gi(), m = gi();
for (int i = 1; i <= m; i+=1)
{
int u = gi(), v = gi();
add(u, v);
}
for (int i = 1; i <= n; i+=1) if (!dfn[i]) Tarjan(i);
for (int i = 1; i <= n; i+=1)
{
for (int j = head[i]; j; j = nxt[j])
{
int u = ver[j];
if (sy[i] != sy[u])
{
++cd[sy[i]];//统计出度
}
}
}
int fl = 0;
for (int i = 1; i <= kok; i+=1)
{
if (!cd[i])//出度为0
{
if (fl) {puts("0"); return 0;}//有多个出度为0的点,直接输出0
fl = i;//记录答案所在的强连通分量的编号
}
}
printf("%d\n", ys[fl]);//输出答案
return 0;
}

题解【洛谷P2341】 [HAOI2006]受欢迎的牛的更多相关文章

  1. 洛谷 P2341 [HAOI2006]受欢迎的牛 解题报告

    P2341 [HAOI2006]受欢迎的牛 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的"喜欢&q ...

  2. 洛谷——P2341 [HAOI2006]受欢迎的牛//POJ2186:Popular Cows

    P2341 [HAOI2006]受欢迎的牛/POJ2186:Popular Cows 题目背景 本题测试数据已修复. 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所 ...

  3. 洛谷P2341 [HAOI2006]受欢迎的牛 (Tarjan,SCC缩点)

    P2341 [HAOI2006]受欢迎的牛|[模板]强连通分量 https://www.luogu.org/problem/P2341 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就 ...

  4. 【模板】Tarjan缩点,强连通分量 洛谷P2341 [HAOI2006]受欢迎的牛 [2017年6月计划 强连通分量01]

    P2341 [HAOI2006]受欢迎的牛 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的“喜欢”是可以传递的 ...

  5. 【题解】洛谷P2341 [HAOI2006]受欢迎的牛(强连通分量)

    洛谷P2341:https://www.luogu.org/problemnew/show/P2341 前言 这题看错题目 足足花了将近5小时提交了15次 在一位dalao的提醒下才AC了 记得要看清 ...

  6. 洛谷 P2341 [HAOI2006]受欢迎的牛 题解

    今天学了强连通分量的Tarjan算法,做了这道类似于板子题的题(尽管我调了1.5h).主要的思路是用Tarjan缩点之后,求每个点的入度(实际上是出度,因为我是反着连边的).如果 有且只有一个点的入度 ...

  7. 洛谷P2341 [HAOI2006]受欢迎的牛|【模板】强连通分量

    https://www.luogu.org/problem/P2341 缩点之后唯一 一个出度为0的点 #include<cstdio> #include<iostream> ...

  8. 洛谷 P2341 [HAOI2006]受欢迎的牛

    题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的“喜欢”是可以传递的——如果A喜 欢B,B喜欢C,那么A也喜欢C ...

  9. POJ——T2186 Popular Cows || 洛谷——P2341 [HAOI2006]受欢迎的牛

    http://poj.org/problem?id=2186 || https://www.luogu.org/problem/show?pid=2341 Time Limit: 2000MS   M ...

  10. 洛谷 P2341 [HAOI2006]受欢迎的牛|【模板】强连通分量

    题目传送门 解题思路: 先求强联通分量,缩点,然后统计新图中有几个点出度为0,如果大于1个,则说明这不是一个连通图,答案即为0.否则入度为0的那个强连通分量的点数即为答案 AC代码: #include ...

随机推荐

  1. 剑指offer-面试题29-顺时针打印矩阵-矩阵

    /* 题目: 输入一个矩阵,按照从外到内顺时针的顺序依次打印每一个数字. */ /* 思路: 1.将打印矩阵看作是打印一个个从外向内的环. 2.每一个环都有一个起始节点,起始节点的坐标*2小于行数和列 ...

  2. 【14】Softmax回归

    在下面的内容中,我们用C来表示需要分的类数. 最后一层的隐藏单元个数为4,为所分的类的数目,输出的值表示属于每个类的概率. Softmax函数的具体步骤如下图: 简单来说有三步: 计算z值(4×1矩阵 ...

  3. R语言的内存(小总结)

    memory.size()----->查看当前的内存的使用情况. memory.limit()------->当前的工作空间的最大内存容量. ls()-------->查看当前的内存 ...

  4. PAT (Basic Level) Practice (中文)1016 部分A+B (15 分)

    正整数 A 的“D​A​​(为 1 位整数)部分”定义为由 A 中所有 D​A​​ 组成的新整数 P​A​​.例如:给定 8,D​A​​=6,则 A 的“6 部分”P​A​​ 是 66,因为 A 中有 ...

  5. spring-framework核心接口ApplicationContext

    核心接口(ApplicationContext) 继承关系 继承接口: org.springframework.beans.factory.ListableBeanFactory:用于访问应用程序组件 ...

  6. 牛客练习赛53 B题调和级数

    https://ac.nowcoder.com/acm/contest/1114/B 这题时间卡的比较死,多了一个快速幂的logn就过不了这题. #include<bits/stdc++.h&g ...

  7. 51nod(1089 最长回文子串 V2)(hash 加二分)

    1089 最长回文子串 V2(Manacher算法)   回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串. 输入一个字符串Str,输出Str里最长回文子串的长度.   输入 ...

  8. MPI Maelstrom POJ - 1502 floyd

    #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> usin ...

  9. PPTP搭建

    一.装包 yum  -y  install   pptpd-1.4.0-2.el7.x86_64.rpm    //系统光盘不自带,需要自行下载 二.修改配置文件并启动软件 rpm    -qc    ...

  10. [USACO09JAN]Total Flow【网络流】

    Farmer John always wants his cows to have enough water and thus has made a map of the N (1 <= N & ...