Submission #1260293

#TimeUsernameProblemLanguageResultExecution timeMemory
1260293thdh__Hard route (IZhO17_road)C++20
100 / 100
1417 ms186972 KiB
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define eb emplace_back
#define pu push
#define ins insert
#define fi first
#define se second
#define all(a) a.begin(),a.end()
#define bruh ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fu(x,a,b) for (auto x=a;x<=b;x++)
#define fd(x,a,b) for (auto x=a;x>=b;x--)
#define int ll

using namespace std;
//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());

/*
Competitive Programming notes that I need to study & fix my dumbass self:

1. Coding:
- Always be sure to check the memory of arrays (maybe use vectors), for loops
- Always try to maximize the memory if possible, even if you are going for subtasks
- Do not exploit #define int long long, it will kill you

2. Stress: 
- Always try generating big testcases and try if they run

3. Time management:
- Don't overcommit or undercommit, always spend a certain amount of time to think a problem, don't just look at it and say I'm fucked
- Do not spend too much time coding brute-force solutions, they should be easily-codable solutions that don't take up too much time

Time management schedule:
Offline / LAH days (4 problems - 3h):
15' thinking of solution / idea
1. no idea: skip
2. yes idea: continue thinking for <= 15'

+ implementing: <= 20'
+ brute-force: <= 5'
+ test generator: <= 5'

I hate offline because I am dumb
*/

typedef pair<int, int> ii;
const int N = 5e5+5;
const int B = 750;
const int mod = 1e9+7;
const int inf = 1e18;
using cd = complex<double>;
const long double PI = acos(-1);
int power(int a,int b) {ll x = 1;if (a >= mod) a%=mod; while (b) {if (b & 1) x = x*a % mod;a = a*a % mod;b>>=1;}return x;} 

int n;
vector<int> adj[N];
int tin[N], tout[N], timer = 0, rev[N];
int dist[N], depth[N], cnt[N];
int ans = 0, cres =  1;

ii st[4*N];
int lazy[4*N];

ii merge(ii a, ii b) 
{
	if (a.fi == b.fi) return {a.fi, a.se+b.se};
	return max(a, b);
}

void push(int id) 
{
	st[id*2].fi += lazy[id]; st[id*2+1].fi += lazy[id];
	lazy[id*2] += lazy[id], lazy[id*2+1] += lazy[id];
	lazy[id] = 0;
}

void build(int id, int l, int r) 
{
	if (l == r) 
	{
		st[id] = {dist[rev[l]], 1};
		return;
	}
	int mid = l+r>>1;
	build(id*2, l, mid), build(id*2+1, mid+1, r);
	st[id] = merge(st[id*2], st[id*2+1]);
}

void update(int id, int l, int r, int u, int v, int val) 
{
	if (v < u) return;
	if (l > v || r < u) return;
	if (u <= l && r <= v) 
	{
		st[id].fi += val;
		lazy[id] += val;
		return;
	}
	push(id);
	int mid = l+r>>1;
	update(id*2, l, mid, u, v, val); update(id*2+1, mid+1, r, u, v, val);
	st[id] = merge(st[id*2], st[id*2+1]);
}

ii get(int id, int l, int r, int u, int v) 
{
	if (v < u) return {0, 0};
	if (l > v || r < u) return {0, 0};
	if (u <= l && r <= v) return st[id];
	push(id);
	int mid = l+r>>1;
	return merge(get(id*2, l, mid, u, v), get(id*2+1, mid+1, r, u, v));
}

void predfs(int u, int p) 
{
	depth[u] = 0, cnt[u] = 1;
	tin[u] = ++timer;
	rev[timer] = u;
	for (auto v : adj[u]) 
	{
		if (v == p) continue;
		dist[v] = dist[u] + 1;
		predfs(v, u);
		if (depth[v]+1 > depth[u]) depth[u] = depth[v]+1, cnt[u] = cnt[v];
		else if (depth[v]+1 == depth[u]) cnt[u] += cnt[v]; 
	}
	tout[u] = timer;
}

