-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFind_the_celebrity.cpp
134 lines (94 loc) · 2.61 KB
/
Find_the_celebrity.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include<iostream>
#include<vector>
#include<map>
#include<string.h>
#include<algorithm>
using namespace std;
std::map<string,vector<string> >relations;
std::map<string,int>indegree;
std::map<string,int>outdegree;
int N=0;
void read(int);
void write();
void printVector(vector<string>);
void getDegree();
void inDegree(vector<string>);
void printDegree(string, map<string,int>);
void getSolution(int);
int main(){
int n; std::cout<<"Enter number of relations : "; std::cin>>n;
read(n);
write();
getDegree();
printDegree("indegree",indegree);
printDegree("outdegree",outdegree);
getSolution(n);
return 0;
}
void read(int n){
while(n!=0){
std::vector<string>child;
string src,des; std::cin>>src>>des;
child.push_back(des);
if(relations.find(src)==relations.end()){
relations.insert({src,child});
}
else{
relations[src].push_back(des);//printVector(relations[src]);
}
indegree.insert({src,0});
outdegree.insert({src,0});
indegree.insert({des,0});
outdegree.insert({des,0});
n--;
}
}
void write(){
std::cout<<"\n"<<"Key" << "\t" << "Value"<<"\n";
map<string,vector<string> >::iterator i;
i=relations.begin();
while (i!=relations.end())
{ std::cout<<i->first<<"\t";
printVector(i->second);
i++;
}
}
void printVector(vector<string>arr){
for(vector<string>::iterator i=arr.begin();i<arr.end();i++){
std::cout<<*i<<"\t";
}
std::cout<<"\n";
}
void getDegree(){
std::cout<<"\n";
for(map<string,vector<string> >::iterator i=relations.begin();i!=relations.end();i++){
outdegree[i->first]=outdegree[i->first]+i->second.size();
// std::cout<<"yaha hu "<<i->first;
//printVector(i->second);
inDegree(i->second);
}
}
void inDegree(vector<string>arr){
for(vector<string>::iterator i=arr.begin();i<arr.end();i++){
indegree[*i]++;
}
}
void printDegree( string type, map<string,int>dict){
std::cout<< type+" map is : \n";
for(map<string,int>::iterator i=dict.begin();i!=dict.end();i++)
{
std::cout<<i->first << "\t" << i->second<<"\n";
}
}
void getSolution(int n){
string ans="";
for(map<string,int>::iterator i=indegree.begin();i!=indegree.end();i++){
if(indegree[i->first]==indegree.size()-1 && outdegree[i->first]==0)
{
ans=i->first;break;
}
}
if(ans=="")ans="No Celebrity";
else ans="Celebrity is : "+ans;
std::cout<<ans<<"\n";
}