QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#286154 | #7936. Eliminate Tree | ucup-team2894# | WA | 89ms | 34692kb | C++20 | 2.3kb | 2023-12-17 04:22:45 | 2023-12-17 04:22:46 |
Judging History
answer
// UW BLACK
#include <bits/stdc++.h>
#define fr first
#define sc second
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)((x).size()))
#define rep(i,a,b) for(int i=(a);i<(b);++i)
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using pii = pair<int,int>;
mt19937 rnd(223);
clock_t TIME0;
#define TIME ((clock() - TIME0) * 1.0 / CLOCKS_PER_SEC)
constexpr int maxn = 1e6 + 100, inf = 1e9 + 100;
using T = long double;
constexpr T EPS = 1e-9;
const ll mod = 1e9+7;
bool rem[maxn];
int deg[maxn];
vi g[maxn];
void solve() {
int n;
cin >> n;
for(int i = 0; i < n - 1; ++i) {
int a,b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
priority_queue<pii> lfs;
for(int i=1;i<=n;i++){
deg[i] = g[i].size();
}
for(int i=1;i<=n;i++){
if(deg[i]<=1){
if(deg[i]==0){
lfs.push({-1,i});
continue;
}
int j = g[i][0];
if(deg[j]<=2){
lfs.push({j,i});
}
else lfs.push({-1,i});
}
}
int ans = 0;
while(lfs.size()){
auto [z,x] = lfs.top();
lfs.pop();
if(rem[x])continue;
if(z!=-1&&rem[z]){
lfs.push({-1,x});
continue;
}
if(z==-1)ans += 2;
else ans +=1;
rem[x]=true;
if(z!=-1)rem[z]=true;
if(z!=-1)x=z;
for(int y:g[x])if(!rem[y]){
deg[y]--;
if(deg[y]<=1){
int oy=-1;
for(int yy : g[y])if(!rem[yy]){
if(deg[yy]<=2)oy=yy;
}
lfs.push({oy,y});
}
if(deg[y]<=2){
for(int yy : g[y])if(!rem[yy]){
if(deg[yy]<=1){
lfs.push({y,yy});
}
}
}
}
}
cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout << fixed;
cout.precision(20);
TIME0 = clock();
int k=1;
// cin >> k;
while(k--)
solve();
cerr << "\n\nConsumed " << TIME << endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 27204kb
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: 27156kb
input:
4 1 2 2 3 3 4
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: -100
Wrong Answer
time: 89ms
memory: 34692kb
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:
152864
result:
wrong answer 1st numbers differ - expected: '138182', found: '152864'