272 | 2021-06-22 15:22:21 | mraron | Energiatakarékos ellenőrzés | cpp14 | Accepted 100/100 | 105ms | 48004 KiB |
#include<bits/stdc++.h>
using namespace std;
int n;
vector<int> adj[100001];
array<long long,2> dp[100001];
int st[100001];
int sz[100001];
void dfs(int x) {
st[x]=1;
vector<int> gy;
sz[x]=1;
for(auto i:adj[x]) {
if(0==st[i]) {
dfs(i);
sz[x]+=sz[i];
gy.push_back(i);
}
}
if(gy.empty()) {
dp[x]={0,0};
return ;
}
dp[x][0]=2*sz[x]-2;
for(int c:gy) {
dp[x][1]+=min(dp[c][0]+2*sz[c], dp[c][1]+8);
dp[x][0]+=dp[c][0];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n;
for(int i=1;i<n;++i) {
int a,b;
cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1);
cout<<min(dp[1][0], dp[1][1])<<"\n";
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 14ms | 6524 KiB | ||||
2 | Accepted | 85ms | 18820 KiB | ||||
subtask2 | 5/5 | ||||||
3 | Accepted | 45ms | 21484 KiB | ||||
4 | Accepted | 43ms | 22252 KiB | ||||
5 | Accepted | 54ms | 23024 KiB | ||||
6 | Accepted | 48ms | 23808 KiB | ||||
7 | Accepted | 46ms | 24568 KiB | ||||
subtask3 | 15/15 | ||||||
8 | Accepted | 3ms | 11532 KiB | ||||
9 | Accepted | 3ms | 11548 KiB | ||||
10 | Accepted | 3ms | 11540 KiB | ||||
11 | Accepted | 3ms | 11540 KiB | ||||
12 | Accepted | 3ms | 11552 KiB | ||||
13 | Accepted | 3ms | 11552 KiB | ||||
14 | Accepted | 3ms | 11556 KiB | ||||
subtask4 | 15/15 | ||||||
15 | Accepted | 3ms | 11560 KiB | ||||
16 | Accepted | 4ms | 11560 KiB | ||||
17 | Accepted | 3ms | 11568 KiB | ||||
18 | Accepted | 3ms | 11652 KiB | ||||
19 | Accepted | 3ms | 11580 KiB | ||||
20 | Accepted | 3ms | 11580 KiB | ||||
21 | Accepted | 8ms | 11584 KiB | ||||
subtask5 | 65/65 | ||||||
22 | Accepted | 75ms | 23940 KiB | ||||
23 | Accepted | 75ms | 25108 KiB | ||||
24 | Accepted | 82ms | 26252 KiB | ||||
25 | Accepted | 82ms | 27412 KiB | ||||
26 | Accepted | 86ms | 28568 KiB | ||||
27 | Accepted | 105ms | 43332 KiB | ||||
28 | Accepted | 96ms | 48004 KiB | ||||
29 | Accepted | 90ms | 39436 KiB | ||||
30 | Accepted | 86ms | 36532 KiB | ||||
31 | Accepted | 81ms | 34456 KiB | ||||
32 | Accepted | 79ms | 35328 KiB | ||||
33 | Accepted | 93ms | 36540 KiB | ||||
34 | Accepted | 61ms | 39708 KiB | ||||
35 | Accepted | 61ms | 40844 KiB | ||||
36 | Accepted | 71ms | 41396 KiB | ||||
37 | Accepted | 59ms | 42520 KiB | ||||
38 | Accepted | 59ms | 43388 KiB | ||||
39 | Accepted | 64ms | 44504 KiB | ||||
40 | Accepted | 75ms | 45576 KiB |