#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> Pair;
const int Nmax = 5e5 + 5;
const ll lim = 1e17;
vector<ll> h[Nmax];
vector<int> root[Nmax];
vector<int> v[Nmax], c[Nmax];
int w[Nmax];
bool used[Nmax];
int n, All;
ll best[Nmax];
int up[Nmax], worst[Nmax];
vector<int> nodes;
void dfs0(int node, int dad = -1)
{
w[node] = 1;
nodes.push_back(node);
for(auto it : v[node])
if(!used[it] && it!=dad)
{
dfs0(it, node);
worst[node] = max(worst[node], w[it]);
w[node] += w[it];
}
}
void dfs(int node, int dad = -1)
{
int i;
for(i=0; i<v[node].size(); ++i)
{
int son, cost;
son = v[node][i], cost = c[node][i];
if(used[son] || son == dad) continue;
h[son].push_back(h[node].back() + cost);
root[son].push_back(root[node].back());
dfs(son, node);
}
}
void build(int node)
{
nodes.clear();
dfs0(node);
All = w[node];
Pair centroid = {All, All};
for(auto &it : nodes)
centroid = min(centroid, { max(worst[it], All - w[it]), it });
node = centroid.second;
for(auto &it : nodes) worst[it] = 0;
h[node].push_back(0);
root[node].push_back(node);
dfs(node);
used[node] = 1;
for(auto it : v[node])
if(!used[it])
build(it);
}
void Init(int N, int A[], int B[], int D[])
{
n = N;
int i;
for(i=0; i<n-1; ++i)
{
v[A[i]].push_back(B[i]);
v[B[i]].push_back(A[i]);
c[A[i]].push_back(D[i]);
c[B[i]].push_back(D[i]);
}
for(i=0; i<n; ++i) best[i] = lim;
build(0);
}
static void min_to(ll &x, ll y)
{
if(x > y) x = y;
}
ll Query(int S, int X[], int T, int Y[])
{
int i, j;
vector<pair<int,ll>> S1, S2;
for(i=0; i<S; ++i)
{
int node = X[i];
for(j=0; j<root[node].size(); ++j)
min_to( best[root[node][j]], h[node][j] );
}
ll ans = lim;
for(i=0; i<T; ++i)
{
int node = Y[i];
for(j=0; j<root[node].size(); ++j)
min_to(ans, best[root[node][j]] + h[node][j] );
}
for(i=0; i<S; ++i)
{
int node = X[i];
for(j=0; j<root[node].size(); ++j)
best[root[node][j]] = lim;
}
return ans;
}
Compilation message
factories.cpp: In function 'void dfs(int, int)':
factories.cpp:41:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0; i<v[node].size(); ++i)
~^~~~~~~~~~~~~~~
factories.cpp: In function 'll Query(int, int*, int, int*)':
factories.cpp:111:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j=0; j<root[node].size(); ++j)
~^~~~~~~~~~~~~~~~~~
factories.cpp:121:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j=0; j<root[node].size(); ++j)
~^~~~~~~~~~~~~~~~~~
factories.cpp:129:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j=0; j<root[node].size(); ++j)
~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
47736 KB |
Output is correct |
2 |
Correct |
458 ms |
56540 KB |
Output is correct |
3 |
Correct |
487 ms |
56940 KB |
Output is correct |
4 |
Correct |
484 ms |
56952 KB |
Output is correct |
5 |
Correct |
526 ms |
57320 KB |
Output is correct |
6 |
Correct |
391 ms |
56184 KB |
Output is correct |
7 |
Correct |
481 ms |
56844 KB |
Output is correct |
8 |
Correct |
492 ms |
56952 KB |
Output is correct |
9 |
Correct |
527 ms |
57380 KB |
Output is correct |
10 |
Correct |
392 ms |
56160 KB |
Output is correct |
11 |
Correct |
487 ms |
56856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
47612 KB |
Output is correct |
2 |
Correct |
4130 ms |
206224 KB |
Output is correct |
3 |
Correct |
6035 ms |
258172 KB |
Output is correct |
4 |
Correct |
1555 ms |
136796 KB |
Output is correct |
5 |
Correct |
6832 ms |
323084 KB |
Output is correct |
6 |
Correct |
6225 ms |
271424 KB |
Output is correct |
7 |
Correct |
1450 ms |
100916 KB |
Output is correct |
8 |
Correct |
680 ms |
86376 KB |
Output is correct |
9 |
Correct |
1717 ms |
113684 KB |
Output is correct |
10 |
Correct |
1390 ms |
102140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
47736 KB |
Output is correct |
2 |
Correct |
458 ms |
56540 KB |
Output is correct |
3 |
Correct |
487 ms |
56940 KB |
Output is correct |
4 |
Correct |
484 ms |
56952 KB |
Output is correct |
5 |
Correct |
526 ms |
57320 KB |
Output is correct |
6 |
Correct |
391 ms |
56184 KB |
Output is correct |
7 |
Correct |
481 ms |
56844 KB |
Output is correct |
8 |
Correct |
492 ms |
56952 KB |
Output is correct |
9 |
Correct |
527 ms |
57380 KB |
Output is correct |
10 |
Correct |
392 ms |
56160 KB |
Output is correct |
11 |
Correct |
487 ms |
56856 KB |
Output is correct |
12 |
Correct |
48 ms |
47612 KB |
Output is correct |
13 |
Correct |
4130 ms |
206224 KB |
Output is correct |
14 |
Correct |
6035 ms |
258172 KB |
Output is correct |
15 |
Correct |
1555 ms |
136796 KB |
Output is correct |
16 |
Correct |
6832 ms |
323084 KB |
Output is correct |
17 |
Correct |
6225 ms |
271424 KB |
Output is correct |
18 |
Correct |
1450 ms |
100916 KB |
Output is correct |
19 |
Correct |
680 ms |
86376 KB |
Output is correct |
20 |
Correct |
1717 ms |
113684 KB |
Output is correct |
21 |
Correct |
1390 ms |
102140 KB |
Output is correct |
22 |
Correct |
4724 ms |
217528 KB |
Output is correct |
23 |
Correct |
4928 ms |
221700 KB |
Output is correct |
24 |
Correct |
7176 ms |
271520 KB |
Output is correct |
25 |
Correct |
6857 ms |
275804 KB |
Output is correct |
26 |
Correct |
6910 ms |
271828 KB |
Output is correct |
27 |
Correct |
7593 ms |
338620 KB |
Output is correct |
28 |
Correct |
2172 ms |
153684 KB |
Output is correct |
29 |
Correct |
6476 ms |
272244 KB |
Output is correct |
30 |
Correct |
6679 ms |
272520 KB |
Output is correct |
31 |
Correct |
6778 ms |
272500 KB |
Output is correct |
32 |
Correct |
1783 ms |
114456 KB |
Output is correct |
33 |
Correct |
825 ms |
87160 KB |
Output is correct |
34 |
Correct |
1187 ms |
96204 KB |
Output is correct |
35 |
Correct |
1225 ms |
96736 KB |
Output is correct |
36 |
Correct |
1504 ms |
100100 KB |
Output is correct |
37 |
Correct |
1552 ms |
99956 KB |
Output is correct |