2153: D.ly的排队问题 (拓扑排序)(vector , set , priority_queue )

Description

马上要上体育课了,上体育课之前总归是要排个队的,ly作为班长,怎么排队的问题只能由她来解决,但是马上要上课了,ly又不清楚所有人的身高,她又不好意思问每个人的身高,因为这样会显的自己很不负责,于是她只能通过肉眼观察...那么问题来了,她只能观察出两个人A和B谁高谁矮,但是她没有办法排出一个序列。
ly都已经帮你出了两次主意赢过wjw,那么现在她需要你的帮助,你可以帮她吗?
(ly会告诉你A和B谁高,如果A比B高,会用A>B来表示)

 

 

Input

只有一组数据,每个比较结果占一行,读取到文件结束

 

Output

若输入数据无解,则输出"No Answer!",否则从高到低输出每个人的名字,中间没有分割符
若有多种情况,输出字典序最小的答案

 

 

Sample Input

E>A A>S S>Y

Sample Output

EASY
 
 
 
 
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define mem(s,t) memset(s,t,sizeof(s))
 4 #define pq priority_queue
 5 #define pb push_back
 6 #define fi first
 7 #define se second
 8 #define ac return 0;
 9 #define TLE std::ios::sync_with_stdio(false);   cin.tie(NULL);   cout.tie(NULL);   cout.precision(10);
10     string str;
11     set <int> id;
12     int cnt[50];
13     vector <int> edge[50];
14     pq<int , vector<int> ,greater<int> >q;
15     vector <int> ans;
16 void init()
17 {
18     ans.clear();
19     while(!q.empty()) q.pop();
20     for(int i=0;i<n;i++)
21     {
22         edge[i].clear();
23         cnt[i]=0;
24     }
25 }
26 int main()
27 {
28     TLE;
29     init();
30     while(cin>>str)
31     {
32         id.insert(str[0]-'A');
33         id.insert(str[2]-'A');
34         if(str[1]=='>')
35         {
36             cnt[ str[2]-'A' ]++;
37             edge[ str[0]-'A' ].pb( str[2]-'A' );
38         }
39         else
40         {
41             cnt[ str[0]-'A' ]++;
42             edge[ str[2]-'A' ].pb( str[0]-'A' );
43         }
44     }
45 
46     for(int i=0;i<30;i++)
47     {
48         if( !cnt[i] && id.count(i)!=0  )
49             q.push(i);
50     }
51 
52     while(!q.empty())
53     {
54         int now = q.top();
55         q.pop();
56         ans.pb(now);
57         cout<<ans.front()<<endl;
58         for(int i=0;i<edge[now].size();i++)
59         {
60             int x=edge[now][i];
61             cnt[x]--;
62             if( !cnt[x] && id.count(x)!=0 )
63                 q.push(x);
64         }
65     }
66     if(ans.size()==id.size())
67     {
68         for(int i=0;i<ans.size();i++)
69             cout<<char(ans[i]+'A');
70         cout<<endl;
71     }
72     else
73         cout<<"No Answer!"<<endl;
74     ac;
75 }

 

所遇皆星河
原文地址:https://www.cnblogs.com/Shallow-dream/p/11678631.html