void dfs(int u, int p) 
{
	if (adj[u].size() == 1) return;
	// cout<<u<<endl;
	// for (int i = 1; i <= n; i++) cout<<get(1,1,n,tin[i],tin[i]).fi<<" ";
	// cout<<endl;
	ii m1 = {-1, -1}, m2 = {-1, -1}, m3 = {-1, -1};
	vector<ii> a;
	if (p) 
	{
		a.pb(merge(get(1, 1, n, 1, tin[u]-1), get(1, 1, n, tout[u]+1, n)));
		m1 = a.back();
	}
	for (auto v : adj[u]) 
	{
		if (v == p) continue;
		ii cur = {depth[v]+1, cnt[v]};
		if (cur > m1) m3 = m2, m2 = m1, m1 = cur;
		else if (cur > m2) m3 = m2, m2 = cur; 
		else if (cur > m3) m3 = cur;
		a.pb(cur);
	}
	int c12 = 0, c23 = 0, c13 = 0, c1 = 0, c2 = 0, c3 = 0;
	// cout<<u<<endl;
	for (auto i : a) 
	{
		// cout<<i.fi<<" "<<i.se<<" "<<c1<<" "<<c2<<" "<<c3<<endl;
		if (m1.fi == m2.fi && i.fi == m1.fi) c12 += i.se * c1;
		if (m1.fi == m3.fi && i.fi == m1.fi) c13 += i.se * c1;
		if (m2.fi == m3.fi && i.fi == m2.fi) c23 += i.se * c2;
		if (i.fi == m1.fi) c1 += i.se;
		if (i.fi == m2.fi) c2 += i.se;
		if (i.fi == m3.fi) c3 += i.se;
	}
	// cout<<c23<<endl;
	if (m1.fi != m2.fi) c12 = c1 * c2;
	if (m1.fi != m3.fi) c13 = c1 * c3;
	if (m2.fi != m3.fi) c23 = c2 * c3;
	// cout<<u<<" "<<m1.fi<<" "<<m2.fi<<" "<<m3.fi<<" "<<c12<<" "<<c13<<" "<<c23<<endl;
	if (m3.fi != -1) 
	{
		int val = m1.fi * (m2.fi + m3.fi);
		if (val > ans) 
		{
			ans = val;
			cres = c23;
		} else if (val == ans) 
		{
			cres += c23;
		}
	}
	for (auto v : adj[u]) 
	{
		if (v == p) continue;
		update(1, 1, n, 1, tin[v]-1, 1); update(1, 1, n, tout[v]+1, n, 1);
		update(1, 1, n, tin[v], tout[v], -1);
		dfs(v, u);
		update(1, 1, n, tin[v], tout[v], 1);
		update(1, 1, n, 1, tin[v]-1, -1); update(1, 1, n, tout[v]+1, n, -1);
	}
}

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);
	}
	if (n == 2) 
	{
		cout<<"0 1"; return;
	}
	int root = 1, l = 0;
	for (int i = 1; i <= n; i++) 
	{
		if (adj[i].size() > 1) root = i;
		else l++;
	}
	predfs(root, 0);
	// for (int i = 1; i <= n; i++) cout<<dist[i]<<" ";
	// cout<<endl;
	build(1,1,n);
	// for (int i = 1; i <= n; i++) cout<<depth[i]<<" "<<cnt[i]<<endl;
	dfs(root, 0);
	cout<<ans<<" "<<cres;
}

/*
Go through the mistakes you usually make and revise your code, for god's sake...
*/

signed main()
{
	bruh
	//freopen("input.inp","r",stdin);
	//freopen("output.inp","w",stdout);
	int t = 1;
	// cin>>t;
	while (t--)
	{
		solve();
		cout<<"\n";
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...