提交时间:2026-06-12 15:45:06
运行 ID: 91432
// Problem: P1983 [NOIP 2013 普及组] 车站分级 // Contest: Luogu // URL: https://www.luogu.com.cn/problem/P1983 // Memory Limit: 128 MB // Time Limit: 1000 ms // Author : roammer // Source : https://www.luogu.com.cn/training/1023724#problems // Powered by CP Editor (https://cpeditor.org) #include <cstdio> #include <cstring> #include <queue> #define max(a, b) (((a) > (b)) ? (a) : (b)) int n, m; int x[1010]; bool stand[1010]; bool edged[1010][1010]; std::vector<int> graph[1010]; int degree[1010]; int order[1010]; int level[1010]; void init() { for(int i = 1; i <= n; i++) for(auto u : graph[i]) degree[u]++; } void toposort() { std::queue<int> q; init(); for(int i = 1; i <= n; i++) if(degree[i] == 0) { q.push(i); level[i] = 1; } while(!q.empty()) { for(auto u : graph[q.front()]) { degree[u]--; level[u] = max(level[q.front()] + 1, level[u]); if(degree[u] == 0) { q.push(u); } } q.pop(); } } int main() { scanf("%d %d", &n, &m); for(int i = 1; i <= m; i++) { memset(stand, false, sizeof(stand)); int sz; scanf("%d", &sz); for(int j = 1; j <= sz; j++) { scanf("%d", &x[j]); stand[x[j]] = true; } for(int j = x[1]; j <= x[sz]; j++) if(!stand[j]) for(int k = 1; k <= sz; k++) if(!edged[j][x[k]]) { graph[j].push_back(x[k]); edged[j][x[k]] = true; } } toposort(); int ans = 0; for(int i = 1; i <= n; i++) ans = max(ans, level[i]); printf("%d", ans); return 0; }