Submission #38716

#TimeUsernameProblemLanguageResultExecution timeMemory
38716grumpy_gordonHard route (IZhO17_road)C++14
100 / 100
1749 ms156532 KiB
/* .:*+=%@@@@@@=-. .:=@#@@@#@@#######%==*. .-=####@######%*-.....:%##%. .*@###########%+:--........-%@- .*@##############@+--.........-:%- .+##################@==%%%%=+*:----+. .-@####################%++%@@@@@=+**%@@* .%###################@%%@@@###@%+:--%@@%. -@###################@%%%%*::*%++:-----=@+. -#####################@%=++++++*:-------.-=: .+####################@%++*::-:::--::*:::***=: .@#####################%=++*::::-:::++*=##@@#@- ..#####################@%%=++**:::::**+%@#@%%##-.. .%####################@@%=+++*+****::*=@######@. .=######################@%%==+==++**+=@%@##@###+:... -#######################@@@%%%===++=@@@%=++===*::--... -########################@@@@@@@%==%%=++==@@:::::*:--. ..:#########################@@@@@@%%======++++::-..:-.--... %#############################@###@%%@@%==%=%*----.--.::---. #############################################*-:*:-:---*---- . #############################################*--*--:---*---:-. #############################################+--::--::-*::-::. ###########################################+:*-.---.---.:---*-.. ###########################################**:-----------------. ##########################################@::**:--::::::--:::::- ###########################################:--:*:::::::::**::*+* ###########################################=:::***::::::**:::*+* ############################@@@@@@#########@+****::::********+++ ############################@%%%%%@@@@@@@###%+***::::::::***+==+ ############################@%%%%%%%%%%%@####=+:::-::::-::*+=%%+ #############################@%%%%%%%%%%@#####=::--------:*=%@%+ %###########################@%%%%==%%%%%%@##@#=:------..-:+%@@%= ---------------------------------------------- -------------------------------------------- ---------------------------------------------- -------------------------------------------- ---------------------------------------------- o###########oo o##" ""##o o#" "## o#" "#o #" ## ## "## #" ## # ################### # # # # # # # # # # # # # #o # "#o ## "#o ## "#o o#" "#o ## "#o o#" "#ooo ooo#######oo ############### "######o o###"" "###o # ### o###o oooo ### oo####" o###**# #**# ############" ""##""""""""""########### # # oooooooo#"#** ## # # # # # ** ## # #o# #o# *****###ooo# ## ## o###o ## o##***## o########## #***#**##o o##" ""### #***##***# o#######o ### oo#### ##**####*# o##" ""#############"" ##****### ##" ## ##*##*### ## ### ##### ## ## ### # ## # ## ## # ## ## ## ### ## ###oo ### ""### ### ### */ #include <bits/stdc++.h> //#pragma GCC optimize("O3") //#pragma GCC optimize("Ofast,no-stack-protector") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx") //#pragma GCC target("avx,tune=native") //float __attribute__((aligned(32))) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; int mysqrt(ll x){ int l = 0, r = 1e9 + 1; while (r - l > 1){ int m = (l + r) / 2; if (m * (ll)m <= x) l = m; else r = m; } return l; } mt19937 rnd(1337); ll AR = 19, BR = 13, CR = 23, XR = 228, YR = 322, MODR = 1e9 + 993; ll myrand(){ ll ZR = (XR * AR + YR * BR + CR) % MODR; XR = YR; YR = ZR; return ZR; } const int Mod = 1e9 + 7; int bpow(int x, int y){ if (y == 0) return 1; if (y == 1) return x; int ret = bpow(x, y >> 1); ret = (ret * (ll)ret) % Mod; if (y & 1) ret = (ret * (ll)x) % Mod; return ret; } int bdiv(int x, int y){ return (x * (ll)bpow(y, Mod - 2)) % Mod; } void setmin(int &x, int y){ x = min(x, y); } void setmax(int &x, int y){ x = max(x, y); } const ll llinf = 1e18 + 100; const int maxn = 5e5 + 100, maxw = 1e6 + 100, inf = 2e9 + 100, sq = 300, mod = 1e9 + 7, LG = 17; pair<int, int> get(pair<int, int> x, pair<int, int> y){ if (x.first < y.first) swap(x, y); if (x.first == y.first) x.second += y.second; return x; } int n; vector<int> e[maxn]; vector<pair<int, int> > q[maxn]; vector<pair<int, int> > g[maxn]; ll ans, answer = 1; void upd(ll x, ll y){ if (ans > x) return; if (ans < x) ans = x, answer = 0; answer += y; } pair<int, int> dfs0(int v, int par){ pair<int, int> ret = {0, 0}; q[v].resize(e[v].size()); g[v].resize(e[v].size()); for (int i = 0; i < e[v].size(); i++) if (e[v][i] != par){ q[v][i] = dfs0(e[v][i], v); q[v][i].first++; ret = get(ret, q[v][i]); } if (!ret.first) ret.second++; return ret; } void dfs(int v, int par, pair<int, int> pv){ int a = 0, b = 0, c = 0, x = 0, y = 0, z = 0, ax = 0, ay = 0, az = 0; ll px = 0, py = 0, pz = 0; if (pv.first) a = pv.first, x = 1, ax = pv.second; for (int i = 0; i < e[v].size(); i++) if (e[v][i] != par){ if (a < q[v][i].first){ c = b; b = a; z = y; y = x; az = ay; ay = ax; pz = py; py = px; a = q[v][i].first; x = 1; px = 0; ax = q[v][i].second; } else if (a == q[v][i].first){ x++; px += ax * (ll)q[v][i].second; ax += q[v][i].second; } else if (b < q[v][i].first){ c = b; z = y; az = ay; pz = py; b = q[v][i].first; y = 1; py = 0; ay = q[v][i].second; } else if (b == q[v][i].first){ y++; py += ay * (ll)q[v][i].second; ay += q[v][i].second; } else if (c < q[v][i].first){ c = q[v][i].first; z = 1; pz = 0; az = q[v][i].second; } else if (c == q[v][i].first){ z++; pz += az * (ll)q[v][i].second; az += q[v][i].second; } } if (x + y + z > 2){ if (x == 1) if (y == 1) upd(a * (ll)(b + c), ay * (ll)az); else upd(a * (ll)2 * b, py); else if (x > 2) upd(a * (ll)2 * a, px); else upd(a * (ll)(a + b), ax * (ll)ay); } g[v][0] = q[v][0]; for (int i = 1; i < q[v].size(); i++) g[v][i] = get(q[v][i], g[v][i - 1]); pair<int, int> w = pv; if (!w.first) w.second++; for (int i = q[v].size() - 1; i >= 0; i--) if (e[v][i] != par){ pair<int, int> o = w; if (i) o = get(o, g[v][i - 1]); o.first++; dfs(e[v][i], v, o); w = get(w, q[v][i]); } } int dist, dans; void getfar(int v, int par, int ds){ if (ds > dist) dist = ds, dans = v; for (auto i : e[v]) if (i != par) getfar(i, v, ds + 1); } ll X, Y = 1; bool vis[maxn]; int pt[maxn]; int getf(int v, int par){ int ret = 0; for (auto i : e[v]) if (i != par) ret = max(ret, getf(i, v) + 1); return ret; } void go(int v, int par, int len){ pt[len] = v; if (len && e[v].size() == 1){ for (int i = 0; i < n; i++) vis[i] = 0; for (int i = 0; i <= len; i++) vis[pt[i]] = 1; int z = 0; for (int i = 0; i <= len; i++){ int u = pt[i]; for (auto j : e[u]) if (!vis[j]) z = max(z, getf(j, u) + 1); } if (X < z * len) X = z * len, Y = 1; else if (X == z * len) Y++; return; } for (auto i : e[v]) if (i != par) go(i, v, len + 1); } int main() { #ifdef ONPC //ifstream cin("a.in"); //ofstream cout("a.out"); freopen("a.in", "r", stdin); freopen("a.out", "w", stdout); #else //ifstream cin("road.in"); //ofstream cout("road.out"); //freopen("money.in", "r", stdin); //freopen("money.out", "w", stdout); #endif // ONPC ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i < n; i++){ int v, u; cin >> v >> u; v--; u--; e[v].push_back(u); e[u].push_back(v); } dfs0(0, -1); dfs(0, -1, {0, 0}); //for (int i = 0; i < n; i++) //if (e[i].size() == 1) // go(i, -1, 0); //Y /= 2; //assert(ans == X); //assert(Y == answer); //cout << X << ' ' << Y << ' ' << answer; cout << ans << ' ' << answer; }

Compilation message (stderr)

road.cpp: In function 'std::pair<int, int> dfs0(int, int)':
road.cpp:185:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < e[v].size(); i++)
                       ^
road.cpp: In function 'void dfs(int, int, std::pair<int, int>)':
road.cpp:201:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < e[v].size(); i++)
                       ^
road.cpp:267:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 1; i < q[v].size(); i++)
                       ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...