QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#325285 | #7936. Eliminate Tree | automac# | RE | 1ms | 3688kb | C++20 | 1.2kb | 2024-02-11 07:11:19 | 2024-02-11 07:11:19 |
Judging History
answer
#include<bits/stdc++.h>
#define forn(i,n) for(int i = 0; i< (int)n;++i)
#define for1(i,n) for(int i = 1; i<= (int)n;++i)
#define fore(i,l, r) for(int i = l; i<= r;++i)
#define all(a) a.begin(), a.end()
#define el '\n'
#define d(x) cerr<<#x<<" "<<x<<el
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef long long ll;
// const int nax = 2e5+100;
const int nax = 2e3+100;
vi g[nax];
int dp[nax][2];// 1=> borrar todo subarbol
const int inf = 2e9;
void dfs(int u, int p = -1){
dp[u][1] = inf;
dp[u][0] = 0;
for(int v: g[u]){
if(v == p) continue;
dfs(v,u);
dp[u][0] = dp[u][0] + dp[v][1];
}
for(int v : g[u]){
if(v == p) continue;
dp[u][1] = min(dp[u][1], dp[u][0] - dp[v][1] + dp[v][0] + 1);
}
dp[u][1] = min(dp[u][1], dp[u][0] + 2);
}
void solve(){
int n;
cin>>n;
forn(i,n-1){
int u,v;
cin>>u>>v;
--u,--v;
g[u].pb(v), g[v].pb(u);
}
dfs(0);
cout<<dp[0][1]<<el;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
int tt = 1;
// cin>>tt;
while(tt--){
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3616kb
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: 3688kb
input:
4 1 2 2 3 3 4
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: -100
Runtime Error
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...