Luogu P2419 [USACO08JAN]牛大赛Cow Contest

传递闭包板子,震惊自己之前居然没学过,特此记录一下。

#include <bits/stdc++.h>
using namespace std;

const int N = 110;

int n, m, u, v, to[N][N];

int main () {
    cin >> n >> m;
    for (int i = 1; i <= m; ++i) {
        cin >> u >> v;
        to[u][v] = true;
    }
    for (int k = 1; k <= n; ++k) {
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= n; ++j) {
                to[i][j] = to[i][j] | (to[i][k] & to[k][j]);
                //i -直接-> j or i -间接-> j
            }
        }
    }
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
        int tot = 0; //可确定关系数
        for (int j = 1; j <= n; ++j) {
            if (i == j) continue;
            tot += (to[i][j] | to[j][i]);
        }
        ans += (tot == n - 1);
    }
    cout << ans << endl;
}

原文地址:https://www.cnblogs.com/maomao9173/p/10441030.html

时间: 2024-11-09 09:29:13

Luogu P2419 [USACO08JAN]牛大赛Cow Contest的相关文章

P2419 [USACO08JAN]牛大赛Cow Contest

P2419 [USACO08JAN]牛大赛Cow Contest 海星 这题代码比较短 (哪题Floyd代码长的) 太真实了 直接上代码吧 这题就是一个经典的传递闭包问题 可以用拓扑排序啥的 不过还是Floyd简便一下 原文地址:https://www.cnblogs.com/qf-breeze/p/10473684.html

[USACO08JAN]牛大赛Cow Contest

题目链接: Cow Contest 分析: 听说是一个Floyd求传递闭包 被拓扑的标签骗了进去 首先如果整个图不连通那么显然没办法确定,因为两个连通块之间的信息没有办法传递 所以先并查集判一下 然后考虑拓扑排序,一个点能得到确定的排名当且仅当它能被之前所有入过队的点到达 代码: #include<bits/stdc++.h> #define N (300 + 10) #define M (50000 + 10) using namespace std; inline int read() {

牛大赛Cow Contest

题目背景 [Usaco2008 Jan] 题目描述 N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the competito

[LUOGU] P2886 [USACO07NOV]牛继电器Cow Relays

https://www.luogu.org/problemnew/show/P2886 给定无向连通图,求经过k条边,s到t的最短路 Floyd形式的矩阵乘法,同样满足结合律,所以可以进行快速幂. 离散化大可不必sort+unique,因为我们甚至并不在乎相对大小,只是要一个唯一编号,可以开一个桶记录 之所以进行k-1次快速幂,是因为邻接矩阵本身就走了一步了. #include<iostream> #include<cstring> #include<cstdio> u

Luogu P2886 [USACO07NOV]牛继电器Cow Relays|最短路,倍增

题目链接 题意:给出一张无向连通图,求S到E经过N条边的最短路. 数据范围:边数\(\le 100\),顶点编号\(\le1000\),\(N\le1 \times 10^6\) 题解: 最短路有三种解法,这种数据范围可使用\(floyd\) 可以用\(f[i][j][k]\)表示从\(i\)到\(j\)经过\(k\)条边的最短路,显然TLE 考虑倍增.预处理\(K=2^k\),此时\(f[i][j][k]=min\{f[i][l][k-1] + f[l][j][k-1]\}\) 将\(n\)二

ACM: POJ 3660 Cow Contest - Floyd算法

链接 Cow Contest Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all know, some cows code better than others. Eac

poj 3660 Cow Contest(warshall算法)

poj 3660 Cow Contest Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the co

POJ3660:Cow Contest(Floyd传递闭包)

Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16941   Accepted: 9447 题目链接:http://poj.org/problem?id=3660 Description: N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all k

洛谷P3045 [USACO12FEB]牛券Cow Coupons

P3045 [USACO12FEB]牛券Cow Coupons 71通过 248提交 题目提供者洛谷OnlineJudge 标签USACO2012云端 难度提高+/省选- 时空限制1s / 128MB 提交  讨论  题解 最新讨论更多讨论 86分求救 题目描述 Farmer John needs new cows! There are N cows for sale (1 <= N <= 50,000), and FJ has to spend no more than his budget