#include "factories.h"
#include <iostream>
#include <queue>
#include <cassert>
#include <set>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std;
typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(x) x.begin(), x.end()
#define uniq(x) x.erase(unique(all(x)), x.end())
#define index(x, y) (int)(lower_bound(all(x), y) - x.begin())
#define pb push_back
#define _1 first
#define _2 second
#define INF (1LL<<60)
int N;
vector<P> G[500000];
int U[19][500000];
int R[500000];
long long W[500000];
int IN[500000], OUT[500000];
vector<int> T;
void dfs(int x, int p, int r, long long w) {
IN[x] = T.size();
T.pb(x);
R[x] = r, W[x] = w;
U[0][x] = p;
for (P pp : G[x]) if (pp._1 != p) dfs(pp._1, x, r+1, w+pp._2);
OUT[x] = (int)T.size()-1;
}
int lca(int x, int y) {
if (R[x] < R[y]) swap(x, y);
for (int k=18; k>=0; k--) if ((R[x]-R[y])>>k) x = U[k][x];
if (x == y) return x;
for (int k=18; k>=0; k--) if (U[k][x] != U[k][y]) x = U[k][x], y = U[k][y];
return U[0][x];
}
inline long long dist(int x, int y) {
int g = lca(x, y);
return W[x]+W[y]-2LL*W[g];
}
void Init(int NN, int A[], int B[], int D[]) {
N = NN;
rep(i, N-1) {
G[A[i]].pb(P(B[i], D[i]));
G[B[i]].pb(P(A[i], D[i]));
}
dfs(0, -1, 0, 0);
rep(k, 18) rep(x, N) {
if (U[k][x] == -1) U[k+1][x] = -1;
U[k+1][x] = U[k][U[k][x]];
}
}
long long D[500000];
bool isT[500000];
vector<P> G2[500000];
long long Query(int S, int X[], int T, int Y[]) {
//cout<<"query {"; rep(i, S) cout<<X[i]<<",";cout<<"}, {";rep(i, T) cout<<Y[i]<<",";cout<<"}\n";
vector<P> ps;
rep(i, S) ps.pb(P(IN[X[i]], X[i]));
rep(i, T) ps.pb(P(IN[Y[i]], Y[i]));
sort(all(ps)); uniq(ps);
vector<P> new_ps(ps);
rep(i, (int)ps.size()-1) {
int v = lca(ps[i]._2, ps[i+1]._2);
new_ps.pb(P(IN[v], v));
}
swap(new_ps, ps);
sort(all(ps)); uniq(ps);
//cout<<"{"; for (P p : ps) cout<<p._2<<",";cout<<"}\n";
vector<int> st;
for (P p : ps) {
int v = p._2;
D[v] = INF;
while (!st.empty() && !(IN[st.back()] <= IN[v] && IN[v] <= OUT[st.back()])) st.pop_back();
if (!st.empty()) {
int p = st.back();
//cout<<p<<"<->"<<v<<", w="<<W[v]-W[p]<<"\n";
G2[p].pb(P(v, W[v]-W[p]));
G2[v].pb(P(p, W[v]-W[p]));
}
st.pb(v);
}
//while (!st.empty()) st.pop();
st.clear();
st.shrink_to_fit();
rep(i, T) isT[Y[i]] = true;
priority_queue<P, vector<P>, greater<P> > q;
rep(i, S) q.push(P(0, X[i])), D[X[i]] = 0;
long long ans = INF;
while (!q.empty()) {
long long r = q.top()._1;
int x = q.top()._2;
q.pop();
if (D[x] < r) continue;
if (isT[x]) {
ans = r;
break;
}
for (P p : G2[x]) {
int t = p._1, len = p._2;
if (D[t] > r+len) {
D[t] = r+len;
q.push(P(D[t], t));
}
}
}
while (!q.empty()) q.pop();
for (P p : ps) G2[p._2].clear();
for (P p : ps) G2[p._2].shrink_to_fit();
for (P p : ps) G2[p._2].shrink_to_fit();
ps.clear();
ps.shrink_to_fit();
new_ps.clear();
new_ps.shrink_to_fit();
rep(i, T) isT[Y[i]] = false;
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
99000 KB |
Output is correct |
2 |
Correct |
1605 ms |
99468 KB |
Output is correct |
3 |
Correct |
1770 ms |
99264 KB |
Output is correct |
4 |
Incorrect |
1693 ms |
99704 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
99000 KB |
Output is correct |
2 |
Incorrect |
3045 ms |
120836 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5367 ms |
127416 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |