洛谷- P1983 车站分级 (拓扑思想)

题目传送
题意:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
思路
由题意可知,起点和终点之间的没有停的站台点一定是比停了的站台点的级数低,所以这里就有一个大小关系,我们可以把没有停的站台点到已经停了的站台点之间连一条边。然后最后利用拓扑的思想来判断最多有几级。

AC代码

#include <bits/stdc++.h>
inline long long read(){char c = getchar();long long x = 0,s = 1;
while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}
return x*s;}
using namespace std;
#define NewNode (TreeNode *)malloc(sizeof(TreeNode))
#define Mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) (x)&(-x)
const int N = 2e5 + 10;
const long long INFINF = 0x7f7f7f7f7f7f7f;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
const int mod = 1e9+7;
const double II = acos(-1);
const double PP = (II*1.0)/(180.00);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> piil;
int n,m,Max = 1;
vector<int> v[1005];
queue<pii> q;
int ru[1005],ans[1005][1005];
void TOPU()
{
    for(int i = 1;i <= n;i++)
        if(ru[i] == 0)
            q.push(make_pair(i,1));
    while(!q.empty())
    {
        int a = q.front().first,b = q.front().second;
        q.pop();
        for(int i = 0;i < v[a].size();i++)
        {
            ru[v[a][i]]--;
            if(ru[v[a][i]] == 0)
            {
                q.push(make_pair(v[a][i],b+1));
                Max = max(Max,b+1);
            }
        }
    }
}
signed main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    //    freopen("input.txt","r",stdin);
    //    freopen("output.txt","w",stdout);
    cin >> n >> m;
    
    for(int i = 1,num;i <= m;i++)
    {
        
        cin >> num;
        int vis[n+5] = {0},arr[n+5] = {0};
        for(int j = 1;j <= num;j++)
        {
            cin >> arr[j];
            vis[arr[j]] = 1;
        }
        for(int j = arr[1];j <= arr[num];j++)
        {
            if(vis[j]) continue;
            for(int k = 1;k <= num;k++)
            {
                if(ans[j][arr[k]]) continue;
                v[j].push_back(arr[k]);
                ru[arr[k]]++;
                ans[j][arr[k]] = 1;
            }
        }
    }
    TOPU();
    cout << Max << endl;
}

猜你喜欢

转载自blog.csdn.net/moasad/article/details/107155526