Links:
http://acm.hdu.edu.cn/showproblem.php?pid=1285
Topology sort template.
#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include < sstream> #include <cstdio> #include <vector> #include <cmath> #include <queue> #include < stack> #include <set> #include <map> #define Lson o<<1, L, M#define Rson o<<1|1, m+1, R#define PI I Pair<int, int> #define ALL (x) X.begin (), x.end () #define MEM (a) memset (A,0,sizeof (a)) typedef long Long Ll;const Double pi = ACOs ( -1.0); const int MAX = 0x3f3f3f3f;const ll mod = 1000000007ll;const int N = 250005;using namespace Std;str UCT C {int NE, to;} e[n];int N, M;int du[505], he[505];void add (int id, int x, int y) {e[id].to = y; E[id].ne = He[x]; HE[X] = ID;} int main () {//Freopen ("In.txt", "R", stdin);//Freopen ("OUT.txt", "w", stdout); while (CIN >> n >> m) {mem (du); memset (He,-1, sizeof (he)); for (int i = 1; I <= m; i++) {int x, y; scanf ("%d%d", &x, &y); du[y]++; Add (i, x, y); } int cnt = 0; while (1) {for (int i = 1; I <= n; i++) if (du[i] = = 0) {printf ("%d", I); cnt++; if (CNT < n) printf (""); else {puts (""); Break } du[i]--; for (int j = he[i]; J! =-1; j = e[j].ne) {du[e[j].to]--; } break; } if (cnt = = n) break; }} return 0; }
HDU 1285 determining the tournament position (topological sorting)