# HDU 1285 确定比赛名次 + HDU 4857 逃生（拓扑排序由浅入深）

HDU - 1285

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
using namespace std;
int n, m;
bool vis[505][505];
int indegree[505];
void topo()
{
bool flag = 0;
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= n; j ++)
{
if(! indegree[j])
{
indegree[j] --; //防止重复判断
if(flag)printf(" ");
printf("%d",j);
flag = 1;
for(int k = 1; k <= n; k ++)
if(vis[j][k])
indegree[k] --;
break;//及时跳出进入i + 1层循环
}
}
}
int main()
{
while(scanf("%d %d", &n, &m) != EOF)
{
memset(vis, 0, sizeof(vis));
memset(indegree, 0, sizeof(indegree));
int a, b;
for(int i = 1; i <= m; i ++)
{
scanf("%d %d", &a, &b);
if(! vis[a][b])
{
vis[a][b] = 1;
indegree[b] ++;
}
}
topo();
printf("\n");
}
return 0;
}

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
using namespace std;
int n, m;
int ind[505];
bool vis[505][505];
int to[505];
int cnt;
void topo()
{
priority_queue<int, vector<int>, greater<int> >q;
for(int i = 1; i <= n; i ++)
{
if(! ind[i])
q.push(i);
}
while(! q.empty())
{
int h = q.top();
q.pop();
to[cnt ++] = h;
for(int i = 1; i <= n; i ++)
{
if(vis[h][i])
{
ind[i] --;
if(! ind[i])q.push(i);
vis[h][i] = 0;
}
}
}
}
int main()
{
while(scanf("%d%d",&n,&m) != EOF)
{
memset(vis, 0, sizeof(vis));
memset(ind, 0, sizeof(ind));
int a, b;
for(int i = 1; i <= m; i ++)
{
scanf("%d%d",&a,&b);
if(! vis[a][b])
{
vis[a][b] = 1;
ind[b] ++;
}
}
cnt = 0;
topo();
int flag = 0;
for(int i = 0; i < cnt; i ++)
{
if(flag)printf(" ");
printf("%d",to[i]);
flag = 1;
}
printf("\n");
}
return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
using namespace std;
int n, m;
int ind[505];
int to[505];
int cnt;
int main()
{
while(scanf("%d%d",&n,&m) != EOF)
{
vector<int>e[505];
memset(ind, 0, sizeof(ind));
int a, b;
for(int i = 1; i <= m; i ++)
{
scanf("%d%d",&a,&b);
e[a].push_back(b);
ind[b] ++;
}
cnt = 0;

priority_queue<int, vector<int>, greater<int> >q;
for(int i = 1; i <= n; i ++)
{
if(! ind[i])
q.push(i);
}
while(! q.empty())
{
int h = q.top();
to[cnt ++] = h;
q.pop();
for(int i = 0; i < e[h].size(); i ++)
{
int H = e[h][i];
ind[H] --;
if(! ind[H])q.push(H);
}
}

int flag = 0;
for(int i = 0; i < cnt; i ++)
{
if(flag)printf(" ");
printf("%d",to[i]);
flag = 1;
}
printf("\n");
}
return 0;
}


AC代码：
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
using namespace std;
int n, m;
int ind[50005];
int to[50005];
int cnt;
int main()
{
int t;
scanf("%d",&t);
while(t --)
{
scanf("%d%d",&n,&m);
vector<int>e[50005];
memset(ind, 0, sizeof(ind));
int a, b;
for(int i = 1; i <= m; i ++)
{
scanf("%d%d",&a,&b);
e[b].push_back(a);
ind[a] ++;
}
cnt = n;
priority_queue<int, vector<int>, less<int> >q;
for(int i = 1; i <= n; i ++)
{
if(! ind[i])
q.push(i);
}
while(! q.empty())
{
int h = q.top();
to[cnt --] = h;
q.pop();
for(int i = 0; i < e[h].size(); i ++)
{
int H = e[h][i];
ind[H] --;
if(! ind[H])q.push(H);
}
}

int flag = 0;
for(int i = 1; i <= n; i ++)
{
if(flag)printf(" ");
printf("%d",to[i]);
flag = 1;
}
printf("\n");
}
return 0;
}