# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
962335 |
2024-04-13T10:45:54 Z |
Cookie |
Hard route (IZhO17_road) |
C++14 |
|
5 ms |
20032 KB |
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
#define sz(a) (int)a.size()
#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mpp make_pair
const ld PI = 3.14159265359, prec = 1e-9;;
//using u128 = __uint128_t;
//const int x[4] = {1, 0, -1, 0};
//const int y[4] = {0, -1, 0, 1};
const ll mod = 1e9 + 7, pr = 31;
const int mxn = 5e5 + 5, mxq = 1e5 + 5, sq = 450, mxv = 5e4 + 1;
//const int base = (1 <<18);
const ll inf = 1e9 + 5, neg = -69420, inf2 = 1e14;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// have fun!
struct DP{
ll mx, cnt;
DP(){
mx = 0; cnt = 1;
}
DP(ll _mx, ll _cnt){
mx = _mx; cnt = _cnt;
}
};
DP comb(DP a, DP b){
if(a.mx > b.mx)return(a);
if(a.mx < b.mx)return(b);
return(DP(a.mx, a.cnt + b.cnt));
}
int n;
DP dp[mxn + 1];
vt<int>adj[mxn + 1];
void dfs(int s, int pre){
for(auto i: adj[s]){
if(i != pre){
dfs(i, s);
dp[s] = comb(dp[s], DP(dp[i].mx + 1, dp[i].cnt));
}
}
}
DP res;
bool cmp(int a, int b){
return(dp[a].mx > dp[b].mx);
}
void dfs2(int s, int pre){
sort(ALL(adj[s]), cmp);
if(sz(adj[s]) >= 3){
ll mx0 = dp[adj[s][0]].mx + 1, mx1 = dp[adj[s][1]].mx + 1, mx2 = dp[adj[s][2]].mx + 1;
ll pref = 0;
DP cand = DP(mx0 * (mx1 + mx2), 0);
for(auto i: adj[s]){
if(dp[i].mx + 1 == mx1)cand.cnt += pref * dp[i].cnt;
if(dp[i].mx + 1 == mx2)pref += dp[i].cnt;
}
res = comb(res, cand);
}
DP fir, sec = DP(0, 0);
for(auto i: adj[s]){
if(dp[i].mx + 1 > fir.mx){
sec = fir;
fir = DP(dp[i].mx + 1, dp[i].cnt);
}
else if(dp[i].mx + 1 == fir.mx)fir.cnt += dp[i].cnt;
else if(dp[i].mx + 1 > sec.mx)sec = DP(dp[i].mx + 1, dp[i].cnt);
else if(dp[i].mx + 1 == sec.mx)sec.cnt += dp[i].cnt;
}
DP rem = dp[s];
for(auto i: adj[s]){
if(i != pre){
dp[s] = fir;
if(dp[s].mx == dp[i].mx + 1){
if(dp[s].cnt == dp[i].cnt){
dp[s] = sec;
}else{
dp[s].cnt -= dp[i].cnt;
}
}
dfs2(i, s);
}
}
dp[s] = rem;
}
void solve(){
cin >> n;
for(int i = 1; i < n; i++){
int u, v; cin >> u >> v;
adj[u].pb(v); adj[v].pb(u);
}
dfs(1, -1); dfs2(1, -1);
cout << res.mx << " " << res.cnt;
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("netw.inp", "r", stdin);
//freopen("netw.out", "w", stdout)
int tt; tt = 1;
while(tt--){
solve();
}
return(0);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
19804 KB |
Output is correct |
2 |
Correct |
5 ms |
19804 KB |
Output is correct |
3 |
Correct |
4 ms |
20032 KB |
Output is correct |
4 |
Correct |
4 ms |
19800 KB |
Output is correct |
5 |
Correct |
4 ms |
19804 KB |
Output is correct |
6 |
Correct |
4 ms |
19804 KB |
Output is correct |
7 |
Incorrect |
4 ms |
19804 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
19804 KB |
Output is correct |
2 |
Correct |
5 ms |
19804 KB |
Output is correct |
3 |
Correct |
4 ms |
20032 KB |
Output is correct |
4 |
Correct |
4 ms |
19800 KB |
Output is correct |
5 |
Correct |
4 ms |
19804 KB |
Output is correct |
6 |
Correct |
4 ms |
19804 KB |
Output is correct |
7 |
Incorrect |
4 ms |
19804 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
19804 KB |
Output is correct |
2 |
Correct |
5 ms |
19804 KB |
Output is correct |
3 |
Correct |
4 ms |
20032 KB |
Output is correct |
4 |
Correct |
4 ms |
19800 KB |
Output is correct |
5 |
Correct |
4 ms |
19804 KB |
Output is correct |
6 |
Correct |
4 ms |
19804 KB |
Output is correct |
7 |
Incorrect |
4 ms |
19804 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |