被普及组的题吓到了……
其实是我对拓扑排序的理解不够.
这个题可以转化成一个求最大层次的问题.理论上可以暴力建树然后求深度, 也可以用拓扑排序.
然后注意一下建图时不要重边就好了. 考虑到这个题目的需求和规模, 我们使用矩阵存图.
#include#include #include #include #include #include using namespace std;typedef pair P;const int MAXN = 1e3 + 20;inline int read(){ int x = 0; char ch = getchar(); while(!isdigit(ch)) ch = getchar(); while(isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); return x;}int N, M;bool g[MAXN][MAXN];int ideg[MAXN];int bfs(){ queue q; for(int i = 1; i <= N; i++) if(!ideg[i]) q.push(P(i, 1)); int ans = 1; while(!q.empty()) { P p = q.front(); q.pop(); ans = max(ans, p.second); int u = p.first; for(int i = 1; i <= N; i++) if(g[u][i]){ --ideg[i]; if(!ideg[i]) q.push(P(i, p.second + 1)); } } return ans;}bool vis[MAXN];vector
edges;int main(){ cin>>N>>M; for(int i = 1, s, t, j; i <= M; i++){ memset(vis, false, sizeof(vis)); edges.clear(); j = read() - 1, s = read(), vis[s] = true; while(--j, j) vis[read()] = true; t = read(), vis[t] = true; for(j = s; j <= t; j++) if(!vis[j]) edges.push_back(j); for(j = s; j <= t; j++) if(vis[j]) for(int k = 0; k < (int) edges.size(); k++) if(!g[j][edges[k]]) g[j][edges[k]] = true, ++ideg[edges[k]]; } cout<<