The definition of topological ordering is simply to give you a diagram to write a sequence diagram if a leads to B then a must precede B in the sequence
Topological ordering may have a lot of results that have to be a direction-free graph can be used to determine the existence of a ring by using topological sequencing. But the topological sort time complexity is very low only O (v+e)
The basic realization of the idea is to remove the point of 0 points and then delete the edge connected to it until there is no edge if there is an edge but can not find a point of 0 points with a ring
Learning this algorithm to contact two topics is very classic very simple but generally no OJ have evaluation bonus this topic in Fuzhou University OJ and Sun Yat-sen town of Sicily found on the evaluation (why call Sicily?) )
First put the topic:
Family tree
"Title description"
There is a large family, generational relationship is very confusing, please help to tidy up this relationship.
Give a message to everyone's children.
Enter a sequence so that everyone's descendants are listed later than that person.
Input
The first line is an integer (1<=n<=100) that represents the number of families.
Next n lines, line I represents the son of the first person.
The end of each line is 0, which means the description is complete.
Output
Output a sequence so that everyone's descendants are listed later than that person.
If there is more than one solution to output any solution.
"Input Sample"
5
0
4 5 1 0
1 0
5 3
0
3 0
"Output Example"
2 4 5) 3 1
This topic obviously only needs to be sorted once. The output of any sequence can be a sequence that does not require an output result but a series so that the array is stored in the process the elements of the team.
Release code:
Code for normal loops
、
Code with STL queues is easier to understand
Bonus
"Title description"
Because of invincible everywhere in 2005 the world handsome handsome male general choice wins, Yali company general manager Mr.Z Mood Good, decided to give each employee bonuses. The company decided to calculate how much they were rewarded by the company's contribution as a standard this year.
So Mr.Z ordered to convene M-party talks. Each of the delegates who attended the meeting made their own comments: "I think employee A's bonus should be higher than B!" "Mr.Z decided to find a bonus scheme that would meet the opinions of delegates, while at the same time making the total number of bonuses minimal. Each employee has a minimum bonus of 100 yuan.
Input
The first line two integer n,m, indicating the total number of employees and the number of delegates;
The following m lines, 2 integers per line, A, B, indicate that a representative believes that the bonus of employee number A should be higher than that of number B.
Output
If the legal scheme cannot be found, the output is "1"; otherwise the output of a number indicates a minimum total bonus.
Input/Output sample
Reward.in
2 1
1 2
Reward.out
201
"Data Range"
80% of data Meet n<=1000,m<=2000;
100% of the data meets n<=10000,m<=20000.
This topic has a ring, which requires output instead of process, so the solution is unique. A tiered approach is used for each bonus level to be calculated centrally. The code comments are very clear and are not mentioned here.
This idea refers to the Huang long code, but his code is wrong, I have been in his blog the Middle finger.
But there is still a question, this code in Sicily measured AC, Fuzhou University WA, so this code may also have some problems
Try to write the STL queue version but the time relationship is not finished and the subject queue to achieve some difficulties if successful and then fill in
On the Code
Finally, add a few good articles that sort the topology:
http://blog.csdn.net/dm_vincent/article/details/7714519 This talk is a bit advanced, interested can see
http://blog.csdn.net/liwen_7/article/details/7298736 briefly introduced the principle
The demonstration of http://www.tyut.edu.cn/kecheng1/site01/suanfayanshi/topological_sort.asp Taiyuan Science and technology is helpful to understand
In fact, all blogs are not as good as their own understanding of the best ...
Try hard to Tarjan!!!
--Smoke and cold forest clusters, painted screen exhibition; The sky is small, and Daisy's brow is shallow.
"Daily Learning" "Topological sorting" Genealogy tree &fzu1483 Sicily1424 Bonus