1. 程式人生 > 其它 >旅行商(TSP)

旅行商(TSP)

清華OJ——資料結構與演算法實驗(中國石油大學)

旅行商(TSP)


Description

Shrek is a postman working in the mountain, whose routine work is sending mail to n villages. Unfortunately, road between villages is out of repair for long time, such that some road is one-way road. There are even some villages that can’t be reached from any other village. In such a case, we only hope as many villages can receive mails as possible.

Shrek hopes to choose a village A as starting point (He will be air-dropped to this location), then pass by as many villages as possible. Finally, Shrek will arrived at village B. In the travelling process, each villages is only passed by once. You should help Shrek to design the travel route.

Input

There are 2 integers, n and m, in first line. Stand for number of village and number of road respectively.

In the following m line, m road is given by identity of villages on two terminals. From v1 to v2. The identity of village is in range [1, n].

Output

Output maximum number of villages Shrek can pass by.

Example

Input

4 3
1 4
2 4
4 3

Output

3

Restrictions

1 <= n <= 1,000,000

0 <= m <= 1,000,000

These is no loop road in the input.

Time: 2 sec

Memory: 256 MB

Hints

Topological sorting

描述

Shrek是一個大山裡的郵遞員,每天負責給所在地區的n個村莊派發信件。但杯具的是,由於道路狹窄,年久失修,村莊間的道路都只能單向通過,甚至有些村莊無法從任意一個村莊到達。這樣我們只能希望儘可能多的村莊可以收到投遞的信件。

Shrek希望知道如何選定一個村莊A作為起點(我們將他空投到該村莊),依次經過儘可能多的村莊,路途中的每個村莊都經過僅一次,最終到達終點村莊B,完成整個送信過程。這個任務交給你來完成。

輸入

第一行包括兩個整數n,m,分別表示村莊的個數以及可以通行的道路的數目。

以下共m行,每行用兩個整數v1和v2表示一條道路,兩個整數分別為道路連線的村莊號,道路的方向為從v1至v2,n個村莊編號為[1, n]。

輸出

輸出一個數字,表示符合條件的最長道路經過的村莊數。

樣例

見英文題面

限制

1 ≤ n ≤ 1,000,000

0 ≤ m ≤ 1,000,000

輸入保證道路之間沒有形成環

時間:2 sec

空間:256 MB

提示

拓撲排序

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #define N 1100000
 5 using namespace std;
 6 
 7 struct ss
 8 {
 9     int v,next;
10 };
11 ss edg[2*N];
12 int head[N*2],sum_edge=0;
13 
14 void addedge(int u,int v)
15 {
16     edg[sum_edge]=(ss){v,head[u]};
17     head[u]=sum_edge++;
18 }
19 
20 int du[N];
21 int Stack[N],top=0;
22 int arr[N],c1=0;
23 int dp[N]={0};
24 
25 int read()
26 {
27     int now=0;
28     char ch=getchar();
29     while(!(ch>='0'&&ch<='9'))ch=getchar();
30     while(ch>='0'&&ch<='9')
31     {
32         now=now*10+ch-'0';
33         ch=getchar();
34     }
35     return now;
36 }
37 
38 int main()
39 {
40     memset(head,-1,sizeof(head));
41     int n,m,u,v;
42     n=read();
43     m=read();
44     
45     while(m--)
46     {
47         u=read();
48         v=read();
49         addedge(u,v);
50         du[v]++;
51     } 
52     
53     for(int i=1;i<=n;i++)if(!du[i])Stack[top++]=i;
54     
55     while(top)
56     {
57         int now=Stack[--top];
58         arr[c1++]=now;
59         
60         for(int i=head[now];i!=-1;i=edg[i].next)
61         {
62             v=edg[i].v;
63             du[v]--;
64             if(!du[v])Stack[top++]=v;
65         }
66     }
67     
68     int ans=0;
69     
70     for(int i=0;i<c1;i++)
71     {
72         u=arr[i];
73         dp[u]++;
74         ans=max(ans,dp[u]);
75         for(int i=head[u];i!=-1;i=edg[i].next)
76         {
77              v=edg[i].v;
78              dp[v]=max(dp[v],dp[u]);
79         }
80     }
81     
82     printf("%d\n",ans);
83     return 0;
84 }