QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#48616 | #4216. Funny Salesman | ckiseki# | RE | 5ms | 6040kb | C++ | 1.3kb | 2022-09-14 19:07:36 | 2022-09-14 19:07:37 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using lld = int64_t;
static constexpr int maxn = 100000 + 5;
vector<pair<int, int>> g[maxn];
int vis[maxn], visc;
lld gao(const vector<int> &all, int c, int lef) {
assert (lef >= 0);
++visc;
vector<vector<int>> subs;
auto dfs = [&](auto self, int u, int f) -> void {
subs.back().push_back(u);
vis[u] = visc;
for (auto [v, w] : g[u]) {
if (v == f or w >= c) continue;
self(self, v, u);
}
};
for (int u : all) {
if (vis[u] == visc) continue;
subs.push_back({});
dfs(dfs, u, u);
}
pair<size_t, size_t> mxs = {0, 0};
for (size_t i = 0; i < subs.size(); ++i)
mxs = max(mxs, pair{subs[i].size(), i});
size_t now = all.size() - mxs.first;
if (now + 1 >= mxs.first) {
return lld(lef) << c;
}
now *= 2;
return gao(subs[mxs.second], c - 1, lef - now) + (now << c);
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n; cin >> n;
for (int i = 1; i < n; ++i) {
int u, v, w; cin >> u >> v >> w;
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
vector<int> all(n);
iota(all.begin(), all.end(), 1);
cout << gao(all, 30, n - 1) << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 5892kb
input:
5 1 2 0 2 3 0 3 4 0 4 5 1
output:
6
result:
ok 1 number(s): "6"
Test #2:
score: 0
Accepted
time: 4ms
memory: 5904kb
input:
10 2 1 1 3 1 1 1 4 0 5 1 2 6 4 1 2 7 2 8 4 2 8 9 3 6 10 0
output:
42
result:
ok 1 number(s): "42"
Test #3:
score: 0
Accepted
time: 5ms
memory: 6040kb
input:
2 2 1 20
output:
1048576
result:
ok 1 number(s): "1048576"
Test #4:
score: 0
Accepted
time: 5ms
memory: 6040kb
input:
5 4 2 25 5 4 0 5 3 16 5 1 28
output:
603979776
result:
ok 1 number(s): "603979776"
Test #5:
score: 0
Accepted
time: 5ms
memory: 5884kb
input:
8 3 2 2 3 4 27 6 3 2 1 6 9 5 7 24 8 5 23 8 1 3
output:
318767616
result:
ok 1 number(s): "318767616"
Test #6:
score: 0
Accepted
time: 1ms
memory: 5980kb
input:
4 1 2 6 4 1 26 4 3 10
output:
201326592
result:
ok 1 number(s): "201326592"
Test #7:
score: 0
Accepted
time: 2ms
memory: 5880kb
input:
3 2 1 2 2 3 3
output:
16
result:
ok 1 number(s): "16"
Test #8:
score: 0
Accepted
time: 2ms
memory: 5876kb
input:
3 1 2 10 1 3 26
output:
134217728
result:
ok 1 number(s): "134217728"
Test #9:
score: -100
Dangerous Syscalls
input:
10 1 4 15 6 2 23 1 7 20 1 9 29 1 3 1 6 1 4 10 6 12 8 5 13 8 10 20