0%

ABC-385-E - Snowflake Tree

思路讲解

总体上来说这题还是比较顺的,就是顺着做就好了,也没用什么算法,for循环就完了

1
2
3
4
sort(all1(sons),greater<ll>());
FOR(j, 0, sons.size()-1){
remainNode=max(remainNode,1+sons[j]*(j+1)+j+1);
}

核心是这一段代码,因为是0-based,所以要 j+1

AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define ROF(i, a, b) for (int i = a; i >= b; --i)
#define all(x,size) x.begin()+1,x.begin()+size+1
#define all1(x) x.begin(),x.end()

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef array<ll,3> arr;
typedef double DB;
const ll MAXN=static_cast<ll>(3e5)+10,MAXval=static_cast<ll>(1e18)+3;

ll N,T;
vector<ll> G[MAXN];

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>N;
FOR(i, 1, N-1){
ll u,v;
cin>>u>>v;
G[u].push_back(v);
G[v].push_back(u);
}
ll ans=MAXval;
FOR(i, 1, N){
ll remainNode=0;
vector<ll> sons; // 他的子节点的子节点有几个?
FOR(j, 0, G[i].size()-1){
ll node=G[i][j];
sons.push_back(G[node].size()-1);
}
sort(all1(sons),greater<ll>());
FOR(j, 0, sons.size()-1){
remainNode=max(remainNode,1+sons[j]*(j+1)+j+1);
}
ans=min(ans,N-remainNode);

// cout<<"debug:\n";
// FOR(j, 0, sons.size()-1){
// cout<<sons[j]<<' ';
// }
// cout<<'\n';
}
cout<<ans<<'\n';
return 0;
}
/*
AC
https://atcoder.jp/contests/abc385/submissions/62235314
*/

心路历程(WA,TLE,MLE……)