ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

luoguP1137旅行计划

2020-06-27 09:06:22  阅读:284  来源: 互联网

标签:oth 旅行 ch 正整数 小明 城市 getc luoguP1137 计划


题目描述

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

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

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

输入格式

第1行为两个正整数N, M。

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

输出格式

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

输入输出样例

输入

5 6
1 2
1 3
2 3
2 4
3 4
2 5

输出

1
2
3
4
3

说明/提示

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

对于20%的数据,N ≤ 100

对于60%的数据,N ≤ 1000

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

#include <bits/stdc++.h>
namespace FastIO {
    char buf[1 << 21], buf2[1 << 21], a[20], *p1 = buf, *p2 = buf, hh = '\n';
    int p, p3 = -1;

    void read() {}

    void print() {}

    inline int getc() {
        return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++;
    }

    inline void flush() {
        fwrite(buf2, 1, p3 + 1, stdout), p3 = -1;
    }

    template<typename T, typename... T2>
    inline void read(T &x, T2 &... oth) {
        int f = 0;
        x = 0;
        char ch = getc();
        while (!isdigit(ch)) {
            if (ch == '-')
                f = 1;
            ch = getc();
        }
        while (isdigit(ch)) {
            x = x * 10 + ch - 48;
            ch = getc();
        }
        x = f ? -x : x;
        read(oth...);
    }

    template<typename T, typename... T2>
    inline void print(T x, T2... oth) {
        if (p3 > 1 << 20)
            flush();
        if (x < 0)
            buf2[++p3] = 45, x = -x;
        do {
            a[++p] = x % 10 + 48;
        } while (x /= 10);
        do {
            buf2[++p3] = a[p];
        } while (--p);
        buf2[++p3] = hh;
        print(oth...);
    }
} // namespace FastIO
#define read FastIO::read
#define print FastIO::print
//======================================
using namespace std;
const int maxn=1e5+10;
typedef long long ll;
int n,m;
struct node{
    int v,nxt;
}e[maxn*2];
int head[maxn],d[maxn],a[maxn],tot,in[maxn],t;
inline void add(int u,int v) {
    t++;
    e[t].v = v;
    e[t].nxt = head[u];
    head[u] = t;
}
inline void toposort() {
    queue<int> q;
    for (int i = 1; i <= n; i++) {
        d[i]=1;
        if (!in[i]) {
            q.push(i);
            a[++tot] = i;
        }
    }
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (int i = head[u]; i; i = e[i].nxt) {
            int v = e[i].v;
            if (!--in[v]) {
                q.push(v);
                a[++tot] = v;
            }
        }
    }
}
int main() {
#ifndef ONLINE_JUDGE
    freopen("1.txt", "r", stdin);
    //freopen("2.txt", "w", stdout);
#endif
    //======================================
    read(n, m);
    for (int i = 1, u, v; i <= m; i++) {
        read(u, v);
        add(u, v);
        in[v]++;
    }
    toposort();
    for (int i = 1; i <= n; i++) {
        int u = a[i];
        for (int j = head[u]; j; j = e[j].nxt) {
            int v = e[j].v;
            d[v] = max(d[v], d[u] + 1);
        }
    }
    for (int i = 1; i <= n; i++) {
        print(d[i]);
    }
    //======================================
    FastIO::flush();
    return 0;
}

标签:oth,旅行,ch,正整数,小明,城市,getc,luoguP1137,计划
来源: https://www.cnblogs.com/Accpted/p/13197289.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有