QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#346645 | #7936. Eliminate Tree | Delay_for_five_minutes# | WA | 141ms | 32108kb | C++11 | 1.7kb | 2024-03-08 20:14:19 | 2024-03-08 20:14:19 |
Judging History
answer
#include<bits/stdc++.h>
#define maxn 200005
std::set<int> G[maxn];
std::queue<int> q1,q2;
bool vis[maxn];
bool deg2(int x) {
if (vis[x]) return 0;
if (G[x].size() > 2) return 0;
for(auto i : G[x]) {
if (G[i].size() == 1) {
return 1;
}
}
return 0;
}
void rmv(int u) {
vis[u] = 1;
for(auto i : G[u]) {
G[i].erase(u);
if (G[i].size() == 1) {
int v = *G[i].begin();
q1.push(i);
if (deg2(v)) {
q2.push(v);
}
}
else if (deg2(i)) {
q2.push(i);
}
}
}
int main() {
int n;
std::cin >> n;
for(int i = 1; i < n; i++) {
int x,y;
std::cin >> x >> y;
G[x].insert(y);
G[y].insert(x);
}
for(int i = 1; i <= n; i++) {
if (G[i].size() == 1) {
q1.push(i);
}
else if (deg2(i)){
q2.push(i);
}
}
int ans = 0;
for(int cnt = 0;cnt < n;) {
while(!q2.empty()) {
int u = q2.front();q2.pop();
if (!deg2(u)) continue;
int v = -1;
for(auto i : G[u]) {
if (G[i].size() == 1) {
v = i;
break;
}
}
rmv(v),rmv(u);
cnt += 2;
ans ++;
}
if (!q1.empty()) {
int u = q1.front();
q1.pop();
if (vis[u]) continue;
rmv(u);
ans += 2;
cnt++;
}
}
std::cout << ans << std::endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 13004kb
input:
5 1 2 2 3 3 4 3 5
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: 0
Accepted
time: 3ms
memory: 12948kb
input:
4 1 2 2 3 3 4
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: -100
Wrong Answer
time: 141ms
memory: 32108kb
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:
152825
result:
wrong answer 1st numbers differ - expected: '138182', found: '152825'