Do this problem feel very excited, because in the next first contact topology sort Ah =, and looked at the explanation, suddenly found that the problem can be optimized with DP, and then a off all the samples, the whole person excited bad, wow ka ka ka ka ~ ka ka ~ oops, a Laugh--| |
Travel Salesman (TSP)
Description
Shrek is a postman working in the mountain, and whose routine work are sending mail to n villages. Unfortunately, road between villages is out of the repair for long time, such this some road is one-way road. There is even some villages that can ' t is reached from any other village. In such a case, we are only hope as many villages can receive mails as possible.
Shrek hopes to choose a village a as starting point (He'll be air-dropped to this location), then pass by as many Villag Es as possible. Finally, Shrek'll arrived at village B. In the travelling process, each villages is a passed by once. You should help Shrek to the design of the travel route.
Input
There is 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 the 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 31 42 44 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
Description
Shrek, A postman in the mountains, is responsible for distributing letters to n villages in the area every day. The cup is, however, because of the narrow road and the disrepair of the village, the road between the villages can only be passed one way or even some villages cannot reach from any one village. In this way we can only hope that as many villages as possible receive the delivery of the letter.
Shrek wanted to know how to select a village a as a starting point (we dropped him into the village), and then through as many villages as possible, each village on the way through only once, and finally to the end of village B, to complete the process. This task is yours to complete.
input
The first line consists of two integer n,m, each representing the number of villages and how many roads can be used.
The following total m lines, each line with two integers v1 and v2 represent a road, and two integers are road-connected village numbers, with the direction of the road from V1 to v2,n villages numbered [1, n].
Output
Outputs a number that represents the number of villages on the longest road that meets the criteria.
Sample Example
See the English question face
Limit
1≤n≤1,000,000
0≤m≤1,000,000
Input to ensure no ring formation between roads
Time: 2 sec
Space: Up to MB
Tips
Topological sorting
Notes section:
At the beginning of this problem, I do not know that there is a topological sort, has been thinking of the node 10^6 how to operate the diagram, so the first idea is the adjacency table , with a vetor and an array to simulate the adjacency table, and then consider the one-way search for the longest road when the optimization should be done. The first idea is depth-first search (DFS), even considering the breadth-first search (BFS), but no matter how much time it takes for O (n^2), it is no doubt that either the optimization is considered, or the algorithm is changed, then suddenly find the topic below the hint- topological sort --| |, Bo Master suddenly feel brain remnant ~
Then Bo Master began to find topological sort related information, fortunately there is a book of my seniors borrowed from me, after reading the concept of AOV and AOE network and the basic concept of topological sequencing, but I am still a little confused about this problem, because the direct sort will undoubtedly destroy the adjacency table, later thought with The array holds the subscript after the topological sort , and later finds that the array is actually a queue , so that the numbering sequence and adjacency table are not broken. After preparing to start writing the main algorithm, the original consideration is to use dfs+ optimization, and then suddenly found that each city after the topological sequencing , there is a state to meet the no effect -from the start of the city to the city's largest number of cities.
With this idea, based on the topological ordering (the algorithm is written in the topological sorting function), the DP algorithm is completed.
So this topic, my method is the adjacency table (Save and find) + Queue (save topology Sort) +DP (time Optimization) + topology sequencing (completion of AOV network node ordering)
The above for Bo master gibberish = =| |, directly read the code easier to understand.
1 //Tshinghua OJ travel Salesman (TSP)2 //adjacency Table +dp+ queue + topology ordering3 //memory:66304kb time:1101ms (No.17)4#include <iostream>5#include <cstring>6#include <cstdio>7 using namespacestd;8 9 #defineMAX 1000005Ten #defineMax (x, y) (() > (y)? ( x):(y)) One A intN, M;//Village Number-number of roads - intTopology[max],lt;//topology Array-length - intMark[max];//Mark of Entry the intMaxcity =1;//Answer - - //a village leading from the city - structnode{ + intNum//Village Number -Node *Next; +Node () {next =NULL;} ANode (intX,node *N): num (x), Next (n) {} at }; - - structcity{ -Node *NC;//next-city - intdp//maximum number of cities to pass to -City () {NC = NULL; dp =1; } in voidInsertintNC); - }city[max]; to + voidCity::insert (intNC) - { themark[nc]++;//Direct successor City admission +1 * if( This->NC = =NULL) $ This->NC =NewNode (nc,null);Panax Notoginseng Else{ -Node *node =NewNode (NC, This-NC); the This->NC =node; + } A return; the } + - /*Topological sorting*/ $ voidtopology () $ { - for(inti =1; I <= N; i++) - if(!mark[i]) topology[++lt] = i;//City with an entry level of 0 the //Main Content - for(inti =1; topology[i];i++)Wuyi { the intcur = topology[i];//the City-number - //Traverse all direct successors of the city Wu for(Node *tmp = CITY[CUR].NC; tmp = NULL; tmp = tmp->next) - { About //No effect is met here-DP $CITY[TMP->NUM].DP = Max (CITY[CUR].DP +1, city[tmp->NUM].DP); -maxcity = Max (city[tmp->NUM].DP, maxcity); - //Processing subsequent - intnum = tmp->num; Amark[num]--;//subsequent penetration -1 + if(!mark[num]) topology[++lt] = num;//if the subsequent entry is 0 the } - } $ } the the intMain () the { thescanf"%d%d", &n, &m); - for(inti =0; I < m; i++) in { the intx, y;//X->y thescanf"%d%d", &x, &y); About City[x].insert (y); the } the /*Topological sorting*/ the topology (); +printf"%d\n", maxcity); - the return 0;Bayi}
Small Ink--Original
ACM/ICPC Data Structure-adjacency table +dp+ queue + topology sort (tshinghua oj-tsp)