제출 #764913

#제출 시각아이디문제언어결과실행 시간메모리
764913vjudge1Roadside Advertisements (NOI17_roadsideadverts)C++17
7 / 100
1077 ms26484 KiB
//Bismillahir-Rahmanir-Rahim # include <bits/stdc++.h> # define pb push_back # define ff first # define ss second # define nl "\n" # define sz(x) ((int)(x).size()) # define deb(x) cerr << #x << " = " << x << endl; # define pll pair <ll, ll> # define pii pair <int, int> typedef long long ll; typedef unsigned long long ull; typedef long double ld; const ll maxn = 5e4 + 2; const ll inf = 2e18 + 0; const ll mod = 1e9 + 7; const ll dx[] = {-1, 1, 0, 0}; const ll dy[] = {0, 0, -1, 1}; const ll P = 67; using namespace std; ll n, q, timer; vector <pll> g[maxn]; ll tin[maxn]; ll tout[maxn]; ll up[maxn][17]; ll sum[maxn][17]; ll d[maxn]; map <pll, ll> mp; bool used[maxn]; ll ww[maxn]; void dfs (ll v = 1, ll pa = 1) { up[v][0] = pa; d[v] = d[pa] + 1; tin[v] = ++timer; for (ll j = 1; j <= 16; ++j) { up[v][j] = up[up[v][j - 1]][j - 1]; sum[v][j] = sum[v][j - 1] + sum[up[v][j - 1]][j - 1]; } for (pll it : g[v]) { ll to = it.ff, w = it.ss; if (to == pa) continue; sum[to][0] = w; dfs(to, v); } tout[v] = timer; } bool anc (ll u, ll v) { return tin[u] <= tin[v] && tout[v] <= tout[u]; } pll lca (ll u, ll v) { if (d[u] < d[v]) swap(u, v); ll k = d[u] - d[v]; ll summ = 0; for (ll j = 16; j >= 0; --j) { if (k & (1LL << j)) { summ += sum[u][j]; u = up[u][j]; } } for (ll j = 16; j >= 0; --j) { if (up[u][j] != up[v][j]) { summ += sum[u][j]; summ += sum[u][j]; u = up[u][j]; v = up[v][j]; } } return {up[v][0], summ}; } void ma1n (/* SABR */) { cin >> n; for (ll i = 1; i < n; ++i) { ll u, v; cin >> u >> v >> ww[i]; g[u].pb({v, ww[i]}); g[v].pb({u, ww[i]}); mp[{u, v}] = i; mp[{v, u}] = i; } dfs(); cin >> q; while (q--) { vector <ll> v(5); for (ll i = 0; i < 5; ++i) { cin >> v[i]; } ll lc = lca(v[0], v[1]).ff; for (ll i = 0; i < 5; ++i) { for (ll j = i + 1; j < 5; ++j) { lc = lca(lc, lca(v[i], v[j]).ff).ff; } } memset(used, 0, sizeof used); for (ll i = 0; i < 5; ++i) { ll cur = v[i]; while (cur != lc) { used[mp[{cur, up[cur][0]}]] = 1; cur = up[cur][0]; } } ll res = 0; for (ll i = 1; i < n; ++i) { if (used[i]) { res += ww[i]; } } cout << res << nl; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); // freopen("file.in", "r", stdin); // freopen("file.out", "w", stdout); int ttt = 1; // cin >> ttt; for (int test = 1; test <= ttt; ++test) { // cout << "Case " << test << ":" << '\n'; ma1n(); } return 0; } // 998batrr | BbIWEJI 3A TObOU!!! // tourist | BbIWEJI 3A TObOU!!!
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...