拓扑排序玄关求调

P1983 [NOIP2013 普及组] 车站分级

Qianmo_su @ 2024-11-06 00:59:21

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

vector<int> g[N];
int n,m,ans=1;
int in[N],t[N],depth[N];
bool stop[N];

int main()
{
    int n,m;
    cin >> n >> m;
    for(int i=1;i<=m;i++)
    {
        memset(stop,false,sizeof(stop));
        memset(t,0,sizeof(t));
        memset(in,0,sizeof(in));
        memset(depth,0,sizeof(depth));
        for(int i=0;i<N;i++) g[i].clear();
        int s;
        cin >> s;
        for(int j=1;j<=s;j++)
        {
            cin >> t[j];
            stop[t[j]] = true;
        }
        for(int j=1;j<=s;j++)
            for(int k=2;k<n;k++)
                if(!stop[k])
                {
                    g[t[j]].push_back(k);
                    in[k]++;
                }
        queue<int> q;
        for(int j=1;j<=n;j++)
            if(in[j] == 0)
            {
                q.push(j);
                depth[j] = 1;
            }
        while(!q.empty())
        {
            int t = q.front();
            q.pop();
            for(auto x:g[t])
            {
                depth[x] = depth[t]+1;
                ans = max(ans,depth[x]);
                in[x]--;
                if(in[x] == 0) q.push(x);
            }
        }
    }
    cout << ans;
    return 0;
}

by Qianmo_su @ 2024-11-06 13:45:20

已结


|