QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#392557 | #7936. Eliminate Tree | ucup-team3215# | WA | 264ms | 40116kb | C++20 | 1.2kb | 2024-04-17 17:16:43 | 2024-04-17 17:16:44 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using item = array<int, 2>;
vector< set<int> > g;
set<item> st;
void add(int u) {
st.insert({g[u].size(), u});
}
void rmleaf(int u) {
int v = *g[u].begin();
st.erase({g[u].size(), u});
st.erase({g[v].size(), v});
g[u].erase(v);
g[v].erase(u);
add(v);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
g.resize(n);
for (int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
--u;--v;
g[u].insert(v);
g[v].insert(u);
}
for (int i = 0; i < n; ++i) {
add(i);
}
int ans = 0;
while (n) {
auto [dx, x] = *st.begin();
if (g[x].size() ) {
int y = *g[x].begin();
int dy = g[y].size();
if (dy <= 2) {
rmleaf(x);
rmleaf(y);
ans += 1;
n -= 2;
} else {
rmleaf(x);
ans += 2;
n -= 1;
}
} else {
rmleaf(x);
ans += 2;
n -= 1;
}
}
cout << ans << endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3596kb
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: 3732kb
input:
4 1 2 2 3 3 4
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: -100
Wrong Answer
time: 264ms
memory: 40116kb
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:
169523
result:
wrong answer 1st numbers differ - expected: '138182', found: '169523'