QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#75447 | #5313. Please Save Pigeland | team1377# | RE | 2ms | 16880kb | C++14 | 2.2kb | 2023-02-05 10:51:43 | 2023-02-05 10:51:46 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std; const int maxn = 5e5 + 5; typedef long long ll;
int n, k, c[maxn];
struct edge { int v, w; }; vector<edge> T[maxn];
inline ll gcd(ll x, ll y)
{
return !y ? x : gcd(y, x % y);
}
struct GCD
{
ll x, y;
bool hv;
GCD operator +(const GCD &b) const
{
if (!hv) return b;
else if (!b.hv) return *this;
else
{
return { x, gcd(gcd(y, b.y), abs(b.x - x)), 1 };
}
}
inline GCD add(int z) { return { x + z, y, hv }; }
};
int vl[maxn];
int S1[maxn]; ll S2[maxn];
GCD G[maxn];
void dp0(int u, int fa)
{
S1[u] = vl[u];
if (vl[u]) G[u].hv = 1;
for (auto e : T[u]) if (e.v != fa)
{
dp0(e.v, u);
S1[u] += S1[e.v];
S2[u] += S2[e.v] + S1[e.v] * e.w;
G[u] = G[u] + G[e.v].add(e.w);
}
}
GCD H[maxn]; ll S[maxn];
void dp1(int u, int fa)
{
// cerr << u << ' ' << H[u].x << ' ' << H[u].y << ' ' << H[u].hv << ' ' << G[u].x << ' ' << G[u].y << ' ' << G[u].hv << endl;
GCD t = { 0, 0, 0 };
for (auto e : T[u]) if (e.v != fa)
{
H[e.v] = H[e.v] + t.add(e.w);
t = t + G[e.v].add(e.w);
}
reverse(T[u].begin(), T[u].end()), t = { 0, 0, 0 };
for (auto e : T[u]) if (e.v != fa)
{
H[e.v] = H[e.v] + t.add(e.w);
t = t + G[e.v].add(e.w);
}
GCD add = { 0, 0, 0 }; if (vl[u]) add.hv = 1; add = add + H[u];
for (auto e : T[u]) if (e.v != fa)
{
H[e.v] = H[e.v] + add.add(e.w);
S[e.v] = S[u] + ll(k - 2 * S1[e.v]) * e.w;
dp1(e.v, u);
}
}
int main()
{
scanf("%d%d", &n, &k);
for (int i = 1; i <= k; i++) scanf("%d", c + i);
for (int i = 1, u, v, w; i < n; i++) scanf("%d%d%d", &u, &v, &w), T[u].push_back({ v, w }), T[v].push_back({ u, w });
for (int i = 1; i <= k; i++) vl[c[i]]++;
dp0(1, 0), S[1] = S2[1], dp1(1, 0);
ll ans = 1e18;
for (int i = 1; i <= n; i++)
{
G[i] = G[i] + H[i];
// cerr << i << ' ' << S[i] << ' ' << abs(gcd(G[i].x, G[i].y)) << endl;
ans = min(ans, S[i] / abs(gcd(G[i].x, G[i].y)));
}
return 0 * printf("%lld\n", ans * 2);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 16660kb
input:
5 3 3 4 5 1 2 2 2 3 4 2 5 4 3 4 6
output:
8
result:
ok 1 number(s): "8"
Test #2:
score: 0
Accepted
time: 2ms
memory: 16880kb
input:
10 3 1 7 10 7 6 3 1 8 3 3 6 3 8 6 2 4 1 1 10 6 4 2 8 3 9 10 3 5 10 3
output:
24
result:
ok 1 number(s): "24"
Test #3:
score: -100
Runtime Error
input:
1 1 1