Luo Gu P1137 (tree each point of the longest road, dp)

Title Description

Xiao Ming to go to a national tour. The country has # N N cities, numbered 1 of 1 to N N, and there are M M roads connected, Xiao Ming ready to depart from one of the cities, and only go east city i stopped.

So he needs to select a city first to arrive, and to develop a route to the city i is the end, so that in addition to the first city, every city east of the city in front of a line on the line, and to meet this premise also want to visit city ​​as much as possible.

Now, you only know the relative positional relationship between the two cities every road is connected, but does not know the specific location of all cities. Now for all i, you need to develop a course for Xiao Ming, and to find the city i i end at up to how many cities can tour.

Input Format

First . 1 . 1 conduct two positive integers N, M N , M.

Next M M rows, each row of two positive integers the X-, y the X- , y, shows there is a connection city the X- the X-urban y road y, to ensure the city the X- the X-urban y y west.

Output Format

N N line, the i i line contains a positive integer, it expressed in the first i i cities for the end of the maximum number of cities can tour.

Sample input and output

Input # 1
5 6
1 2
1 3
2 3
2 4
3 4
2 5
Output # 1
1
2
3
4
3

Description / Tips

Select all of these answers can be obtained from the city of departure 1.

For 20 \% 2 0 % of the data, N ≤ 100 N . 1 0 0;

For 60 \% . 6 0 % of the data, N ≤ 1000 N . 1 0 0 0;

For 100 \% . 1 0 0 % data, N ≤ 100000, 200000 M ≤ N . 1 0 0 0 0 0 , M 2 0 0 0 0 0.

 

Reverse Figure deposit, then the question becomes transformed from the i-th point, the maximum distance to go all

DP [i] denotes the i th maximum distance through the edge can go, pruning advantage of this, the efficiency is very high. Each point dfs while seeking answers

Code:

#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define fi first
#define se second
#define ptch putchar
#define CLR(a) while(!(a).empty()) a.pop()

using namespace std;
inline LL read() {
    LL s = 0,w = 1;
    char ch = getchar();
    while(!isdigit(ch)) {
        if(ch == '-') w = -1;
        ch = getchar();
    }
    while(isdigit(ch))
        s = s * 10 + ch - '0',ch = getchar();
    return s * w;
}
inline void write(LL x) {
    if(x < 0)
        putchar('-'), x = -x;
    if(x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}

const int maxn = 1e5 + 10;
int head[maxn],cnt;
struct xx{
    int v,nex;
    xx(int v = 0,int nex = 0):
        v(v),nex(nex){}
}edge[maxn << 1];
int dp[maxn << 1];

int dfs(int p,int f){
    int ans = 0;
    for(int i = head[p]; ~i;i = edge[i].nex){
        int v = edge[i].v;
        if(v == f) continue;
        if(!dp[i]) dp[i] = dfs(v,p) + 1;
        ans = max(ans,dp[i]);
    }
    return ans;
}

int main() {
//#ifndef ONLINE_JUDGE
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
//#endif
    int n = read(),m = read();
    clr(head,-1); cnt = 0;
    for(int i = 1;i <= m;++ i){
        int u = read(),v = read();
        edge[cnt] = xx(u,head[v]);
        head[v] = cnt ++;
    }
    for(int i = 1;i <= n;++ i)
        write(dfs(i,-1) + 1),ptch('\n');
    return 0;
}

 

 

Guess you like

Origin www.cnblogs.com/rookie-acmer/p/11354464.html