[USACO08JAN]牛大赛Cow Contest

题目链接:

Cow Contest

分析:

听说是一个Floyd求传递闭包
被拓扑的标签骗了进去
首先如果整个图不连通那么显然没办法确定,因为两个连通块之间的信息没有办法传递
所以先并查集判一下
然后考虑拓扑排序,一个点能得到确定的排名当且仅当它能被之前所有入过队的点到达

代码:

#include<bits/stdc++.h>
#define N (300 + 10)
#define M (50000 + 10)
using namespace std;
inline int read() {
    int cnt = 0, f = 1; char c = getchar();
    while (!isdigit(c)) {if (c == '-') f = -f; c = getchar();}
    while (isdigit(c)) {cnt = (cnt << 3) + (cnt << 1) + (c ^ 48); c = getchar();}
    return cnt * f;
}
int n, m, first[N], nxt[M], to[M], fa[N], x, y, gone[N][N], total = 0, topo[N], rd[N], tot, cnt;
queue <int> q;
void add(int x, int y) {nxt[++tot] = first[x], first[x] = tot, to[tot] = y;}
int get_fa(int x) {return x == fa[x] ? x : fa[x] = get_fa(fa[x]);}
void tpsort() {
    for (register int i = 1; i <= n; ++i) if (!rd[i]) q.push(i), topo[++total] = i;
//  if (q.size() == 1) ++cnt;
    while (!q.empty()) {
        int u = q.front(); q.pop();
        bool flag = 0;
        for (register int i = 1; i <= total; ++i) if (!gone[u][topo[i]]) flag = 1;
        if (!flag && !q.size()) ++cnt;
        for (register int i = first[u]; i; i = nxt[i]) {
            int v = to[i];
            --rd[v];
            for (register int i = 1; i <= n; ++i) gone[v][i] |= gone[u][i];
            if (!rd[v]) q.push(v), topo[++total] = v;
        }
    }
}
int main() {
//  freopen("1.in", "r", stdin);
    n = read(), m = read();
    for (register int i = 1; i <= n; ++i) fa[i] = i, gone[i][i] = 1;
    for (register int i = 1; i <= m; ++i) {
        x = read(), y = read();
        add(x, y), ++rd[y], fa[get_fa(y)] = get_fa(x);
    }
    for (register int i = 1; i <= n; ++i) if (fa[i] == i) ++cnt;
    if (cnt > 1) return printf("0"), 0;
    cnt = 0;
    tpsort();
    printf("%d", cnt);
    return 0;
}

原文地址:https://www.cnblogs.com/kma093/p/11791381.html

时间: 2024-08-30 14:48:29

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

P2419 [USACO08JAN]牛大赛Cow Contest

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

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

牛大赛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

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

洛谷 P3014 [USACO11FEB]牛线Cow Line

P3014 [USACO11FEB]牛线Cow Line 题目背景 征求翻译.如果你能提供翻译或者题意简述,请直接发讨论,感谢你的贡献. 题目描述 The N (1 <= N <= 20) cows conveniently numbered 1...N are playing yet another one of their crazy games with Farmer John. The cows will arrange themselves in a line and ask Far

洛谷 P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver

P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver 题目描述 The cows are out exercising their hooves again! There are N cows jogging on an infinitely-long single-lane track (1 <= N <= 100,000). Each cow starts at a distinct position on the track, and some cows jog at