QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#809265 | #7216. Boxes on tree | KellyWLJ | WA | 1ms | 4280kb | C++17 | 5.3kb | 2024-12-11 13:39:09 | 2024-12-11 13:39:10 |
Judging History
answer
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
using pii = pair<int, int>;
const int N = 5010, SZ = (1 << 18) + 5;
static char buf[SZ], *bgn = buf, *til = buf;
char getc() {
if(bgn == til) bgn = buf, til = buf + fread(buf, 1, SZ, stdin);
return bgn == til ? EOF : *bgn++;
}
template<typename T>
void read(T &x) {
char ch = getc(); T fh = 0; x = 0;
while(ch < '0' || ch > '9') fh |= (ch == '-'), ch = getc();
while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getc();
x = fh ? -x : x;
}
template<typename Type, typename... argc>
void read(Type &x, argc &...args) {read(x), read(args...);}
template<typename T>
void print(T x) {
if(x < 0) putchar('-'), x = -x;
if(x / 10) print(x / 10);
putchar(x % 10 + '0');
}
int n, u, v, a[N], cnt, fr[N], vis[N], fa[N], bk[N], tmp[N], ans, sum, book[N], nxt[N], val[N], dis[N], que[N], hh, tt, idx;
vector<int> edge[N], cir[N], has[N], in[N];
void clear() {
for(int i = 1; i <= cnt; ++i) cir[i].clear(), has[i].clear(), in[i].clear();
for(int i = 1; i <= n; ++i) fr[i] = vis[i] = book[i] = 0, edge[i].clear();
cnt = ans = sum = idx = 0;
}
struct DSU {
int sum[N], pa[N];
void init() {for(int i = 1; i <= n; ++i) sum[i] = 0, pa[i] = i;}
pii find(int x) {
if(pa[x] == x) return {x, 0};
pii res = find(pa[x]); pa[x] = res.fi, sum[x] += res.se; return {res.fi, sum[x]};
}
void merge(int x, int y, int v) {
if(!x || !y) return;
x = find(x).fi, y = find(y).fi;
if(x != y) pa[x] = y, sum[x] += v;
}
}dsu, pnt;
void dfs(int x, int fath) {
fa[x] = fath;
for(int y : edge[x]) if(y != fath) dfs(y, x);
}
void bfs(int str) {
hh = 1, tt = 0;
memset(dis, 0x3f, sizeof(dis));
for(int st : in[str])
for(int x : has[st]) if(dis[x]) que[++tt] = x, dis[x] = 0;
while(hh <= tt) {
int x = que[hh++];
auto [t, v] = dsu.find(fr[x]); v = pnt.find(fr[x]).se;
if(book[fr[x]] && t != str) {
if(val[t] > dis[x] - v) val[t] = dis[x] - v, nxt[t] = str;
}
for(int y : edge[x])
if(dis[y] > dis[x] + 1) dis[y] = dis[x] + 1, que[++tt] = y;
}
}
void mst() {
dsu.init(), pnt.init(), idx = cnt;
int num = cnt;
for(int i = 1; i <= cnt; ++i) book[i] = 1, in[i].pb(i);
book[1] = 0;
while(num > 1) {
for(int i = 1; i <= idx; ++i) val[i] = N, nxt[i] = 0;
for(int i = 1; i <= idx; ++i) if(dsu.find(i).fi == i) bfs(i);
memset(vis, 0, sizeof(vis)), memset(bk, 0, sizeof(bk));
// for(int i = 1; i <= idx; ++i)
// if(dsu.find(i).fi == i) cerr << i << " : " << nxt[i] << " " << val[i] << " " << "\n";
int ct = 0, pre = idx;
num = 0;
for(int i = 1; i <= idx; ++i) tmp[i] = dsu.find(i).fi;
for(int i = 1; i <= pre; ++i) if(dsu.find(i).fi == i && !vis[i]) {
int x = i, las = x; ++ct;
while(x && !vis[x]) dsu.merge(x, nxt[x], 0), sum += nxt[x] ? val[x] : 0, vis[x] = ct, las = x, x = nxt[x];
if(x && vis[x] == ct) {
int nw = nxt[x]; ++idx, tmp[idx] = idx, ++num;
while(nw != x) dsu.merge(nw, idx, 0), bk[nw] = 1, pnt.merge(nw, idx, val[nw]), nw = nxt[nw];
dsu.merge(x, idx, 0), bk[x] = 1, pnt.merge(x, idx, val[x]);
}
else if(!x) bk[las] = 1, ++num;
}
for(int i = 1; i <= idx; ++i) in[i].clear();
for(int i = 1; i <= cnt; ++i) in[dsu.find(i).fi].pb(i);
// for(int i = 1; i <= idx; ++i) if(dsu.find(i).fi == i) ++num;
// for(int i = 1; i <= idx; ++i) cerr << bk[i] << " ";
// cerr << "\n";
for(int i = 2; i <= cnt; ++i) if(book[i] && !bk[tmp[i]]) book[i] = 0;
// for(int i = 1; i <= idx; ++i) cerr << dsu.find(i).fi << " ";
// cerr << "\n" << num << "\n";
// for(int i = 1; i <= cnt; ++i) cerr << book[i] << " ";
// cerr << "\n";
}
}
void mian() {
read(n), clear();
for(int i = 1; i <= n; ++i) read(a[i]);
for(int i = 1; i < n; ++i) read(u, v), edge[u].pb(v), edge[v].pb(u);
for(int i = 1; i <= n; ++i) if((i == 1 || i != a[i]) && !vis[i]) {
++cnt; int x = i;
while(a[x] != i) cir[cnt].pb(x), x = a[x];
cir[cnt].pb(x);
for(int j : cir[cnt]) vis[j] = 1, fr[j] = cnt;
}
for(int i = 1; i <= cnt; ++i) {
cir[i].pb(cir[i][0]);
memset(vis, 0, sizeof(vis));
for(int j = 0; j + 1 < cir[i].size(); ++j) {
dfs(cir[i][j + 1], 0);
for(int x = cir[i][j]; x; x = fa[x]) {
++ans;
if(!vis[x]) vis[x] = 1, has[i].pb(x);
}
--ans;
}
}
// cerr << cnt << "\n";
mst();
// cerr << ans << " " << sum << "\n";
print(ans + sum * 2), putchar('\n');
}
int main() {
#ifdef Kelly
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
freopen("err.txt", "w", stderr);
#else
// freopen("classic.in", "r", stdin);
// freopen("classic.out", "w", stdout);
#endif
int T = 1; // read(T);
while(T--) mian();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 4204kb
input:
3 1 3 2 1 2 2 3
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: 0
Accepted
time: 1ms
memory: 4212kb
input:
4 2 1 4 3 1 3 3 2 3 4
output:
6
result:
ok 1 number(s): "6"
Test #3:
score: 0
Accepted
time: 1ms
memory: 4208kb
input:
1 1
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: 0
Accepted
time: 0ms
memory: 4280kb
input:
10 1 6 3 4 5 2 7 8 9 10 10 8 8 6 2 1 6 5 7 6 3 4 5 1 8 9 4 8
output:
8
result:
ok 1 number(s): "8"
Test #5:
score: 0
Accepted
time: 0ms
memory: 4200kb
input:
10 1 3 9 4 5 6 7 8 2 10 7 4 2 1 8 10 1 8 6 5 6 4 9 8 8 6 2 3
output:
10
result:
ok 1 number(s): "10"
Test #6:
score: 0
Accepted
time: 0ms
memory: 4192kb
input:
10 8 2 3 9 5 6 7 1 4 10 5 1 5 8 6 5 3 6 10 8 9 3 7 1 6 2 4 10
output:
20
result:
ok 1 number(s): "20"
Test #7:
score: 0
Accepted
time: 0ms
memory: 4148kb
input:
10 1 2 3 5 4 6 7 8 9 10 1 6 1 4 9 2 5 4 10 7 1 3 10 1 9 3 10 8
output:
4
result:
ok 1 number(s): "4"
Test #8:
score: 0
Accepted
time: 1ms
memory: 4148kb
input:
10 1 2 3 6 5 4 8 7 9 10 2 10 9 6 4 2 1 8 2 9 9 5 6 3 7 6 8 10
output:
18
result:
ok 1 number(s): "18"
Test #9:
score: 0
Accepted
time: 0ms
memory: 4204kb
input:
10 6 9 4 1 2 10 3 7 5 8 7 9 10 9 3 1 10 2 8 6 4 10 8 7 4 3 4 5
output:
30
result:
ok 1 number(s): "30"
Test #10:
score: 0
Accepted
time: 1ms
memory: 4144kb
input:
10 10 8 9 3 2 4 5 7 1 6 6 10 4 6 10 5 7 1 9 6 3 6 4 7 8 6 1 2
output:
32
result:
ok 1 number(s): "32"
Test #11:
score: 0
Accepted
time: 1ms
memory: 4232kb
input:
10 8 3 9 6 5 1 2 10 4 7 8 6 7 9 1 9 10 2 10 8 10 3 9 5 7 2 4 8
output:
28
result:
ok 1 number(s): "28"
Test #12:
score: 0
Accepted
time: 1ms
memory: 4224kb
input:
10 9 2 6 4 5 7 10 3 8 1 2 3 9 4 4 5 9 2 8 6 8 3 3 1 9 7 9 10
output:
20
result:
ok 1 number(s): "20"
Test #13:
score: 0
Accepted
time: 1ms
memory: 4224kb
input:
10 3 1 9 4 2 8 5 10 6 7 8 10 4 8 4 5 5 3 4 1 10 9 6 9 6 2 7 3
output:
32
result:
ok 1 number(s): "32"
Test #14:
score: 0
Accepted
time: 1ms
memory: 4248kb
input:
20 1 2 13 4 5 3 7 8 9 10 11 14 6 12 15 17 16 18 19 20 14 19 10 12 8 18 8 1 18 9 4 8 2 4 11 4 17 7 16 12 4 17 6 3 13 5 6 11 8 20 5 3 19 13 6 12 15 1
output:
34
result:
ok 1 number(s): "34"
Test #15:
score: 0
Accepted
time: 1ms
memory: 4140kb
input:
20 1 19 3 4 5 2 7 13 9 10 11 14 8 18 15 16 17 12 6 20 14 7 11 17 16 5 20 14 8 14 15 13 18 2 6 18 11 4 9 7 7 4 5 18 12 9 15 18 10 12 11 2 16 1 18 19 1 3
output:
44
result:
ok 1 number(s): "44"
Test #16:
score: 0
Accepted
time: 0ms
memory: 4216kb
input:
20 1 2 3 4 19 6 7 8 9 10 11 12 15 14 13 16 17 18 5 20 11 16 9 18 2 11 19 1 7 12 20 16 13 20 10 17 7 2 16 18 18 15 16 14 2 6 1 7 10 6 8 5 5 13 4 16 3 14
output:
26
result:
ok 1 number(s): "26"
Test #17:
score: 0
Accepted
time: 1ms
memory: 4208kb
input:
20 1 2 12 4 13 7 6 8 17 10 11 14 16 5 15 19 9 18 3 20 4 6 19 14 6 9 8 9 2 19 14 9 6 16 5 9 14 13 10 7 15 20 2 20 12 1 19 3 7 19 17 2 4 11 14 12 18 12
output:
36
result:
ok 1 number(s): "36"
Test #18:
score: 0
Accepted
time: 1ms
memory: 4192kb
input:
20 12 2 3 4 5 18 7 8 17 10 19 1 13 14 15 11 9 6 16 20 5 11 9 15 17 2 2 8 2 20 11 7 19 9 10 11 10 13 14 10 19 11 20 3 20 6 1 10 9 6 18 12 11 16 11 12 1 4
output:
30
result:
ok 1 number(s): "30"
Test #19:
score: 0
Accepted
time: 0ms
memory: 4196kb
input:
20 15 20 17 12 2 6 14 13 4 3 1 7 9 19 10 16 5 8 18 11 3 20 15 6 17 1 6 5 17 11 13 14 12 4 4 14 2 16 12 16 15 1 13 19 12 6 9 1 1 18 1 8 20 8 7 5 5 10
output:
76
result:
ok 1 number(s): "76"
Test #20:
score: 0
Accepted
time: 1ms
memory: 4212kb
input:
20 2 10 12 13 7 1 19 18 17 15 14 5 6 4 16 9 8 11 3 20 15 10 1 4 7 20 9 16 20 15 3 19 17 20 11 7 17 14 4 6 6 11 6 3 15 2 10 12 18 10 8 11 14 13 9 11 5 14
output:
82
result:
ok 1 number(s): "82"
Test #21:
score: 0
Accepted
time: 1ms
memory: 4260kb
input:
20 10 4 1 15 3 9 11 14 13 2 17 6 7 5 20 16 19 8 12 18 8 17 9 15 7 16 9 17 13 14 4 14 12 10 19 17 15 1 15 16 17 5 20 12 10 19 20 3 3 6 19 18 14 10 2 1 16 11
output:
92
result:
ok 1 number(s): "92"
Test #22:
score: 0
Accepted
time: 1ms
memory: 4204kb
input:
20 3 7 6 11 15 16 12 13 19 5 14 2 20 9 10 8 1 17 18 4 9 12 7 2 1 12 16 1 14 15 11 5 2 10 8 5 9 11 3 7 7 5 20 1 11 18 14 16 2 19 13 15 10 17 8 6 10 4
output:
114
result:
ok 1 number(s): "114"
Test #23:
score: 0
Accepted
time: 1ms
memory: 4156kb
input:
20 11 12 6 5 4 18 10 16 2 19 14 1 9 7 3 17 13 8 20 15 13 11 7 3 19 11 12 4 17 15 19 1 16 20 8 16 4 13 8 12 8 6 17 1 13 14 18 11 10 17 17 2 1 5 4 3 9 8
output:
106
result:
ok 1 number(s): "106"
Test #24:
score: -100
Wrong Answer
time: 1ms
memory: 4196kb
input:
50 1 2 3 4 28 6 7 8 9 10 11 12 13 27 38 16 19 15 50 21 33 22 37 24 25 26 14 5 30 34 31 48 20 29 35 36 23 18 39 40 41 42 43 44 45 46 47 32 49 17 39 13 11 26 8 1 45 35 35 3 49 23 31 9 34 21 49 43 3 48 20 36 45 8 44 21 42 11 44 4 33 29 47 46 22 9 46 43 48 5 16 1 20 4 1 10 11 29 41 4 24 15 16 17 15 42 2...
output:
184
result:
wrong answer 1st numbers differ - expected: '174', found: '184'