-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path124.cpp
More file actions
71 lines (65 loc) · 1.58 KB
/
124.cpp
File metadata and controls
71 lines (65 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<bits/stdc++.h>
using namespace std;
string s;
vector<char>vertices;
map<char,int>indegreee;
map<char,vector<char> > G;
map<char,bool>visited;
vector<char>result;
void clear(){
vertices.clear();
indegreee.clear();
visited.clear();
result.clear();
G.clear();
}
void allTopoSort(vector<char>& result,map<char,bool>visited){
bool f = false;
for(auto i:vertices){
if(indegreee[i] == 0 && !visited[i]){
for(auto j:G[i]){
indegreee[j] = indegreee[j] - 1;
}
result.push_back(i);
visited[i] = true;
allTopoSort(result,visited);
visited[i] = false;
result.pop_back();
for(auto j:G[i]){
indegreee[j] = indegreee[j] + 1;
}
f = true;
}
}
if(!f){
for(auto i:result){
cout<<i;
}
cout<<endl;
}
}
int main(){
for(int T = 0;;T++){
if(!getline(cin,s)) break;
if(T>0) cout<<endl;
stringstream ss;
char c;
ss<<s;
while(ss>>c){
vertices.push_back(c);
indegreee[c] = 0;
visited[c] = false;
}
sort(vertices.begin(),vertices.end());
getline(cin,s);
stringstream ss1;
char a,b;
ss1<<s;
while(ss1>>a>>b){
G[a].push_back(b);
indegreee[b] = indegreee[b] + 1;
}
allTopoSort(result,visited);
clear();
}
}