#include "factories.h"
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define FF first
#define SS second
#define all(a) a.begin(), a.end()
#define mod (ll)(1000000007)
// #define MAX_N 500000
// #define MAX_Q 100000
// #define MAX_SUM_ST 1000000
// #define MAX_VALUE 1000000000
// static int N, Q;
// static int A[MAX_N], B[MAX_N], D[MAX_N];
// static int S[MAX_N];
// static int T[MAX_N];
// static int X[MAX_SUM_ST];
// static int Y[MAX_SUM_ST];
// static int Qx[MAX_N];
// static int Qy[MAX_N];
const ll inf=1e18, lg=20;
int n;
vector<ll>Dis, dpth;
vector<vector<ll>>AA, up, Lca;
map<pair<int, int>, ll>mp;
void pre(){
Dis.clear(), dpth.clear(), AA.clear(), up.clear(), Lca.clear(), mp.clear();
Dis.resize(n, inf), dpth.resize(n, inf), AA.resize(n), up.resize(n, vector<ll>(lg+1)), Lca.resize(n, vector<ll>(lg+1));
}
void bfs(int ind){
queue<int>q;
q.push(ind);
up[ind][0]=ind, Lca[ind][0]=0;
Dis[ind]=dpth[ind]=0;
while(!q.empty()){
int u=q.front();
q.pop();
for(int j=1;j<=lg;j++){
up[u][j]=up[up[u][j-1]][j-1];
Lca[u][j]=Lca[u][j-1]+Lca[up[u][j-1]][j-1];
}
for(int i=0;i<AA[u].size();i++){
int v=AA[u][i];
if(dpth[u]+1<dpth[v]){
dpth[v]=dpth[u]+1, Dis[v]=Dis[u]+mp[{u, v}];
up[v][0]=u, Lca[v][0]=mp[{u, v}];
q.push(v);
}
}
}
}
ll get_dis(int u, int v){
// u
// v
ll res=0;
if(dpth[u]>dpth[v])
swap(u, v);
for(int j=lg;j>=0;j--){
if(dpth[up[v][j]]>=dpth[u])
res+=Lca[v][j], v=up[v][j];
}
if(u==v)
return res;
for(int j=lg;j>=0;j--){
if(up[u][j]!=up[v][j])
res+=Lca[u][j]+Lca[v][j], u=up[u][j], v=up[v][j];
}
res+=Lca[u][0]+Lca[v][0];
return res;
}
void Init(int N, int A[], int B[], int D[]) {
n=N;
pre();
for(int i=0;i<n-1;i++){
int u=A[i], v=B[i], w=D[i];
AA[u].push_back(v);
AA[v].push_back(u);
mp[{u, v}]=mp[{v, u}]=w;
}
bfs(0);
}
long long Query(int S, int X[], int T, int Y[]) {
ll ans=inf;
for(int i=0;i<S;i++){
for(int j=0;j<T;j++){
ans=min(ans, get_dis(X[i], Y[j]));
}
}
return ans;
}
// int main() {
//
// int i, j, k;
// int STop, TTop;
//
// if (2 != scanf("%d%d", &N, &Q)) {
// fprintf(stderr, "error: cannot read N and Q.\n");
// exit(1);
// }
// if (!(2 <= N && N <= MAX_N)) {
// fprintf(stderr, "error: N is out of bounds.\n");
// exit(1);
// }
// if (!(1 <= Q && Q <= MAX_Q)) {
// fprintf(stderr, "error: Q is out of bounds.\n");
// exit(1);
// }
// for (i = 0; i < N - 1; ++i) {
// if (1 != scanf("%d", &A[i])) {
// fprintf(stderr, "error: cannot read A[%d].\n", i);
// exit(1);
// }
// if (!(0 <= A[i] && A[i] <= N - 1)) {
// fprintf(stderr, "error: A[%d] is out of bounds.\n", i);
// exit(1);
// }
// if (1 != scanf("%d", &B[i])) {
// fprintf(stderr, "error: cannot read B[%d].\n", i);
// exit(1);
// }
// if (!(0 <= B[i] && B[i] <= N - 1)) {
// fprintf(stderr, "error: B[%d] is out of bounds.\n", i);
// exit(1);
// }
// if (A[i] == B[i]) {
// fprintf(stderr, "error: B[%d] is equal to A[%d].\n", i, i);
// exit(1);
// }
// if (1 != scanf("%d", &D[i])) {
// fprintf(stderr, "error: cannot read D[%d].\n", i);
// exit(1);
// }
// if (!(1 <= D[i] && D[i] <= MAX_VALUE)) {
// fprintf(stderr, "error: D[%d] is out of bounds.\n", i);
// exit(1);
// }
// }
//
// STop = 0;
// TTop = 0;
//
// for (j = 0; j < Q; ++j) {
// if (2 != scanf("%d%d", &S[j], &T[j])) {
// fprintf(stderr, "error: cannot read L[%d] and R[%d].\n", j, j);
// exit(1);
// }
//
// if(STop + S[j] > MAX_SUM_ST) {
// fprintf(stderr, "error: S[0] + S[1] + ... + S[%d] is out of bounds.\n", j);
// exit(1);
// }
//
// if(TTop + T[j] > MAX_SUM_ST) {
// fprintf(stderr, "error: T[0] + T[1] + ... + T[%d] is out of bounds.\n", j);
// exit(1);
// }
//
// for (k = 0; k < S[j]; ++k, ++STop) {
// if (1 != scanf("%d", &X[STop])) {
// fprintf(stderr, "error: cannot read X[%d][%d].\n", j, k);
// exit(1);
// }
//
// if (!(0 <= X[STop] && X[STop] <= N - 1)) {
// fprintf(stderr, "error: cannot read X[%d][%d].\n", j, k);
// exit(1);
// }
// }
//
// for (k = 0; k < T[j]; ++k, ++TTop) {
// if (1 != scanf("%d", &Y[TTop])) {
// fprintf(stderr, "error: cannot read Y[%d][%d].\n", j, k);
// exit(1);
// }
//
// if (!(0 <= Y[TTop] && Y[TTop] <= N - 1)) {
// fprintf(stderr, "error: cannot read Y[%d][%d].\n", j, k);
// exit(1);
// }
// }
// }
//
// STop = 0;
// TTop = 0;
// Init(N, A, B, D);
//
// for (j = 0; j < Q; ++j) {
// for (k = 0; k < S[j]; k++) {
// Qx[k] = X[STop++];
// }
// for (k = 0; k < T[j]; k++) {
// Qy[k] = Y[TTop++];
// }
//
// printf("%lld\n", Query(S[j], Qx, T[j], Qy));
// }
//
//
// return 0;
// }
Compilation message
factories.cpp: In function 'void bfs(int)':
factories.cpp:47:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for(int i=0;i<AA[u].size();i++){
| ~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
852 KB |
Output is correct |
2 |
Execution timed out |
8039 ms |
11212 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
596 KB |
Output is correct |
2 |
Correct |
3885 ms |
309912 KB |
Output is correct |
3 |
Execution timed out |
8057 ms |
307060 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
852 KB |
Output is correct |
2 |
Execution timed out |
8039 ms |
11212 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |