-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdona_minhoca2.cpp
92 lines (72 loc) · 1.77 KB
/
dona_minhoca2.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
// iagorrr ;)
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<ll> adj[50000+4];
ll n;
ll qtda, qtdb;
pair <ll, ll> bfs(ll s, ll N){
vector<ll> dist(N+1, LLONG_MAX);
dist[s] = 0;
qtda = 0;
queue<ll> q;
q.push(s);
ll last = s;
while(!q.empty()){
ll current = q.front();
q.pop();
last = current;
for(ll u : adj[current]){
if(dist[u] == LLONG_MAX){
dist[u] = dist[current] + 1;
q.push(u);
}
}
}
// Verificar quantos possuem a mesma distância máxima para sabermos as possibilidades.
for(auto x : dist){
if(x == dist[last]) qtda++;
}
return {last, dist[last]};
}
pair <ll, ll> bfs2(ll s, ll N){
vector<ll> dist(N+1, LLONG_MAX);
dist[s] = 0;
qtdb = 0;
queue<ll> q;
q.push(s);
ll last = s;
while(!q.empty()){
ll current = q.front();
q.pop();
last = current;
for(ll u : adj[current]){
if(dist[u] == LLONG_MAX){
dist[u] = dist[current] + 1;
q.push(u);
}
}
}
// Verificar quantos possuem a mesma distância máxima para sabermos as possibilidades.
for(auto x : dist){
if(x == dist[last]) qtdb++;
}
return {last, dist[last]};
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n;
for(ll i = 0; i < n-1; ++i){
ll a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
auto [last1, aaa] = bfs(1, n);
auto [last, _] = bfs2(last1, n);
auto [last2, diameter] = bfs(last, n);
cout << diameter +1<< endl;
cout << qtda*qtdb << endl;
return 0;
}
// 70/100 WA