zoukankan      html  css  js  c++  java
  • 旅行计划

    题目描述

    小明要去一个国家旅游。这个国家有N个城市,编号为1至N,并且有M条道路连接着,小明准备从其中一个城市出发,并只往东走到城市i停止。

    所以他就需要选择最先到达的城市,并制定一条路线以城市i为终点,使得线路上除了第一个城市,每个城市都在路线前一个城市东面,并且满足这个前提下还希望游览的城市尽量多。

    现在,你只知道每一条道路所连接的两个城市的相对位置关系,但并不知道所有城市具体的位置。现在对于所有的i,都需要你为小明制定一条路线,并求出以城市ii为终点最多能够游览多少个城市。

    输入格式

    1行为两个正整数N, M

    接下来M行,每行两个正整数x,y,表示了有一条连接城市x与城市y的道路,保证了城市x在城市y西面。

    输出格式

    N行,第i行包含一个正整数,表示以第i个城市为终点最多能游览多少个城市。

    输入输出样例

    输入 #1
    5 6
    1 2
    1 3
    2 3
    2 4
    3 4
    2 5
    输出 #1
    1
    2
    3
    4
    3

    说明/提示

    均选择从城市1出发可以得到以上答案。

    对于20%的数据,N100;

    对于60%的数据,N1000;

    对于100%的数据,N100000,M200000。

    分析:

    本题首先需要一次拓补排序,如果不懂拓补排序看这篇博客(我觉得写的比较好):点这儿

    然后我们就可以愉快地DP了,即对于每条边u--->v,有f[v]=max(f[v],f[u]+1)。

    CODE:

     1 #include<cmath>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<iostream>
     5 #include<algorithm>
     6 #include<queue>
     7 using namespace std;
     8 const int M=200005;
     9 int n,m;
    10 int head[M],to[M],next[M],tot;
    11 int f[M];
    12 int rd[M];
    13 int get(){
    14     int res=0,f=1;
    15     char c=getchar();
    16     while (c>'9'||c<'0') {
    17         if (c=='-') f=-1;
    18         c=getchar();
    19     }
    20     while (c<='9'&&c>='0'){
    21         res=(res<<3)+(res<<1)+c-'0';
    22         c=getchar();
    23     }
    24     return res*f;
    25 }
    26 void add(int u,int v){
    27     next[++tot]=head[u];
    28     head[u]=tot;
    29     to[tot]=v;
    30     return ;
    31 }
    32 queue<int> q;
    33 int done[M],cnt;
    34 int main(){
    35     n=get(),m=get();
    36     for (int i=1;i<=n;i++) f[i]=1;
    37     for (int i=1;i<=m;i++){
    38         int u=get(),v=get();
    39         rd[v]++;
    40         add(u,v);
    41     }
    42     for (int i=1;i<=n;i++)
    43         if (rd[i]==0) q.push(i);
    44     while (!q.empty()){
    45         int x=q.front();
    46         //cout<<x<<endl;
    47         q.pop();
    48         done[++cnt]=x;
    49         for (int i=head[x];i;i=next[i]) {
    50             rd[to[i]]--;
    51             if (rd[to[i]]==0) q.push(to[i]);
    52         }
    53     }
    54     for (int i=1;i<=cnt;i++){
    55         for (int j=head[done[i]];j;j=next[j]){
    56             //cout<<done[i]<<" "<<to[j]<<" "<<f[i]<<endl;
    57             int v=to[j];
    58             f[v]=max(f[v],f[done[i]]+1);
    59         }
    60     }
    61     //cout<<endl;
    62     for (int i=1;i<=n;i++) printf ("%d
    ",f[i]);
    63     return 0;
    64 }
  • 相关阅读:
    ES6你不知道的let关键字及变量的提升
    [ts] Property 'aaa' does not exist on type 'Window' 解决办法
    EXPRESS项目PM2启动NODE_ENV传参数不生效问题解决方法
    react组件开发规范总结
    JavaScript heap out of memory解决方法
    移动端响应式布局--你不知道的CSS3.0媒体查询,解决rem部分情况下无法适配的场景
    在nodeJs的Express框架下用TypeScript编写router路由出现import关键字错误的解决方案
    Zepto的天坑汇总
    mac下CornerstoneSVN出错 Description : The working copy is locked due to a previous error
    bootstrap的popover插件在focus模式时在Safari浏览器无法使用的bug解决方案
  • 原文地址:https://www.cnblogs.com/kanchuang/p/11495221.html
Copyright © 2011-2022 走看看