巧妙设计状态+不断对拍寻找合适贪心策略:P8341

https://www.luogu.com.cn/problem/P8341

场上看错题了...


考虑维护几个东西: a x , b x ax,bx ax,bx 表示完整匹配,半完整匹配的数量。 p x px px 表示某条向上路径在 x x x 完成任务,可以变成 b b b。

然后如果 x x x 位置有向上的话,我们贪心希望它和 p p p 合并。那么保留的应该是最小的,我们记在 t o p x topx topx 里。我们分情况讨论 t o p x topx topx 是否需要向上延展。

如果 t o p x topx topx 不存在,要么是拿一个 b b b 来代替,要么拿两个 a a a 来换

考虑合并子树。 b y by by 先要加上 p x px px,也就是完成任务变成了向上路径的。

令 b y ≥ b x by\ge bx by≥bx

然后我们先贪心让 b y by by 和 b x bx bx 匹配,然后把 a x ax ax 换成2被的向上路径匹配。如果还不行,就只能保留向上路径。

然后你会发现有巨多细节和Corner case,然后你就可以写对拍。大致调7-8次应该就差不多了。

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
//#define int long long
inline int read(){int x=0,f=1;char ch=getchar(); while(ch<'0'||
ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define Z(x) (x)*(x)
#define pb push_back
//mt19937 rand(time(0));
//mt19937_64 rand(time(0));
//srand(time(0));
#define N 200010
//#define M
//#define mo
int n, m, i, j, k, T;
int u, v, dep[N], a[N], b[N], top[N], p[N], E[N], g[N]; 
vector<int>G[N]; 

void dfs1(int x, int fa) {
	dep[x]=dep[fa]+1; 
	for(int y : G[x]) {
		if(y==fa) continue; 
		dfs1(y, x); 
	}
}

int U(int x) {
	return x/2+x%2; 
}

void dfs2(int x, int fa) {
	int k=0; 
	top[x]=0; 
	for(int y : G[x]) {
		if(y==fa) continue; 
		dfs2(y, x); 
//		if(top[y]==x) top[y]=0, ++b[y], --p[x]; 
		if(dep[top[y]]<dep[top[x]]) swap(top[x], top[y]); 
//		if(dep[top[y]]<=dep[x] && dep[top[x]]<=dep[x]) ++b[y], --p[top[y]]; 
//		if(x==1) printf("# %d\n", p[x]); 
		b[y]+=p[x]; p[x]=0; // tree y 给x的
//		if(x==1) printf("%d : %d %d | %d %d\n", y, a[x], b[x], a[y], b[y]); 
		
		if(b[x]>b[y]) swap(b[x], b[y]), swap(a[x], a[y]); 
		if(b[y]==b[x]) a[x]+=b[x], b[x]=0; 
		else if(b[y]-b[x]<=2*a[x]) {
//			if(x==5) printf("%d %d %d\n", a[x], U(b[y]-b[x]), b[y]); 
			a[x]-=U(b[y]-b[x]); a[x]+=b[y]; 
			b[x]=(b[y]-b[x])%2; 
		}
		else {
//			if(x==3) printf("-----\n"); 
			k=b[x];  b[x]=b[y]-b[x]-2*a[x]; 
			a[x]=2*a[x]+k; 
		}
		a[x]+=a[y]; 
	}
//	printf("%d : %d %d | %d | E(%d)\n", x, a[x], b[x], top[x], E[x]); 
	if(!E[x]) return ; 
	if(dep[top[x]]<=dep[E[x]]) return ; 
	else if(dep[top[x]]<dep[x]) {
		--p[top[x]]; ++p[E[x]]; top[x]=E[x]; 
	}
	else {
//		if(x==5) printf("i love crx\n"); 
		top[x]=E[x]; ++p[E[x]]; 
		if(b[x]) --b[x]; 
		else if(a[x]) --a[x], b[x]=1; 
//		else 
	}
	
	
//		printf("%d : %d %d | %d | E(%d)\n", x, a[x], b[x], top[x], E[x]); 

}

signed main()
{
//	freopen("in.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);
	T=read();
	while(T--) {
		n=read(); m=read(); 
		for(i=0; i<=n; ++i) 
			G[i].clear(), E[i]=a[i]=b[i]=p[i]=dep[i]=g[i]=top[i]=0; 
		for(i=1; i<n; ++i) {
			u=read(); v=read(); 
			G[u].pb(v); G[v].pb(u); 
		}
		dfs1(1, 0); dep[0]=1e9; 
		for(i=1; i<=m; ++i) {
			u=read(); v=read(); 
			if(dep[u]<dep[E[v]]) E[v]=u; 
		}
		dfs2(1, 0); 
		printf("%d\n", a[1]+b[1]); 
	}

	return 0;
}
相关推荐
qz5zwangzihan11 天前
题解:Atcoder Beginner Contest abc467 F - Email Scheduling Optimization
c++·贪心·atcoder·平衡树·abc467·abc467_f·fhq-treap
cpp_25016 天前
P6625 [省选联考 2020 B 卷] 卡牌游戏
数据结构·c++·算法·前缀和·贪心·洛谷题解·省选
cpp_25018 天前
P10098 [ROIR 2023] 地铁建设 (Day 2)
数据结构·c++·算法·贪心·二分答案·洛谷题解
tkevinjd9 天前
力扣300-最长递增子序列
算法·leetcode·职场和发展·动态规划·贪心
芜湖xin10 天前
【题解-信息学奥赛一本通】1226:装箱问题
贪心
Tisfy1 个月前
LeetCode 3689.最大子数组总值 I:What The Medium
算法·leetcode·题解·贪心·模拟·脑筋急转弯
Misnearch2 个月前
3635. 最早完成陆地和水上游乐设施的时间II
leetcode·贪心·排序
不太会a2 个月前
Educational Codeforces Round 175 (Rated for Div. 2) C
贪心·二分
芜湖xin2 个月前
【题解-洛谷】P1012 [NOIP 1998 提高组] 拼数
算法·贪心
lulutoy2 个月前
1624: 安排电视节目(PIPIPOJ)
贪心