!-- Loading 底层遮罩 -->

P1983 车站分级

感谢所有AC

 

传送门

思路

     层次分明,有优先级---拓扑排序。读入数据要考虑是否有重边问题,有重边需要特殊处理,重边影响入度导致TLE。

代码

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#define MAX 1010
using namespace std;
bool vis[MAX], map[MAX][MAX];
int In[MAX], Value[MAX], Pre[MAX];
vector<int> Plant[MAX];
queue<int> Q; int ans = 0;
int main(void)
{
    int n = 0, m = 0, N = 0, M = 0;
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= m; i++)
    {
        scanf("%d", &N);
        for (int i = 1; i <= N; i++)
        {
            scanf("%d", &M);
            vis[M] = true;
            Pre[i] = M;
        }
        for (int i = Pre[1]; i <= Pre[N]; i++)
        {
            if (!vis[i]) {
                for (int j = 1; j <= N; j++)
                {
                    if (!map[i][Pre[j]])
                    {
                        Plant[i].push_back(Pre[j]);
                        In[Pre[j]]++;
                        map[i][Pre[j]] = true;
                    }
                }
            }
        }
        memset(vis, false, sizeof(vis));
    }
    for (int i = 1; i <= n; i++)
        if (!In[i]) { Q.push(i); Value[i] = 1; }
    while (!Q.empty()) {
        int pos = Q.front(); Q.pop();
        for (int i = 0; i < Plant[pos].size(); i++)
        {
            Value[Plant[pos][i]] = max(Value[pos] + 1, Value[Plant[pos][i]]);
            ans = max(ans, Value[Plant[pos][i]]);
            if (--In[Plant[pos][i]]==0) {
                Q.push(Plant[pos][i]);
            }
        }
    }
    cout << ans;
    return 0;
}
 

 

posted @ 2022-03-30 22:07  Thinker-X  阅读(34)  评论(0)    收藏  举报