1 solutions

  • 0
    @ 2024-1-17 16:28:20

    车站分级

    题意: 有n个公交车站,有m个公交车线路,在这m个公交车线路中位于a~b的一条线路,如果公交车在c号点停留了,那么在这条线路中所有>=c点级别的公交车站都必须停。

    分析: 如果公交车的运行线路是 1 3 5 6 那么 1 3 5 6的级别肯定都比2,4高 1,3,5,6>=2,4 同理我们就可以把这个不等式关系转化成边的关系,即u-v的一条边表示u的级别比v高一级 那么我们可以画出下面的一幅图 image 可以看见这张图被分成了两层,最外的一层是1,3,5,6 里面的一层是2,4。即这个车站有2级。

    此时我们就把要求车站的等级数 ---> 图中的最大层数

    使用拓扑排序就可以解决了

    #include<bits/stdc++.h>
    using namespace std;
    const int N=1e4+12,M=1e6+12;
    int head[N],ans,cnt,n,m,l,u,v;
    int a[N],d[N];
    int use[N][N];
    struct{
      int to,nex,w;
    }edge[M];
    
    queue<int>q;
    
    void addedge(int u,int v,int w)
    {
      cnt++;
      edge[cnt]={ v,head[u],w };
      head[u]=cnt;
    }
    
    void toop()
    {
      while( q.size() )
      {
        int x=q.front();
        //cout<<x<<" "<<d[x]<<endl;
        q.pop();
        ans=max(ans,d[x]);
        for(int i=head[x];i;i=edge[i].nex)
        {
          int y=edge[i].to,w=edge[i].w;
          a[y]--;
          d[y]=max( d[y],d[x]+w );
          if(!a[y]){
            q.push( y );
          }
        }
      }
    }
    
    int main(){
      scanf("%d%d",&n,&m);
      while( m-- )
      {
        scanf("%d",&l);
        scanf("%d",&u);
        queue<int>l1,l2;
        l1.push(u);
        for(int i=2;i<=l;i++)
        {
          scanf("%d",&v);
          for( int j=u+1;j<v;j++ )
          {
            l2.push(j);
          }
          l1.push(v);
          u=v;
        }
        while( l1.size() )
        {
          int len=l2.size();
          int x=l1.front();
          l1.pop();
          while( len-- )
          {
            int y=l2.front();
            l2.push(y);
            l2.pop();
            if( use[x][y] ) continue;
            use[x][y]=1;
            addedge( x,y,1 );
            a[y]++;
            //cout<<y<<endl;
          }
    
        }
      }
      for(int i=1;i<=n;i++)
      {
        if( !a[i] )
        {
          //cout<<"!!"<<i<<endl;
          q.push( i );
          d[i]=1;
        }
      }
      toop();
      printf("%d",ans);
    }
    
    • 1

    Information

    ID
    10542
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    10
    Tags
    # Submissions
    3
    Accepted
    1
    Uploaded By