제출 #841819

#제출 시각아이디문제언어결과실행 시간메모리
841819vjudge1Roadside Advertisements (NOI17_roadsideadverts)C++17
7 / 100
41 ms17740 KiB
#include <bits/stdc++.h> #define ll long long #define int ll #define pii pair<int, int> #define ff first #define ss second #define pb push_back #define in insert #define all(x) (x).begin(), (x).end() #define sz(x) (int)x.size() #define deb(x) cout << #x << " = " << x << '\n'; #define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout) using namespace std; // const int dx[] = {0, 1, 0, -1}; // const int dy[] = {1, 0, -1, 0}; const int inf = 1e9; const ll INF = 1e18; const int mod = 1e9 + 7; const int N = 1e5 + 5; int n, q, tin[N], tout[N], timer, up[N][21], pref[N], ans, a[21]; vector <pii> g[N]; void dfs(int v, int p) { tin[v] = ++timer; up[v][0] = p; for(int i = 1; i <= 20; i++) { up[v][i] = up[up[v][i - 1]][i - 1]; } for(auto to:g[v]) { if(to.ff == p) continue; pref[to.ff] = pref[v] + to.ss; dfs(to.ff, v); } tout[v] = timer; } bool check(int v, int i) { for(int j = 1; j <= 5; j++) { if(i == j) continue; if(tin[v] <= tin[a[j]] && tout[a[j]] <= tout[v]) return 0; } return 1; } void solve() { cin >> n; for(int i = 1; i <= n - 1; i++) { int u, v, w; cin >> u >> v >> w; g[u].pb({v, w}); g[v].pb({u, w}); } dfs(1, 1); cin >> q; while(q--) { for(int i = 1; i <= 5; i++) { cin >> a[i]; } ans = 0; for(int i = 1; i <= 5; i++) { int v = a[i]; for(int j = 20; j >= 0; j--) { if(check(up[v][j], i)) v = up[v][j]; } ans += pref[a[i]] - pref[up[v][0]]; } cout << ans << '\n'; } } signed main() { ios_base::sync_with_stdio(0), cin.tie(0); int TT = 1; // cin >> TT; while(TT--) { solve(); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...