蒟蒻样例没过,求调

P1983 [NOIP2013 普及组] 车站分级

Luckies @ 2023-01-05 15:22:25

rt,拓扑排序

#include<bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
int n, m, g[N][N], a[N], in[N], dis[N], maxi;
bool vis[N];
queue<int> q;
int main()
{
    cin >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        int s;
        cin >> s;
        memset(vis, 0, sizeof(vis));
        for (int j = 1; j <= s; j++)
        {
            cin >> a[i];
            vis[a[i]] = true;
        }
        for (int j = a[1]; j <= a[s]; j++)
            if (!vis[j])
                for (int k = 1; k <= s; k++)
                    if (g[i][a[k]] == 0)
                    {
                        g[i][a[k]] = 1;
                        in[a[k]]++;
                    }
    }
    for (int i = 1; i <= n; i++)
    {
        if (in[i] == 0)
        {
            q.push(i);
            dis[i] = 1;
        }
    }
    while (!q.empty())
    {
        int x = q.front();
        q.pop();
        for (int i = 1; i <= n; i++)
        {
            if (g[x][i] == 1)
            {
                in[i]--;
                if (in[i] == 0)
                    q.push(i);
                dis[i] = max(dis[i], dis[x] + 1);
            }
        }
    }
    for (int i = 1; i <= n; i++)
        maxi = max(maxi, dis[i]);
    cout << maxi;
    return 0;
}

by Luckies @ 2023-01-05 16:02:30

球球了,帮我一下吧


|