travel plan

Title Description

Xiao Ming to go to a national tour. The country has N cities, numbered from 1 to N, and there are 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

The first 1 Behavior two positive integers N, M .

Next M lines, each line two positive integers x , y, shows there is a connection city x and urban roads y to ensure that the city x in the city y west.

Output Format

N line, the i line contains a positive integer, it expressed in the first 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 2 0 % of the data, N . 1 0 0;

For . 6 0 % of the data, N . 1 0 0 0;

For . 1 0 0 % of the data, N . 1 0 0 0 0 0 , M 2 0 0 0 0 0.

 

analysis:

This question first need to sort a topology, topology do not know if the sort to see this blog (I think the writing is better): the point here .

Then we can happily the DP, i.e., for each edge u ---> v, there is 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\n",f[i]);
63     return 0;
64 }

 

 

 

Guess you like

Origin www.cnblogs.com/kanchuang/p/11495221.html