Submission #1260273

#TimeUsernameProblemLanguageResultExecution timeMemory
1260273thdh__Hard route (IZhO17_road)C++20
0 / 100
6 ms12104 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}; if (p) m1 = merge(get(1, 1, n, 1, tin[u]-1), get(1, 1, n, tout[u]+1, n)); for (auto v : adj[u]) { if (v == p) continue; ii cur = {depth[v]+1, cnt[v]}; if (cur > m1) m3 = merge(m2, m3), m2 = m1, m1 = cur; else if (cur > m2) m3 = merge(m2, m3), m2 = cur; else m3 = merge(m3, cur); } // cout<<u<<" "<<m1.fi<<" "<<m1.se<<" "<<m2.fi<<" "<<m2.se<<" "<<m3.fi<<" "<<m3.se<<endl; if (m3.fi != -1) { int val = max({m1.fi * (m2.fi + m3.fi), m2.fi * (m1.fi + m3.fi), m3.fi * (m1.fi + m2.fi)}); if (val > ans) { ans = val; cres = 0; if (m1.fi * (m2.fi + m3.fi) == val) cres += m2.se * m3.se; if (m2.fi * (m1.fi + m3.fi) == val) cres += m1.se * m3.se; if (m3.fi * (m1.fi + m2.fi) == val) cres += m1.se * m2.se; } else if (val == ans) { if (m1.fi * (m2.fi + m3.fi) == val) cres += m2.se * m3.se; if (m2.fi * (m1.fi + m3.fi) == val) cres += m1.se * m3.se; if (m3.fi * (m1.fi + m2.fi) == val) cres += m1.se * m2.se; } } 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...