forked from izanbf1803/Codingame
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDwarfs standing on the shoulders of giants.cpp
69 lines (56 loc) · 1.58 KB
/
Dwarfs standing on the shoulders of giants.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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
typedef struct Node {
int id;
map<int, Node*> child;
map<int, Node*> parents;
} Node;
Node* create_node(int id)
{
Node* node = new Node;
node->id = id;
return node;
}
void next_generation (
map<int, Node*>& graph,
int& longest,
Node* c, // Current node
int steps)
{
if (c->child.size() == 0 && steps > longest) {
longest = steps;
return;
}
for (map<int, Node*>::iterator it = c->child.begin(); it != c->child.end(); it++)
next_generation(graph, longest, it->second, steps + 1);
}
int main()
{
map<int, Node*> graph;
int n; // the number of relationships of influence
cin >> n; cin.ignore();
for (int i = 0; i < n; i++) {
int x; // a relationship of influence between two people (x influences y)
int y;
cin >> x >> y; cin.ignore();
if (graph.find(x) == graph.end())
graph[x] = create_node(x);
if (graph.find(y) == graph.end())
graph[y] = create_node(y);
graph[x]->child[y] = graph[y];
graph[y]->parents[x] = graph[x];
}
int longest = 0;
vector<Node*> roots;
for (map<int, Node*>::iterator it = graph.begin(); it != graph.end(); it++) {
if (it->second->parents.size() == 0)
roots.push_back(it->second);
}
for (int i = 0; i < roots.size(); i++)
next_generation(graph, longest, roots[i], 1);
cout << longest << endl;
}