QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#284768 | #7936. Eliminate Tree | ucup-team045# | WA | 53ms | 15732kb | C++20 | 1.8kb | 2023-12-16 14:58:03 | 2023-12-16 14:58:03 |
Judging History
answer
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
using LL = long long;
int main(){
#ifdef LOCAL
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int n;
cin >> n;
vector<vector<int> > g(n + 1);
for(int i = 0; i < n - 1; i++){
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
vector<int> f(n + 1), s(n + 1);
int ans = 1e9;
auto dfs1 = [&](auto &&dfs1, int u, int fa) -> void {
for(auto j : g[u]){
if (j == fa) continue;
dfs1(dfs1, j, u);
if (s[j] == 0){
f[u] += f[j];
s[u] += 1;
}
else{
f[u] += f[j] + 2 * s[j] - 1;
}
}
};
auto dfs2 = [&](auto &&dfs2, int u, int fa, pair<int, int> pre) -> void {
int sf = f[u];
int ss = s[u];
if (u == 1){
}
else if (pre.second == 0){
sf += pre.first;
ss += 1;
}
else{
sf += pre.first + 2 * (pre.second) - 1;
}
if (ss == 0){
ans = min(ans, sf + 2);
}
else{
ans = min(ans, sf + 2 * ss - 1);
}
for(auto j : g[u]){
if (j == fa) continue;
int nf = sf;
int ns = ss;
if (s[j] == 0){
nf -= f[j];
ss -= 1;
}
else{
nf -= f[j] + 2 * s[j] - 1;
}
dfs2(dfs2, j, u, {nf, ns});
}
};
dfs1(dfs1, 1, -1);
dfs2(dfs2, 1, -1, {0, 0});
cout << ans << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3544kb
input:
5 1 2 2 3 3 4 3 5
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3832kb
input:
4 1 2 2 3 3 4
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: -100
Wrong Answer
time: 53ms
memory: 15732kb
input:
196666 32025 108673 181779 17115 162570 134230 93003 39673 89144 1269 185919 154408 34896 65369 35860 44720 55698 1390 45520 189805 147867 124511 135459 132555 87303 18031 176314 59695 33352 130640 87272 39716 35749 108807 143493 94486 126512 116598 40212 70895 132216 80855 22241 188737 150354 17346...
output:
138181
result:
wrong answer 1st numbers differ - expected: '138182', found: '138181'