70pts, TLE求助
查看原帖
70pts, TLE求助
1537245
mmd7171楼主2025/2/5 23:48

#7#8#9超时

代码如下

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;

vector< unordered_set<int> > a(maxn), b(maxn);
unordered_set<int> tmp;
queue<int> q;
int n, m, ans, in[maxn], f[maxn];
bool vis[maxn];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    scanf("%d %d",&n,&m);
    for (int i = 1; i <= m; i++) {
        int t, head = 0, tail = 0;
        scanf("%d", &t);
        tmp.clear();
        for (int j = 0; j < t; j++) {
            int k;
            scanf("%d", &k);
            if (j == 0) head = k;
            else if (j == t - 1) tail = k;
            tmp.insert(k);
        }
        if (tmp.size() == n) continue;
        for (int j = head + 1; j < tail; j++) {
            if (tmp.count(j) == 0) {
                for (auto &k : tmp) {
                    if (!b[j].count(k)) in[j]++;
                    b[j].insert(k);
                    a[k].insert(j);
                }
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        f[i] = 1;
        if (!in[i]) q.push(i);
    }
    while(!q.empty()) {
        int x = q.front();
        vis[x] = true;
        q.pop();
        for (auto &k : a[x]) {
            if (!vis[k]) {
                f[k] = max(f[x] + 1, f[k]);
                in[k]--;
                if (!in[k]) q.push(k);
            }
        }
    }
    for (int i = 1; i <= n; i++)
        ans = max(ans, f[i]);
    printf("%d", ans);

    return 0;
}
2025/2/5 23:48
加载中...