跳到正文

题解

题解:AT_abc394_f [ABC394F] Alkane

Ivan422约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 3 分钟

版本

  1. v1当前

注意到这是一道化学题。

但是初一不学化学,所以不做。

考虑开局记录一下每个点的度数。

设 fpf_p 为以一个点为起点而扩展出的最大点数。

直接深搜。

当 pp 的儿子 vv 的度数超过 44,就有可能成为一个新的 Alkane 子树,我们把 fvf_v 加入一个 vector 里面。

接着我们考虑枚举。

先统计 fpf_p,初始自己为 fp=1f_p=1,即这个点本事。

在子树内选择 33 个作为 Alkane 子树。

为什么不是 44 个呢?因为有父节点。

我们就可以简单的把 fvf_v 中前 33 大的加入 fpf_p。

再统计答案。

注意这次是选择 44 个,因为答案 ansans 是把四个子树的答案加起来,并非转移到 pp。

所以我们的 ansans 就是把 fvf_v 中前 44 大的加和加上 11(节点本身) 求出 ansans,取最大值即可。

C++
#include<bits/stdc++.h>
using namespace std;
namespace ivan422{
	#define ll long long
	#define ld long double
	#define il __int128
	#define fl __float128
	#define x0 x_0
	#define x1 x_1
	#define y0 y_0
	#define y1 y_1
	#define yn y_n
	#define j0 j_0
	#define j1 j_1
	#define k0 k_0
	#define k1 k_1
	#define d0 d_0
	#define d1 d_1
	#define pb push_back
	#define pf push_front
	#define mp make_pair
	#define ar array<int,3>
	#define us unsigned
	#define io ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
};
using namespace ivan422;
const int N=2e5+10,M=1e3+10;
const int P1=1e9+7,P9=998244353;
const double pi=3.1415926535,eps=1e-7;
int n,U,V,f[N],deg[N],res,ans,cnt,su;
vector<int>e[N],g[N];
bool vis[N];
void dfs(int p,int fa){
	for(auto v:e[p]){
		if(v!=fa){
			dfs(v,p);
			if(deg[p]>=4)g[p].push_back(f[v]);
		}
	}
	sort(g[p].begin(),g[p].end(),greater<int>());
	cnt=0,su=0;
	f[p]=1;
	for(auto v:g[p])if((++cnt)<=3)f[p]+=v;
	ans=max(ans,f[p]+1);
	cnt=0;
	for(auto v:g[p])if((++cnt)<=4)su+=v;
	ans=max(ans,su+1);
}
signed main(){
	io;
	cin>>n;
	for(int i=1;i<n;i++){
		cin>>U>>V;
		deg[U]++,deg[V]++;
		e[U].push_back(V);
		e[V].push_back(U);
	}
	ans=-1;
	dfs(1,0);
	cout<<(ans<5?-1:ans);
	return 0;
}
原文