QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#286507 | #7961. 一棵树 | ucup-team022 | WA | 402ms | 100400kb | C++17 | 3.7kb | 2023-12-17 23:00:38 | 2023-12-17 23:00:39 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll adder = 0, ans = 1e18;
int n, K, sz[500005], used[500005], dis[500005], pl[500005], o[500005], bel[500005];
vector<int> g[500005];
void Getsz(int x, int fa) {
sz[x] = 1;
for (int y : g[x]) {
if (y == fa || used[y]) continue;
Getsz(y, x), sz[x] += sz[y];
}
}
void Findct(int x, int fa, int all, int &mn, int &ct) {
int mx = 0;
for (int y : g[x]) {
if (y == fa || used[y]) continue;
Findct(y, x, all, mn, ct), mx = max(mx, sz[y]);
}
mx = max(mx, all - sz[x]);
if (mx < mn) mn = mx, ct = x;
}
/*void dfz(int z) {
int mn = 1e9, x = 0;
Getsz(z, 0), Findct(z, 0, sz[z], mn, x), used[x] = 1;
for (int y : g[x])
if (!used[y]) dfz(y);
}*/
void dfs(int x, int fa, int b) {
bel[x] = b;
for (int y : g[x]) {
if (y == fa) continue;
dis[y] = dis[x] + 1;
dfs(y, x, (b == 0 ? y : b));
}
}
vector<int> indis[500005];
int P[500005], V[500005];
vector<int> Calc(int x) {
if (!x) return {};
dis[x] = 0;
dfs(x, 0, 0);
for (int i = 1; i <= n; i++) indis[dis[i]].push_back(i);
int m = 0;
for (int i = n; i >= 0; i--) {
for (int j : indis[i]) pl[++m] = j;
o[i] = P[i] = V[i] = 0, indis[i].clear();
}
// for (int i = 1; i <= n; i++) cout << dis[i] << ' ';
// puts("");
int cho = 0;
ll sum = 1ll * (n - 1) * K;
vector<int> might;
for (int i = 1; i <= n; i++) {
int y = pl[i];
// cout << "Taking:" << y << ' ' << bel[y] << '\n';
if (o[bel[y]] == K / 2) {
if (!P[bel[y]]) P[bel[y]] = y, V[bel[y]] = dis[y];
might.push_back(bel[y]);
// cout << "Might:" << y << ' ' << bel[y] << '\n';
continue;
}
o[bel[y]]++, sum -= 2 * dis[y], cho++;
if (cho == K) break;
}
if (cho == K) ans = min(ans, adder + sum);
sort(might.begin(), might.end());
might.resize(unique(might.begin(), might.end()) - might.begin());
return might;
}
int p1 = 2000000;
char buf[2000005];
int gc() {
if (p1 >= 2000000) fread(buf, 1, 2000000, stdin), p1 = 0;
return buf[p1++];
}
int rd() {
int x = 0;
char ch = gc();
while (ch < '0' || ch > '9') ch = gc();
while (ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = gc();
return x;
}
vector<int> mymight;
void DFS2(int x, int fa) {
if (g[x].size() != 2) {
mymight.push_back(x);
return;
}
for (int y : g[x]) {
if (y == fa) continue;
DFS2(y, x);
}
}
int CC = 0;
void Solve(int z) {
assert(z && !used[z]);
int mn = 1e9, x = 0;
Getsz(z, 0), Findct(z, 0, sz[z], mn, x), used[x] = 1;
vector<int> to = Calc(x);
// cerr << "Calcing:" << x << '\n';
if (to.size() == 2) {
if (g[x].size() != 2) {
CC++;
if (CC > 1) {
while (to.size() > 1) to.pop_back();
}
for (auto i : to)
if (!used[i]) Solve(i);
return;
}
assert(g[x].size() == 2);
Calc(P[to[0]]);
Calc(P[to[1]]);
DFS2(x, 0);
for (auto i : mymight) Calc(i);
return;
}
for (auto i : to) {
if (!used[i]) Solve(i);
}
}
int main() {
n = rd(), K = rd();
if (K == 1) return cout << n - 1, 0;
for (int i = 1, x, y; i < n; i++) {
x = rd(), y = rd();
g[x].push_back(y);
g[y].push_back(x);
}
if (n <= 200) {
for (int i = 1; i <= n; i++) Calc(i);
} else Solve(1);
cout << ans;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 211ms
memory: 66300kb
input:
499991 31473 1 2 2 3 1 4 2 5 4 6 4 7 6 8 4 9 8 10 6 11 9 12 10 13 10 14 1 15 14 16 3 17 14 18 12 19 13 20 11 21 16 22 16 23 20 24 5 25 16 26 18 27 8 28 15 29 27 30 27 31 26 32 21 33 3 34 27 35 33 36 25 37 22 38 11 39 11 40 17 41 25 42 3 43 3 44 2 45 35 46 25 47 5 48 6 49 41 50 42 51 1 52 39 53 14 54...
output:
15734930984
result:
ok single line: '15734930984'
Test #2:
score: 0
Accepted
time: 369ms
memory: 85776kb
input:
499993 461706 1 2 2 3 3 4 1 5 3 6 3 7 7 8 5 9 5 10 7 11 10 12 9 13 13 14 12 15 13 16 15 17 14 18 17 19 16 20 15 21 16 22 17 23 22 24 20 25 23 26 25 27 27 28 28 29 25 30 29 31 30 32 31 33 33 34 33 35 32 36 36 37 35 38 33 39 34 40 35 41 41 42 40 43 42 44 39 45 40 46 41 47 43 48 44 49 46 50 49 51 50 52...
output:
195357165512
result:
ok single line: '195357165512'
Test #3:
score: 0
Accepted
time: 356ms
memory: 86800kb
input:
499993 170472 1 2 1 3 1 4 4 5 5 6 4 7 6 8 5 9 8 10 6 11 6 12 10 13 13 14 11 15 14 16 15 17 14 18 16 19 16 20 17 21 17 22 20 23 19 24 21 25 23 26 22 27 27 28 24 29 24 30 28 31 31 32 30 33 29 34 29 35 35 36 33 37 34 38 36 39 36 40 36 41 41 42 40 43 41 44 39 45 45 46 42 47 47 48 44 49 45 50 46 51 50 52...
output:
65062419628
result:
ok single line: '65062419628'
Test #4:
score: -100
Wrong Answer
time: 402ms
memory: 100400kb
input:
499994 801 1 2 2 3 1 4 2 5 2 6 1 7 7 8 4 9 6 10 10 11 7 12 7 13 10 14 13 15 15 16 14 17 16 18 18 19 15 20 17 21 18 22 19 23 21 24 23 25 21 26 26 27 27 28 24 29 28 30 29 31 27 32 30 33 28 34 29 35 35 36 31 37 36 38 36 39 38 40 36 41 36 42 38 43 38 44 42 45 41 46 41 47 47 48 46 49 45 50 49 51 49 52 48...
output:
285905391
result:
wrong answer 1st lines differ - expected: '285905307', found: '285905391'