-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_1167.cpp
More file actions
63 lines (48 loc) · 1022 Bytes
/
BOJ_1167.cpp
File metadata and controls
63 lines (48 loc) · 1022 Bytes
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
//21 09 09
#include <iostream>
#include <vector>
using namespace std;
const int MAX = 100000+1;
vector<pair<int, int>> tree[MAX];
bool visited[MAX];
int v=0, top_node = 0, diameter=0;
void DFS(int node, int cost)
{
if (visited[node])
return;
visited[node] = true;
if (cost>diameter)
{
diameter = cost;
top_node = node;
}
for (int i=0; i<tree[node].size(); i++)
DFS(tree[node][i].first, cost+tree[node][i].second);
}
int main(void)
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>v;
for (int i=0; i<v; i++)
{
int start;
cin>>start;
while (1)
{
int node, length;
cin>>node;
if (node == -1)
break;
cin>>length;
tree[start].push_back({node, length});
}
}
fill_n(visited, v+1, false);
DFS(1,0);
diameter = 0;
fill_n(visited, v+1, false);
DFS(top_node, 0);
cout<<diameter;
return 0;
}