• 洛谷 1137 旅行计划


    https://www.luogu.org/problem/show?pid=1137

    题目描述

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

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

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

    输入输出格式

    输入格式:

    输入的第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%的数据,N ≤ 100;

    对于60%的数据,N ≤ 1000;

    对于100%的数据,N ≤ 100000,M ≤ 200000。

    拓扑排序,没搜到一个点,用当前点+1更新搜到的点

    拓扑吧栈改成队列可以不用取max

    #include<cstdio>
    #include<algorithm>
    #define N 100001
    #define M 200001
    using namespace std;
    int n,m,in[N];
    int front[N],to[M],nxt[M],tot;
    int ans[N];
    int st[N],top;
    void add(int x,int y)
    {
        to[++tot]=y; nxt[tot]=front[x]; front[x]=tot;
    }
    int main()
    {
        scanf("%d%d",&n,&m);
        int x,y;
        while(m--)
        {
            scanf("%d%d",&x,&y);
            add(x,y);
            in[y]++;
        }
        for(int i=1;i<=n;i++)
         if(!in[i]) 
         {
             st[++top]=i;
             ans[i]=1;
         }
        int now;
        while(top)
        {
            now=st[top]; top--;
            for(int i=front[now];i;i=nxt[i])
            {
                in[to[i]]--;
                ans[to[i]]=max(ans[to[i]],ans[now]+1);
                if(!in[to[i]])     st[++top]=to[i];
            }
        }
        for(int i=1;i<=n;i++) printf("%d
    ",ans[i]);
        
    }
  • 相关阅读:
    JS函数调用的方法
    JS 正则表达式
    JS replace()方法-字符串首字母大写
    JS案例之3——倒计时
    JS案例之2——cycle元素轮播
    jsp自定义标签
    每日记载内容总结23
    maven项目部分知识
    eclipse中整合springMvc,velocity和sitemesh
    eclipse中整合springMvc和velocity
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/7067053.html
Copyright © 2020-2023  润新知