QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#810131 | #7216. Boxes on tree | Liuxizai | WA | 1ms | 4888kb | C++20 | 4.8kb | 2024-12-11 19:55:59 | 2024-12-11 19:56:14 |
Judging History
answer
#include <bits/stdc++.h>
#define File(name) freopen(#name".in", "r", stdin); freopen(#name".out", "w", stdout);
using namespace std;
using ll = long long;
using ull = unsigned long long;
template<typename T>
inline T read(){
T n = 0; int f = 1; char ch = getchar();
while(!isdigit(ch)){
if(ch == '-') f = -1;
ch = getchar();
}
while(isdigit(ch)){
n = n * 10 + ch - '0';
ch = getchar();
}
return f * n;
}
template<typename T>
void write(T n){
if(n < 0) return putchar('-'), write(-n);
if(n / 10) write(n / 10);
putchar(n % 10 + '0');
}
void input() {}
template<typename Type, typename... Types>
void input(Type &arg, Types &...args){
arg = read<Type>();
input(args...);
}
namespace Main{
const int N = 505;
int t, n, p[N], fa[N], dep[N], dfn[N], siz[N], dfnCnt, st[13][N], ans;
int bel[N], len[N];
int e[N][N];
bool vis[N];
pair<int, int> mn[N];
vector<int> g[N];
vector<int> vec;
inline int dmin(int u, int v) { return dep[u] < dep[v] ? u : v; }
inline bool in_subt(int u, int r) { return dfn[u] >= dfn[r] && dfn[u] <= dfn[r] + siz[r] - 1; }
void dfs(int u, int f){
dep[u] = dep[f] + 1;
siz[u] = 1;
st[0][dfn[u] = ++dfnCnt] = f;
for(int v: g[u]) if(v != f) dfs(v, u), siz[u] += siz[v];
}
int lca(int u, int v){
if(u == v) return u;
if(dfn[u] > dfn[v]) swap(u, v);
int lg = log2(dfn[v] - dfn[u]);
return dmin(st[lg][dfn[u] + 1], st[lg][dfn[v] - (1 << lg) + 1]);
}
int dis(int u, int v) { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; }
int dis(int u, int s, int t){
if(in_subt(s, u) ^ in_subt(t, u)) return 0;
return min({dis(u, s), dis(u, t), dis(u, lca(s, t))});
}
void merge(int v, int u){
bel[v] = u;
vec.erase(find(vec.begin(), vec.end(), v));
for(int i: vec) e[u][i] = min(e[u][i], e[v][i]);
for(int i: vec) e[i][u] = min(e[i][u], e[i][v]);
}
void Main(){
input(n);
for(int i = 1; i <= n; i++) input(p[i]);
for(int i = 1, u, v; i < n; i++){
input(u, v);
g[u].push_back(v), g[v].push_back(u);
}
dfs(1, 0);
for(int i = 1; i < 13; i++){
for(int j = 1; j + (1 << i) - 1 <= n; j++){
st[i][j] = dmin(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);
}
}
memset(e, 0x3f, sizeof(e));
for(int i = 1; i <= n; i++){
if(bel[i]) continue;
bel[i] = i, len[i] = 1;
for(int j = p[i]; j != i; j = p[j]) bel[j] = i, len[i]++;
}
vec = {1};
for(int i = 2; i <= n; i++) if(bel[i] == i && len[i] > 1) vec.push_back(i);
for(int i = 1; i <= n; i++){
if(bel[i] != 1 && len[bel[i]] > 1){
for(int j = 1; j <= n; j++){
e[bel[i]][bel[j]] = min(e[bel[i]][bel[j]], 2 * dis(i, j, p[j]));
}
}
}
vis[1] = true;
for(int i: vec) if(i != 1){
mn[i] = {1e9, -1};
for(int j: vec) if(i != j) mn[i] = min(mn[i], make_pair(e[i][j], j));
if(mn[i].second == -1) { puts("-1"); return; }
}
iota(bel + 1, bel + n + 1, 1);
for(int i = 1; i <= n; i++) ans += dis(i, p[i]);
while(true){
for(int i: vec) if(i != 1) vis[i] = false, mn[i].second = bel[mn[i].second];
bool ok = true;
for(int i: vec){
if(i == 1) continue;
vector<int> path;
int p = i;
while(!vis[p]){
vis[p] = true;
path.push_back(p);
p = mn[p].second;
}
auto it = find(path.begin(), path.end(), p);
if(it != path.end()){
ok = false;
int s = *it;
while(it != path.end()){
for(int j: vec) g[j][*it] -= mn[*it].first;
ans += mn[*it].first;
if(*it != s) merge(*it, s);
it++;
}
mn[s] = {1e9, -1};
for(int j: vec) if(j != s) mn[s] = min(mn[s], make_pair(e[s][j], j));
if(mn[s].second == -1) { puts("-1"); return; }
}
}
if(ok){
for(int i: vec) if(i != 1) ans += mn[i].first;
break;
}
}
write(ans), puts("");
return;
}
} // namespace Main
int main(){
#ifdef Liuxizai
freopen("in", "r", stdin);
freopen("out", "w", stdout);
#endif // Liuxizai
Main::Main();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 4688kb
input:
3 1 3 2 1 2 2 3
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: 0
Accepted
time: 0ms
memory: 4680kb
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: 4504kb
input:
1 1
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: 0
Accepted
time: 1ms
memory: 4684kb
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: 1ms
memory: 4644kb
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: 1ms
memory: 4820kb
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: 1ms
memory: 4804kb
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: 0ms
memory: 4820kb
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: 1ms
memory: 4884kb
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: 4888kb
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: 0ms
memory: 4688kb
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: 0ms
memory: 4816kb
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: 0ms
memory: 4676kb
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: -100
Wrong Answer
time: 1ms
memory: 4688kb
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:
38
result:
wrong answer 1st numbers differ - expected: '34', found: '38'