began to be tle later only to discover the less for (int i=0;i<n+1;i++) mapp[i].clear (); Oh hey, I'm not happy. Topological sorting
#include <iostream> #include <queue> #include <vector> #include <cstring> #include <cstdio > #define MAXN 505 using namespace std;vector<vector<int> >mapp;int visit[maxn][maxn];int n,m;int head[ Maxn];int RE[MAXN]; void topo () {int t=0; while (T!=n) {for (int i=1;i<=n;i++) {if (!head[i]) {head[i]--; Re[t++]=i; for (int j=0;j<mapp[i].size (); j + +) {head[mapp[i][j]]--; } break; }}}}void print () {printf ("%d", re[0]); for (int i=1;; i++) {if (!re[i]) {break;} printf ("%d", re[i]); } printf ("\ n");} int main () {Cin.sync_with_stdio (false); while (scanf ("%d%d", &n,&m)!=eof) {memset (head,0,sizeof (head)/maxn* (n+1)); memset (re,0,sizeof (re)/maxn* (n+1)); memset (visit,0,sizeof (visit)); Mapp.resize (n+1); for (int i=0; i<n+1;i++) mapp[i].clear ();//start off, always tle int x, y; while (m--) {scanf ("%d%d", &x,&y); if (Visit[x][y]) {continue;} Avoid repeated input; Mapp[x].push_back (y); head[y]++; Visit[x][y]=1; } topo (); Print (); } return 0;}
HDU 1285 determine the tournament position