#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false);; cin.tie(NULL)
void selfMax(long long& a, long long b){
a = max(a, b);
}
void selfMin(long long& a, long long b){
a = min(a, b);
}
template<typename T> istream& operator>>(istream& in, vector<T>& a) {for(auto &x : a) in >> x; return in;};
template<typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& x) {return in >> x.first >> x.second;}
template<typename T1, typename T2> ostream& operator<<(ostream& out, const pair<T1, T2>& x) {return out << x.first << ' ' << x.second;}
template<typename T> ostream& operator<<(ostream& out, vector<T>& a) {for(auto &x : a) out << x << ' '; return out;};
template<typename T> ostream& operator<<(ostream& out, vector<vector<T> >& a) {for(auto &x : a) out << x << '\n'; return out;};
template<typename T1, typename T2> ostream& operator<<(ostream& out, vector<pair<T1, T2> >& a) {for(auto &x : a) out << x << '\n'; return out;};
/*
Use DSU dsu(N);
*/
struct DSU {
vector<long long> adj2;
DSU(long long N) { adj2 = vector<long long>(N, -1); }
// get representive component (uses path compression)
long long get(long long x) { return adj2[x] < 0 ? x : adj2[x] = get(adj2[x]); }
bool same_set(long long a, long long b) { return get(a) == get(b); }
long long size(long long x) { return -adj2[get(x)]; }
bool unite(long long x, long long y) { // union by size
x = get(x), y = get(y);
if (x == y) return false;
if (adj2[x] > adj2[y]) swap(x, y);
adj2[x] += adj2[y]; adj2[y] = x;
return true;
}
};
/*
Run with Bit<long long> BIT
*/
template <class T> class BIT {
private:
long long size;
vector<T> bit;
vector<T> arr;
public:
BIT(long long size) : size(size), bit(size + 1), arr(size) {}
void set(long long ind, long long val) { add(ind, val - arr[ind]); }
void add(long long ind, long long val) {
arr[ind] += val;
ind++;
for (; ind <= size; ind += ind & -ind) { bit[ind] += val; }
}
T pref_sum(long long ind) {
ind++;
T total = 0;
for (; ind > 0; ind -= ind & -ind) { total += bit[ind]; }
return total;
}
};
/*
Change Comb for specific Seg tree probs, but run with Seg<long long> Seg;
*/
template<class T> struct Seg {
const T ID = 1e18; T comb(T a, T b) { return min(a,b); }
long long n; vector<T> seg;
void init(long long _n) { n = _n; seg.assign(2*n,ID); }
void pull(long long p) { seg[p] = comb(seg[2*p],seg[2*p+1]); }
void upd(long long p, T val) {
seg[p += n] = val; for (p /= 2; p; p /= 2) pull(p); }
T query(long long l, long long r) {
T ra = ID, rb = ID;
for (l += n, r += n+1; l < r; l /= 2, r /= 2) {
if (l&1) ra = comb(ra,seg[l++]);
if (r&1) rb = comb(seg[--r],rb);
}
return comb(ra,rb);
}
};
const int N = 50001;
int dp[17][N];
vector<pair<int, int> > adj[N];
int level[N];
int timeIn[N];
int timeOut[N];
long long dist[N];
int timeVal = 0;
void dfs(int u, int p){
timeIn[u] = timeVal;
timeVal++;
for(pair<int, int> x : adj[u]){
int v = x.first;
if(v != p){
dp[0][v] = u;
level[v] = level[u] + 1;
dist[v] = dist[u] + (long long)x.second;
dfs(v, u);
}
}
timeOut[u] = timeVal;
timeVal++;
}
int solve(int x, int k){
int val = 0;
while(k > 0){
if(k % 2 == 1){
x = dp[val][x];
}
val++;
k >>= 1;
}
return x;
}
int solve2(int a, int b){
if(level[a] > level[b]){
swap(a, b);
}
int difference = level[b] - level[a];
b = solve(b, difference);
if(a == b){
return a;
}
for(int i = 16; i >= 0; i--){
if(dp[i][a] != dp[i][b]){
a = dp[i][a];
b = dp[i][b];
}
}
return dp[0][a];
}
long long dist2(int a, int b){
return dist[a] + dist[b] - 2 * dist[solve2(a, b)];
}
int main(){
IOS;
int n;
cin >> n;
for(int i = 0; i < n - 1; i++){
int a, b, c;
cin >> a >> b >> c;
adj[a].push_back(make_pair(b, c));
adj[b].push_back(make_pair(a, c));
}
dfs(0, -1);
for(int i = 1; i < 17; i++){
for(int j = 0; j < n; j++){
dp[i][j] = dp[i - 1][dp[i - 1][j]];
}
}
int q;
cin >> q;
while(q--){
vector<pair<int, int> > v;
for(int i = 0; i < 5; i++){
int x;
cin >> x;
v.push_back(make_pair(timeIn[x], x));
}
sort(v.begin(), v.end());
long long ans = 0;
for(int i = 0; i < 5; i++){
ans += dist2(v[i].second, v[(i + 1) % 5].second);
}
cout << ans/2 << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
9160 KB |
Output is correct |
2 |
Correct |
27 ms |
11524 KB |
Output is correct |
3 |
Correct |
28 ms |
11524 KB |
Output is correct |
4 |
Correct |
28 ms |
11524 KB |
Output is correct |
5 |
Correct |
34 ms |
11504 KB |
Output is correct |
6 |
Correct |
29 ms |
11512 KB |
Output is correct |
7 |
Correct |
28 ms |
11524 KB |
Output is correct |
8 |
Correct |
32 ms |
11584 KB |
Output is correct |
9 |
Correct |
28 ms |
11588 KB |
Output is correct |
10 |
Correct |
32 ms |
11572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
7616 KB |
Output is correct |
2 |
Correct |
19 ms |
10696 KB |
Output is correct |
3 |
Correct |
18 ms |
10732 KB |
Output is correct |
4 |
Correct |
22 ms |
10684 KB |
Output is correct |
5 |
Correct |
19 ms |
10728 KB |
Output is correct |
6 |
Correct |
19 ms |
10708 KB |
Output is correct |
7 |
Correct |
18 ms |
10744 KB |
Output is correct |
8 |
Correct |
19 ms |
10692 KB |
Output is correct |
9 |
Correct |
18 ms |
10700 KB |
Output is correct |
10 |
Correct |
22 ms |
10732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
2 |
Correct |
38 ms |
9160 KB |
Output is correct |
3 |
Correct |
27 ms |
11524 KB |
Output is correct |
4 |
Correct |
28 ms |
11524 KB |
Output is correct |
5 |
Correct |
28 ms |
11524 KB |
Output is correct |
6 |
Correct |
34 ms |
11504 KB |
Output is correct |
7 |
Correct |
29 ms |
11512 KB |
Output is correct |
8 |
Correct |
28 ms |
11524 KB |
Output is correct |
9 |
Correct |
32 ms |
11584 KB |
Output is correct |
10 |
Correct |
28 ms |
11588 KB |
Output is correct |
11 |
Correct |
32 ms |
11572 KB |
Output is correct |
12 |
Correct |
21 ms |
7616 KB |
Output is correct |
13 |
Correct |
19 ms |
10696 KB |
Output is correct |
14 |
Correct |
18 ms |
10732 KB |
Output is correct |
15 |
Correct |
22 ms |
10684 KB |
Output is correct |
16 |
Correct |
19 ms |
10728 KB |
Output is correct |
17 |
Correct |
19 ms |
10708 KB |
Output is correct |
18 |
Correct |
18 ms |
10744 KB |
Output is correct |
19 |
Correct |
19 ms |
10692 KB |
Output is correct |
20 |
Correct |
18 ms |
10700 KB |
Output is correct |
21 |
Correct |
22 ms |
10732 KB |
Output is correct |
22 |
Correct |
31 ms |
10060 KB |
Output is correct |
23 |
Correct |
27 ms |
8652 KB |
Output is correct |
24 |
Correct |
39 ms |
11268 KB |
Output is correct |
25 |
Correct |
29 ms |
11064 KB |
Output is correct |
26 |
Correct |
29 ms |
11044 KB |
Output is correct |
27 |
Correct |
29 ms |
11128 KB |
Output is correct |
28 |
Correct |
30 ms |
11064 KB |
Output is correct |
29 |
Correct |
37 ms |
11084 KB |
Output is correct |
30 |
Correct |
30 ms |
11044 KB |
Output is correct |
31 |
Correct |
31 ms |
11040 KB |
Output is correct |