-
Notifications
You must be signed in to change notification settings - Fork 4
/
UVA_11060.cpp
94 lines (75 loc) · 2.18 KB
/
UVA_11060.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
This Problem is solved using In degree method not DFS .
#include <bits/stdc++.h>
using namespace std;
#define sz 1000
int vertex,edge;
int adj[sz][sz];
int in_degree[sz];
int visited[sz];
vector < string > sorted;
map < string,int> MAP;
map < int,string> MAP2;
void topsort();
int main() {
int caseno = 0 ;
//freopen("inputuva.txt","r",stdin);
while(scanf("%d",&vertex) != EOF ) {
memset(adj,0,sizeof(adj));
memset(visited,0,sizeof(visited));
memset(in_degree,0,sizeof(in_degree));
int mapper = 0;
for ( int i = 0 ; i < vertex ; i++) {
string letter;
cin >> letter;
MAP[letter] = mapper;
MAP2[mapper] = letter;
mapper++;
}
cin >> edge;
for ( int i = 0 ; i < edge ; i++) {
string node1;
string node2;
cin >> node1 >> node2;
int vertex1 = MAP[node1];
int vertex2 = MAP[node2];
adj[vertex1][vertex2] = 1;
}
topsort();
cout << "Case #" << ++caseno << ": Dilbert should drink beverages in this order:";
for ( int i = 0 ; i < sorted.size() ; i++ )
cout << " " << sorted[i];
cout << "." << endl;
cout << endl;
sorted.clear();
MAP.clear();
MAP2.clear();
}
return 0 ;
}
void topsort() {
for ( int i = 0 ; i < vertex ; i++) {
for ( int j = 0 ; j < vertex ; j++) {
if ( adj[i][j] == 1) in_degree[j] = in_degree[j]+1;
}
}
priority_queue<int, vector<int>, greater<int> > Q;
for ( int i = 0 ; i < vertex ; i++) {
if ( in_degree[i] == 0) {
Q.push(i);
visited[i] = 1 ;
}
}
while (!Q.empty()) {
int u = Q.top();
Q.pop();
sorted.push_back(MAP2[u]);
for ( int j = 0 ; j < vertex ; j++) {
if ( adj[u][j] == 1 && visited[j] == 0) {
in_degree[j] = in_degree[j] - 1 ;
if ( in_degree[j] == 0) {
visited[j] = 1 ;
Q.push(j);
}
}
}
}