HDU 4857 逃生

HDU 4857

大意:如题所示。。。。。

思路:拓扑排序,逆向见图,用链式向前星,优先队列,逆序输出答案,主要就是用vector会T。

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <vector>
 4 #include <queue>
 5 #include <cstring>
 6 #include <cstdio>
 7 using namespace std;
 8 struct sfds
 9 {
10     int from,to;
11 }zz[100005];
12 int sb[30004],m,ss[300004],n;
13 vector <int > sc;
14 priority_queue <int> mn;
15 void slove()
16 {
17     for (int i=1;i<=n;i++)
18         if (!ss[i]) mn.push(i);
19     while (!mn.empty())
20     {
21         int x=mn.top();
22         mn.pop();
23         sc.push_back(x);
24         int t=sb[x];
25         while (t!=-1)
26         {
27             ss[zz[t].to]--;
28             if (!ss[zz[t].to]) mn.push(zz[t].to);
29             t=zz[t].from;
30         }
31     }
32     for (int i=sc.size()-1;i>0;i--)
33         printf ("%d ",sc[i]);
34     printf ("%d\n",sc[0]);
35 }
36 int main ()
37 {
38     freopen ("zz.txt","r",stdin);
39     int T;
40     cin >>T;
41     while (T--)
42     {
43         memset(sb,-1,sizeof(sb));
44         memset(zz,-1,sizeof(zz));
45         memset(ss,0,sizeof(ss));
46         sc.clear();
47         cin >>n>>m;
48         for (int i=0;i<m;i++)
49         {
50             int a,b;
51             scanf ("%d%d",&a,&b);
52             zz[i].to=a;
53             zz[i].from=sb[b];
54             sb[b]=i;
55             ss[a]++;
56         }
57         slove();
58     }
59 }
View Code

 

posted @ 2018-04-22 16:22  gefhg  阅读(118)  评论(0编辑  收藏  举报