#include "bits/stdc++.h"
using namespace std;
#define s second
#define f first
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
const ll N = 2*1e5 + 10;
vector<ll> adj[N];
bool v[N];
bool visited[N];
int curdist = 0;
int maxd = 0;
ll maxi = 0;
ll nummaxi = 0;
void update(ll ans, ll numways) {
if (ans > maxi) {
maxi = ans;
nummaxi = numways;
} else if (ans == maxi) nummaxi += numways;
}
bool dfs(int cur, int target, int d) {
if (cur == target) {
v[cur]=true;
curdist = d;
return true;
}
// cout << cur << target << endl;
visited[cur]=true;
for (auto val: adj[cur]) {
if (!visited[val]) {
v[cur] = (dfs(val, target, d+ 1) || v[cur]);
}
}
return v[cur];
}
void dfs2(int cur, int d) {
visited[cur] = true;
if (d > maxd) {
maxd = d;
// update(curdist * d, 1);
}
for (auto val: adj[cur]) {
if (!visited[val] && !v[val]) dfs2(val, d+1);
}
}
int main() {
int n;
cin >> n;
for (ll i = 0; i < n -1; i++ ) {
ll a, b;
cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
}
vector<int> leaves;
for (int i = 1; i <= n; i++) {
if (adj[i].size() == 1) {
leaves.push_back(i);
}
}
for (int i = 0; i < leaves.size(); i++) {
for (int j = i + 1; j < leaves.size(); j++) {
int curans = 0;
maxd = 0;
curdist = 0;
memset(v, false, sizeof v);
memset(visited, false, sizeof visited);
dfs(leaves[i], leaves[j], 0);
memset(visited, false, sizeof visited);
for (int i = 0; i < n; i++) {
if (v[i]) {
dfs2(i, 0);
}
}
// cout << leaves[i] << leaves[j] << curdist << maxd << endl;
update(curdist * maxd, 1);
}
}
cout << maxi << ' ' << nummaxi << endl;
}
Compilation message
road.cpp: In function 'int main()':
road.cpp:79:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for (int i = 0; i < leaves.size(); i++) {
| ~~^~~~~~~~~~~~~~~
road.cpp:80:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | for (int j = i + 1; j < leaves.size(); j++) {
| ~~^~~~~~~~~~~~~~~
road.cpp:81:8: warning: unused variable 'curans' [-Wunused-variable]
81 | int curans = 0;
| ^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5332 KB |
Output is correct |
2 |
Correct |
3 ms |
5332 KB |
Output is correct |
3 |
Correct |
3 ms |
5332 KB |
Output is correct |
4 |
Correct |
3 ms |
5332 KB |
Output is correct |
5 |
Correct |
3 ms |
5332 KB |
Output is correct |
6 |
Correct |
3 ms |
5332 KB |
Output is correct |
7 |
Correct |
11 ms |
5528 KB |
Output is correct |
8 |
Correct |
6 ms |
5384 KB |
Output is correct |
9 |
Correct |
9 ms |
5332 KB |
Output is correct |
10 |
Correct |
8 ms |
5332 KB |
Output is correct |
11 |
Correct |
22 ms |
5332 KB |
Output is correct |
12 |
Correct |
23 ms |
5332 KB |
Output is correct |
13 |
Correct |
24 ms |
5460 KB |
Output is correct |
14 |
Correct |
27 ms |
5332 KB |
Output is correct |
15 |
Correct |
3 ms |
5380 KB |
Output is correct |
16 |
Correct |
4 ms |
5332 KB |
Output is correct |
17 |
Correct |
5 ms |
5332 KB |
Output is correct |
18 |
Correct |
4 ms |
5332 KB |
Output is correct |
19 |
Correct |
4 ms |
5384 KB |
Output is correct |
20 |
Correct |
5 ms |
5384 KB |
Output is correct |
21 |
Correct |
4 ms |
5332 KB |
Output is correct |
22 |
Correct |
4 ms |
5380 KB |
Output is correct |
23 |
Correct |
5 ms |
5384 KB |
Output is correct |
24 |
Correct |
83 ms |
5388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5332 KB |
Output is correct |
2 |
Correct |
3 ms |
5332 KB |
Output is correct |
3 |
Correct |
3 ms |
5332 KB |
Output is correct |
4 |
Correct |
3 ms |
5332 KB |
Output is correct |
5 |
Correct |
3 ms |
5332 KB |
Output is correct |
6 |
Correct |
3 ms |
5332 KB |
Output is correct |
7 |
Correct |
11 ms |
5528 KB |
Output is correct |
8 |
Correct |
6 ms |
5384 KB |
Output is correct |
9 |
Correct |
9 ms |
5332 KB |
Output is correct |
10 |
Correct |
8 ms |
5332 KB |
Output is correct |
11 |
Correct |
22 ms |
5332 KB |
Output is correct |
12 |
Correct |
23 ms |
5332 KB |
Output is correct |
13 |
Correct |
24 ms |
5460 KB |
Output is correct |
14 |
Correct |
27 ms |
5332 KB |
Output is correct |
15 |
Correct |
3 ms |
5380 KB |
Output is correct |
16 |
Correct |
4 ms |
5332 KB |
Output is correct |
17 |
Correct |
5 ms |
5332 KB |
Output is correct |
18 |
Correct |
4 ms |
5332 KB |
Output is correct |
19 |
Correct |
4 ms |
5384 KB |
Output is correct |
20 |
Correct |
5 ms |
5384 KB |
Output is correct |
21 |
Correct |
4 ms |
5332 KB |
Output is correct |
22 |
Correct |
4 ms |
5380 KB |
Output is correct |
23 |
Correct |
5 ms |
5384 KB |
Output is correct |
24 |
Correct |
83 ms |
5388 KB |
Output is correct |
25 |
Execution timed out |
2069 ms |
5652 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5332 KB |
Output is correct |
2 |
Correct |
3 ms |
5332 KB |
Output is correct |
3 |
Correct |
3 ms |
5332 KB |
Output is correct |
4 |
Correct |
3 ms |
5332 KB |
Output is correct |
5 |
Correct |
3 ms |
5332 KB |
Output is correct |
6 |
Correct |
3 ms |
5332 KB |
Output is correct |
7 |
Correct |
11 ms |
5528 KB |
Output is correct |
8 |
Correct |
6 ms |
5384 KB |
Output is correct |
9 |
Correct |
9 ms |
5332 KB |
Output is correct |
10 |
Correct |
8 ms |
5332 KB |
Output is correct |
11 |
Correct |
22 ms |
5332 KB |
Output is correct |
12 |
Correct |
23 ms |
5332 KB |
Output is correct |
13 |
Correct |
24 ms |
5460 KB |
Output is correct |
14 |
Correct |
27 ms |
5332 KB |
Output is correct |
15 |
Correct |
3 ms |
5380 KB |
Output is correct |
16 |
Correct |
4 ms |
5332 KB |
Output is correct |
17 |
Correct |
5 ms |
5332 KB |
Output is correct |
18 |
Correct |
4 ms |
5332 KB |
Output is correct |
19 |
Correct |
4 ms |
5384 KB |
Output is correct |
20 |
Correct |
5 ms |
5384 KB |
Output is correct |
21 |
Correct |
4 ms |
5332 KB |
Output is correct |
22 |
Correct |
4 ms |
5380 KB |
Output is correct |
23 |
Correct |
5 ms |
5384 KB |
Output is correct |
24 |
Correct |
83 ms |
5388 KB |
Output is correct |
25 |
Execution timed out |
2069 ms |
5652 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |