Determine the position of the match
Analysis:
Obviously, a look is a topological sort. Seemingly simple, hidden weapons ah. The first time you do this, you mark their depth on the topological sort side, such as the example given in the title {1 2;2 3;4 3}. The depth of 1 is 1. The depth of 2, 4 is 2, and the depth of 3 is 3. Then the output depth is the same as the output of the first small. Not in fact Ah!! For example, 6 points, side {5, 3, 5, 1, 5, 4, 5, 2, 3, 1, 3, 2, 6, 4; 6, 2; 4,2} It's best to draw it yourself and see it more clearly!! According to my first thought from 1 to 6 they were 1,1,2,2,3,3 in depth, and the results were 5, 6, 3, 4, 1, 2. Actually, Miles. The correct result should be 5, 3, 1, 6, 2, 4.
Initially there is no more than 5, 6 large number, 5〈6, so output 5. At this time the equivalent of No 5, removed 5 after the discovery, there is no greater than 3 of the number, 3 is less than 6, so first output 3. Take out 3, there is no more than 1, in the output 1 .... until all points are output.
The correct solution to the problem is:
1) Select a point P output with an entry degree of 0;
2) Delete P point
3) The penetration of all the subsequent points of P-1
4) Repeat 1-3 until all points are output
#include <iostream>#include <cstdio>#include <string.h>#include <math.h>#include <algorithm>using namespace STD;intN, M, MX, v[505], e[505][505], ru[505];voidTopu () {intsum =0;intFlag =1; while(Sum < N) { for(inti =1; I <= N; i++) {if(V[i] = =0&& Ru[i] = =0) {V[i] =1;if(Flag = =1) {printf("%d", i); flag =0;}Else printf("%d", i); for(intj =1; J <= N; J + +) {if(E[i][j] = =1) {ru[j]--; E[I][J] =0; }} sum++; Break; } } }}intMain () { while(scanf("%d%d", &n, &m)! = EOF) {memsetE0,sizeof(e));memset(V,0,sizeof(v));memset(Ru,0,sizeof(RU)); for(inti =1; I <= m; i++) {intx, y;scanf("%d%d", &x, &y);if(E[x][y] = =0) {E[x][y] =1; ru[y]++; }} topu ();cout<< Endl; }return 0;}
hdoj1285 topology Ordering