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


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

    场上看错题了…


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

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

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

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

    b [ y ] ≥ b [ x ] b[y]\ge b[x] b[y]b[x]

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

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

    #include
    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;
    }
    
    
    
    • 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
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
  • 相关阅读:
    C#中关于 object,dynamic 一点使用心得
    kubernetes集群编排——k8s资源监控
    【Linux Kernel 6.1 代码剖析】- 进程管理概论
    多模态大一统:开启全模态LLM和通用AI时代的大门
    EMQX配置ssl/tls双向认证+EMQX http客户端设备认证(Java实现)_真实业务实践
    c#面试编程题
    python实现熵权法
    第五章:抽象类
    C++,STL,,vector容器
    流式数据处理与高吞吐消息传递:深入探索Kafka技术的奥秘
  • 原文地址:https://blog.csdn.net/zhangtingxiqwq/article/details/133780272