QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#753897 | #7936. Eliminate Tree | arashMLG# | WA | 59ms | 17828kb | C++17 | 1.5kb | 2024-11-16 13:49:35 | 2024-11-16 13:49:36 |
Judging History
answer
#include<bits/stdc++.h>
#ifdef LOCAL
#include "Essentials/algo/debug.h"
#else
#define debug(...) 69
#define debugArr(...) 69
#endif
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define F first
#define S second
#define pb push_back
#define kill(x) cout<<x<<endl, exit(0);
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define lc (v << 1)
#define rc ((v<<1) |1)
const int N = 2e5+23;
const ll INF=1e18+23;
vector<int> g[N];
ll dp[N][2];
int n;
void dfs(int v, int p=0){
vector<int> ch;
for (int u: g[v]){
if(u==p) continue;
ch.pb(u);
dfs(u, v);
}
dp[v][1]=0;
for (int u: ch) dp[v][1]+=dp[u][0];
dp[v][0]=dp[v][1]+2;
ll sum=0;
ll Mn=INF;
for (int u: ch){
sum+=dp[u][0];
Mn=min(Mn, dp[0][1]-dp[u][0]);
}
dp[v][0]=min(dp[v][0], sum+Mn+1);
}
int32_t main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin>>n;
if(n==1)return cout<<2<<endl, 0;
for (int i=0; i<n-1; i++){
int v, u; cin>>v>>u;
g[v].pb(u); g[u].pb(v);
}
int r=-1;
for (int i=1; i<=n; i++) if(g[i].size()>2) r=i;
if(r==-1)return cout<<(n+1)/2<<endl, 0;
dfs(r);
cout<<dp[r][0]<<endl;
return 0;
}
// Jumpsuit, Jumpsuit cover me!
// Jumpsuit, Jumpsuit cover me!
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 9152kb
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: 9104kb
input:
4 1 2 2 3 3 4
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: -100
Wrong Answer
time: 59ms
memory: 17828kb
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:
3
result:
wrong answer 1st numbers differ - expected: '138182', found: '3'