#pragma GCC optimize("Ofast,O2,O3")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << "[" << H << "]";
debug_out(T...);
}
#ifdef dddxxz
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
#define SZ(s) ((int)s.size())
#define all(x) (x).begin(), (x).end()
#define revall(x) (x).rbegin(), (x).rend()
clock_t startTime;
double getCurrentTime() {
return (double) (clock() - startTime) / CLOCKS_PER_SEC;
}
#define MP make_pair
typedef long long ll;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const double eps = 0.00001;
const int MOD = 1e9;
const int INF = 1000000101;
const long long LLINF = 1223372000000000555;
const int N = 2e6 + 3e2;
const int M = 5055;
vector<pair<int, int>> g[N];
void dfs(int v, int p, int d, int &mx){
mx = max(mx, d);
for (auto edge : g[v]){
int u = edge.second;
if (u != p) dfs(u, v, d + 1, mx);
}
}
int get(int v, int a, int b){
int sz = g[v].size();
for (int i = sz - 1; i >= 0; i--){
if (g[v][i].second != a && g[v][i].second != b) return g[v][i].first;
}
return 0;
}
int ans = -1, cnt = 0;
void dfs2(int v, int p, int d, int h){
int sz = g[v].size();
if (sz == 1){
int cost = d * h;
if (cost > ans){
ans = cost;
cnt = 1;
} else if (cost == ans) cnt++;
debug(v, cost, d, h);
return;
}
for (auto edge : g[v]){
int u = edge.second, hh = get(v, u, p);
if (u != p) dfs2(u, v, d + 1, min(h, hh));
}
}
void solve(int TC) {
int n;
cin >> n;
for (int i = 1; i < n; i++){
int u, v;
cin >> u >> v;
g[u].emplace_back(0, v);
g[v].emplace_back(0, u);
}
for (int i = 1; i <= n; i++){
for (auto &edge : g[i]){
dfs(edge.second, i, 1, edge.first);
}
sort(all(g[i]));
}
for (int i = 1; i <= n; i++){
if (g[i].size() == 1){
debug(i);
dfs2(g[i][0].second, i, 1, n);
}
}
cout << ans << ' ' << cnt / 2 << endl;
}
int main() {
startTime = clock();
cin.tie(nullptr);
cout.tie(nullptr);
ios_base::sync_with_stdio(false);
bool llololcal = false;
#ifdef dddxxz
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
llololcal = true;
#endif
int TC = 1;
//cin >> TC;
for (int test = 1; test <= TC; test++) {
debug(test);
//cout << "Case #" << test << ": ";
solve(test);
}
if (llololcal) cerr << endl << "Time: " << int(getCurrentTime() * 1000) << " ms" << endl;
return 0;
}
Compilation message
road.cpp: In function 'void dfs2(int, int, int, int)':
road.cpp:19:20: warning: statement has no effect [-Wunused-value]
19 | #define debug(...) 42
| ^~
road.cpp:70:9: note: in expansion of macro 'debug'
70 | debug(v, cost, d, h);
| ^~~~~
road.cpp: In function 'void solve(int)':
road.cpp:19:20: warning: statement has no effect [-Wunused-value]
19 | #define debug(...) 42
| ^~
road.cpp:100:13: note: in expansion of macro 'debug'
100 | debug(i);
| ^~~~~
road.cpp: In function 'int main()':
road.cpp:19:20: warning: statement has no effect [-Wunused-value]
19 | #define debug(...) 42
| ^~
road.cpp:126:9: note: in expansion of macro 'debug'
126 | debug(test);
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
47172 KB |
Output is correct |
2 |
Correct |
27 ms |
47180 KB |
Output is correct |
3 |
Correct |
27 ms |
47236 KB |
Output is correct |
4 |
Correct |
27 ms |
47204 KB |
Output is correct |
5 |
Correct |
28 ms |
47228 KB |
Output is correct |
6 |
Incorrect |
27 ms |
47244 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
47172 KB |
Output is correct |
2 |
Correct |
27 ms |
47180 KB |
Output is correct |
3 |
Correct |
27 ms |
47236 KB |
Output is correct |
4 |
Correct |
27 ms |
47204 KB |
Output is correct |
5 |
Correct |
28 ms |
47228 KB |
Output is correct |
6 |
Incorrect |
27 ms |
47244 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
47172 KB |
Output is correct |
2 |
Correct |
27 ms |
47180 KB |
Output is correct |
3 |
Correct |
27 ms |
47236 KB |
Output is correct |
4 |
Correct |
27 ms |
47204 KB |
Output is correct |
5 |
Correct |
28 ms |
47228 KB |
Output is correct |
6 |
Incorrect |
27 ms |
47244 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |