标签:
开始一直tle后来才发现少了for(int i=0;i<n+1;i++) mapp[i].clear(); 哎哟喂,本来就不开心的我更不开心.拓扑排序
#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();//开始掉了,一直tle int x,y; while(m--) { scanf("%d%d",&x,&y); if(visit[x][y]){continue;}//避免重复输入; mapp[x].push_back(y); head[y]++; visit[x][y]=1; } topo(); print(); } return 0; }
标签:
原文地址:http://blog.csdn.net/zafkiel_nightmare/article/details/45031367