QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#223706 | #4216. Funny Salesman | warwolf# | WA | 2ms | 9956kb | C++14 | 2.6kb | 2023-10-22 15:38:25 | 2023-10-22 15:38:26 |
Judging History
answer
#include<vector>
#include<iostream>
#include<set>
#include<map>
#include<deque>
#include<stack>
#include<cstring>
#include<queue>
#include<cmath>
#define maxn 100005
typedef long long ll;
const double eps = 1e-9;
const double pi = acos(-1);
const int mod = 998244353;
using namespace std;
ll read(){
ll x = 0, f = 1;char ch = getchar();
while(ch > '9' || ch < '0'){if(ch == '-') f = -1;ch = getchar();}
while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
return x * f;
}
int qpow(int a, int b){
int ans = 1, base = a;
while(b){
if(b & 1) ans = 1ll * ans * base % mod;
base = 1ll * base * base % mod;
b >>= 1;
}
return ans;
}
void init(){
}
int n, m;
int d[maxn];
vector<int> e[maxn];
vector<pair<int, int>> E[maxn];
int in[maxn], ou[maxn], dfc, bit[maxn];
bool vis[maxn];
void dfs(int u, int f){
in[u] = ++dfc;
d[u] = d[f] + 1;
for(auto x: e[u]){
if(x == f) continue;
dfs(x, u);
}
ou[u] = dfc;
}
void add(int u, int x){for(int i = u; i <= n; i += i & -i) bit[i] += x;}
int calc(int u){int sum = 0;for(int i = u; i; i -= i & -i) sum += bit[i]; return sum;}
void clr(int u, int f){
add(in[u], -1);
n--;
vis[u] = 1;
for(auto x: e[u]){
if(x == f || vis[x]) continue;
clr(x, u);
}
}
void solve(){
n = read();
for(int i = 1; i < n; i++){
int u = read(), v = read(), w = read();
e[u].push_back(v), e[v].push_back(u);
E[w].push_back({u, v});
}
dfs(1, 0);
for(int i = 1; i <= n; i++) add(in[i], 1);
ll ans = 0;
int us = 0;
for(int i = 30; i >= 0; i--){
for(auto x: E[i]){
if(vis[x.first] || vis[x.second]) continue;
int u = x.first, v = x.second;
if(d[u] > d[v]) swap(u, v);
int a = n - (calc(ou[v]) - calc(in[v] - 1)), b = calc(ou[v]) - calc(in[v] - 1);
if(a > b) swap(a, b), swap(u, v);
if(b - a - 1 <= us){
int tt = min(us, b - a - 1);
b -= tt;
us -= tt;
if(us) b--, us--;
a -= us / 2, b -= us - us / 2;
ans += (1ll << i) * (a + b - 1);
printf("%lld\n", ans);
return ;
}
ans += (1ll << i) * (2 * a);
clr(u, v);
us += a;
}
}
}
int main() {
int t;
init();
t = 1;
while(t--){
solve();
// puts(solve() ? "Yes" : "No");
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 8676kb
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: 0ms
memory: 9956kb
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: 0ms
memory: 8576kb
input:
2 2 1 20
output:
1048576
result:
ok 1 number(s): "1048576"
Test #4:
score: 0
Accepted
time: 1ms
memory: 8616kb
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: -100
Wrong Answer
time: 1ms
memory: 8576kb
input:
8 3 2 2 3 4 27 6 3 2 1 6 9 5 7 24 8 5 23 8 1 3
output:
327155712
result:
wrong answer 1st numbers differ - expected: '318767616', found: '327155712'