Created
October 10, 2016 08:15
-
-
Save nafis00141/956c168ad900d631b114a4bc18298e19 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<bits/stdc++.h> | |
using namespace std; | |
vector<int>v[105],order; | |
vector<int>A; | |
int deg[105]; | |
bool visited[105]; | |
int n,m; | |
map<string,int>mp; | |
map<int,string>ma; | |
void topsort(){ | |
int q=0; | |
memset(visited,false,sizeof(visited)); | |
while(q!=n){ | |
for(int j=0;j<A.size();j++){ | |
int x = A[j]; | |
if(deg[x]==0 && visited[x]==false){ | |
visited[x]=true; | |
q++; | |
order.push_back(x); | |
for(int i=0;i<v[x].size();i++){ | |
int xx =v[x][i]; | |
deg[xx]--; | |
} | |
break; | |
} | |
} | |
} | |
} | |
int main(){ | |
int sc=1; | |
while(scanf("%d",&n)!=EOF){ | |
string a,b; | |
int k=1; | |
for(int i=1;i<=n;i++){ | |
cin>>a; | |
mp[a]=k; | |
ma[k]=a; | |
A.push_back(k); | |
k++; | |
} | |
cin>>m; | |
while(m--){ | |
cin>>a>>b; | |
v[mp[a]].push_back(mp[b]); | |
deg[mp[b]]++; | |
} | |
topsort(); | |
cout<<"Case #"<<sc<<": Dilbert should drink beverages in this order:"; | |
for(int i=0;i<order.size();i++){ | |
cout<<" "<<ma[order[i]]; | |
} | |
cout<<".\n"; | |
for(int i=0;i<=n;i++){ | |
v[i].clear(); | |
} | |
order.clear(); | |
memset(deg,0,sizeof(deg)); | |
mp.clear(); | |
ma.clear(); | |
A.clear(); | |
sc++; | |
cout<<"\n"; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment