#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=1e15, lg=19;
int n;
ll Dis[MAX_N];
int dpth[MAX_N];
vector<vector<int>>BB(MAX_N), AA(MAX_N);
int up[MAX_N][lg+1];
void pre(){
for(int i=0;i<MAX_N;i++){
Dis[i]=inf, dpth[i]=2*n;
}
}
void bfs(int ind){
queue<int>q;
q.push(ind);
up[ind][0]=ind;
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];
if(up[u][j]==0)
break;
}
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]+BB[u][i];
up[v][0]=u;
q.push(v);
}
}
}
}
int lca(int u, int v){
// u
// v
if(dpth[u]>dpth[v])
swap(u, v);
for(int j=lg;j>=0;j--){
if(dpth[up[v][j]]>=dpth[u])
v=up[v][j];
}
if(u==v)
return u;
for(int j=lg;j>=0;j--){
if(up[u][j]!=up[v][j])
u=up[u][j], v=up[v][j];
}
return up[v][0];
}
inline ll get_dis(int u, int v){
return Dis[u]+Dis[v]-2*Dis[lca(u, v)];
}
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);
BB[u].push_back(w);
BB[v].push_back(w);
}
bfs(0);
}
long long Query(int S, int X[], int T, int Y[]) {
if(S>45 && T>45){
vector<ll>D(n, inf);
vector<bool>In_y(n, 0), Vis(n, 0);
for(int i=0;i<T;i++){
In_y[Y[i]]=1;
}
priority_queue<array<ll, 2>>q;
for(int i=0;i<S;i++){
q.push({0, X[i]});
D[X[i]]=0;
}
while(!q.empty()){
int u=q.top()[1];
q.pop();
if(Vis[u])
continue;
Vis[u]=1;
if(In_y[u])
return D[u];
for(int i=0;i<AA[u].size();i++){
int v=AA[u][i], w=BB[u][i];
if(D[u]+w<D[v]){
D[v]=D[u]+w;
q.push({-D[v], v});
}
}
}
}
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:49:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for(int i=0;i<AA[u].size();i++){
| ~^~~~~~~~~~~~~
factories.cpp: In function 'long long int Query(int, int*, int, int*)':
factories.cpp:113:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
113 | for(int i=0;i<AA[u].size();i++){
| ~^~~~~~~~~~~~~
factories.cpp: At global scope:
factories.cpp:21:12: warning: 'Qy' defined but not used [-Wunused-variable]
21 | static int Qy[MAX_N];
| ^~
factories.cpp:20:12: warning: 'Qx' defined but not used [-Wunused-variable]
20 | static int Qx[MAX_N];
| ^~
factories.cpp:19:12: warning: 'Y' defined but not used [-Wunused-variable]
19 | static int Y[MAX_SUM_ST];
| ^
factories.cpp:18:12: warning: 'X' defined but not used [-Wunused-variable]
18 | static int X[MAX_SUM_ST];
| ^
factories.cpp:17:12: warning: 'T' defined but not used [-Wunused-variable]
17 | static int T[MAX_N];
| ^
factories.cpp:16:12: warning: 'S' defined but not used [-Wunused-variable]
16 | static int S[MAX_N];
| ^
factories.cpp:15:32: warning: 'D' defined but not used [-Wunused-variable]
15 | static int A[MAX_N], B[MAX_N], D[MAX_N];
| ^
factories.cpp:15:22: warning: 'B' defined but not used [-Wunused-variable]
15 | static int A[MAX_N], B[MAX_N], D[MAX_N];
| ^
factories.cpp:15:12: warning: 'A' defined but not used [-Wunused-variable]
15 | static int A[MAX_N], B[MAX_N], D[MAX_N];
| ^
factories.cpp:14:15: warning: 'Q' defined but not used [-Wunused-variable]
14 | static int N, Q;
| ^
factories.cpp:14:12: warning: 'N' defined but not used [-Wunused-variable]
14 | static int N, Q;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
30048 KB |
Output is correct |
2 |
Correct |
485 ms |
38484 KB |
Output is correct |
3 |
Correct |
332 ms |
38444 KB |
Output is correct |
4 |
Correct |
995 ms |
38596 KB |
Output is correct |
5 |
Correct |
436 ms |
38460 KB |
Output is correct |
6 |
Correct |
597 ms |
38980 KB |
Output is correct |
7 |
Correct |
340 ms |
38432 KB |
Output is correct |
8 |
Correct |
756 ms |
38456 KB |
Output is correct |
9 |
Correct |
430 ms |
38488 KB |
Output is correct |
10 |
Correct |
617 ms |
38764 KB |
Output is correct |
11 |
Correct |
311 ms |
38424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
29780 KB |
Output is correct |
2 |
Correct |
1674 ms |
114132 KB |
Output is correct |
3 |
Correct |
3469 ms |
111412 KB |
Output is correct |
4 |
Correct |
1025 ms |
117060 KB |
Output is correct |
5 |
Correct |
3637 ms |
113088 KB |
Output is correct |
6 |
Correct |
2556 ms |
113344 KB |
Output is correct |
7 |
Correct |
5415 ms |
54116 KB |
Output is correct |
8 |
Correct |
1816 ms |
57664 KB |
Output is correct |
9 |
Correct |
4043 ms |
56748 KB |
Output is correct |
10 |
Correct |
3279 ms |
56204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
30048 KB |
Output is correct |
2 |
Correct |
485 ms |
38484 KB |
Output is correct |
3 |
Correct |
332 ms |
38444 KB |
Output is correct |
4 |
Correct |
995 ms |
38596 KB |
Output is correct |
5 |
Correct |
436 ms |
38460 KB |
Output is correct |
6 |
Correct |
597 ms |
38980 KB |
Output is correct |
7 |
Correct |
340 ms |
38432 KB |
Output is correct |
8 |
Correct |
756 ms |
38456 KB |
Output is correct |
9 |
Correct |
430 ms |
38488 KB |
Output is correct |
10 |
Correct |
617 ms |
38764 KB |
Output is correct |
11 |
Correct |
311 ms |
38424 KB |
Output is correct |
12 |
Correct |
19 ms |
29780 KB |
Output is correct |
13 |
Correct |
1674 ms |
114132 KB |
Output is correct |
14 |
Correct |
3469 ms |
111412 KB |
Output is correct |
15 |
Correct |
1025 ms |
117060 KB |
Output is correct |
16 |
Correct |
3637 ms |
113088 KB |
Output is correct |
17 |
Correct |
2556 ms |
113344 KB |
Output is correct |
18 |
Correct |
5415 ms |
54116 KB |
Output is correct |
19 |
Correct |
1816 ms |
57664 KB |
Output is correct |
20 |
Correct |
4043 ms |
56748 KB |
Output is correct |
21 |
Correct |
3279 ms |
56204 KB |
Output is correct |
22 |
Correct |
1321 ms |
123448 KB |
Output is correct |
23 |
Correct |
2067 ms |
127024 KB |
Output is correct |
24 |
Correct |
1461 ms |
122328 KB |
Output is correct |
25 |
Correct |
1628 ms |
123180 KB |
Output is correct |
26 |
Correct |
1604 ms |
118760 KB |
Output is correct |
27 |
Correct |
2135 ms |
123140 KB |
Output is correct |
28 |
Correct |
1589 ms |
139548 KB |
Output is correct |
29 |
Correct |
3532 ms |
118952 KB |
Output is correct |
30 |
Correct |
4381 ms |
118800 KB |
Output is correct |
31 |
Correct |
5323 ms |
118772 KB |
Output is correct |
32 |
Correct |
834 ms |
57144 KB |
Output is correct |
33 |
Correct |
839 ms |
59484 KB |
Output is correct |
34 |
Correct |
587 ms |
54068 KB |
Output is correct |
35 |
Correct |
658 ms |
54128 KB |
Output is correct |
36 |
Correct |
694 ms |
53972 KB |
Output is correct |
37 |
Correct |
658 ms |
53876 KB |
Output is correct |