zoukankan      html  css  js  c++  java
  • P1983 车站分级

    题目链接

    题目描述

    方法:

    topo

    每趟列车,没有停靠的车站(u)的等级必然小于停靠车站(v)的等级,所以u,v连边,拓扑排序算level。

    Code:

    #include <bits/stdc++.h>
    # define LL long long
    using namespace std;
    
    const int maxn=1000+10;
    int n,m;
    int visited[maxn];
    int ind[maxn];
    int connected[maxn][maxn];
    struct Edge{
        int to,next;
    }e[1000000];
    int head[maxn];
    int en;
    int level[maxn];
    int s[maxn];
    
    void add(int from, int to){
        e[en].next=head[from];
        e[en].to=to;
        head[from]=en;
        ++en;
    }
    
    int topo(){
        int maxlevel=1;
        queue<int> q;
        for(int i=1;i<=n;++i){
            if(ind[i]==0){
                q.push(i);
                level[i]=1;
            }
        }
        while(!q.empty()){
            int u=q.front();
            q.pop();
            for(int i=head[u];i!=-1;i=e[i].next){
                int v=e[i].to;
                --ind[v];
                level[v]=max(level[v],1+level[u]);
                if(ind[v]==0){
                    q.push(v);
                    maxlevel=max(maxlevel,level[v]);
                }
            }
        }
        return maxlevel;
    }
    
    int main(){
        memset(head,-1,sizeof(head));
        scanf("%d %d", &n, &m);
        for(int i=1;i<=m;++i){
            int cnt;
            memset(visited,0,sizeof(visited));
    
            scanf("%d", &cnt);
            for(int j=1;j<=cnt;++j){
                int cur;
                scanf("%d", &cur);
                s[j]=cur;
                visited[cur]=1;
            }
            for(int j=s[1];j<=s[cnt];++j){
                if(visited[j]==1) continue;
                for(int k=1;k<=cnt;++k){
                    if(connected[j][s[k]]==0){
                        connected[j][s[k]]=1;
                        add(j,s[k]);
                        ind[s[k]]++;
                    }
                }
            }
        }
        int res=topo();
        printf("%d", res);
        return 0;
    }
  • 相关阅读:
    Codeforces Round #369 (Div. 2)
    poj3189二分图多重匹配
    a 标签传值
    phpStudy 虚拟主机
    wampserver 虚拟主机
    $file函数
    PHP脚本运行时间
    查询timestamp类型数据
    驼峰法
    easyUI导出数据
  • 原文地址:https://www.cnblogs.com/FEIIEF/p/12244105.html
Copyright © 2011-2022 走看看