QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#439516 | #7936. Eliminate Tree | monkey8 | WA | 73ms | 32484kb | C++14 | 2.0kb | 2024-06-12 07:22:41 | 2024-06-12 07:22:41 |
Judging History
answer
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <cstdio>
#include <utility>
#include <queue>
#include <math.h>
#include <set>
#include <bitset>
#include <cmath>
#include <bitset>
#include <stack>
#include <cstring>
#include <math.h>
#include <iomanip>
#include <unordered_map>
#include <deque>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MAXN = 200010;
set<int> graph[MAXN];
int rem[MAXN];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
int n; cin >> n;
for(int i = 0; i < n - 1; i++) {
int x, y; cin >> x >> y;
graph[x].insert(y);
graph[y].insert(x);
}
int ans = 0;
deque<int> q;
for(int i = 1; i <= n; i++) {
if(graph[i].size() <= 1) {
if(graph[i].size() == 1) {
int y = *(graph[i].begin());
if(graph[y].size() <= 2) q.push_front(i);
else q.push_back(i);
}
else q.push_back(i);
}
}
while(!q.empty()) {
int x = q.front();
q.pop_front();
if(rem[x]) continue;
rem[x] = 1;
if(graph[x].size() == 0) ans += 2;
else {
int y = *(graph[x].begin());
if(graph[y].size() <= 2) {
rem[y] = 1;
for(int z : graph[y]) {
if(rem[z]) continue;
graph[z].erase(y);
if(graph[z].size() <= 1) {
if(graph[z].size() == 1) {
int k = *(graph[z].begin());
if(graph[k].size() <= 2) q.push_front(z);
else q.push_back(z);
}
else q.push_back(z);
}
}
ans++;
}
else {
graph[y].erase(x);
ans += 2;
}
}
}
cout << ans << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 12932kb
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: 12932kb
input:
4 1 2 2 3 3 4
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: -100
Wrong Answer
time: 73ms
memory: 32484kb
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:
159524
result:
wrong answer 1st numbers differ - expected: '138182', found: '159524'