牛客挑战赛45 我是 A 题 树dfs

链接:https://ac.nowcoder.com/acm/contest/8563/B
来源:牛客网

题目描述
众所周知,题目名称与题面没有任何关系。

你发现了一道 div2 的 A 题,下面是这道题的题面:

给定一张 n{n}n 个点,n−1{n-1}n−1 条边的无向联通图和一个常数 k{k}k,每个点拥有点权 wiw_iwi​,每条边拥有边权 valival_ivali​,现在他决定保留一些边,这样将剩余部分连通块,牛牛的目标是使得每个剩余的连通块的点权和都是 k{k}k 的倍数,同时最小化被保留的边的权值和。
输入描述:

第一行两个正整数 n,k

接下来一行 n 个正整数,第 i 个正整数表示节点 i 的权值是 wiw_iwi​

接下来 n-1 行,每行 3 个正整数,表示一条连接 u,v 边权为 valival_ivali​ 的边。

输出描述:

输出一行一个正整数最小的权值和。

示例1
输入
复制

5 2
1 0 1 1 1
1 2 5
1 4 3
2 3 2
2 5 1

输出
复制

6

说明

选择 (1,4),(2,3),(2,5)

示例2
输入
复制

10 3
1 3 4 2 1 6 2 3 5 6
1 2 2
1 4 1
2 3 3
2 6 3
2 10 2
5 6 4
5 7 4
6 8 3
6 9 1

输出
复制

12

说明

选择 (1,4),(2,3),(2,6),(5,7),(6,9)

备注:

n,k≤106,wi,vali≤109n,k\le 106,w_i,val_i\le109n,k≤106,wi​,vali​≤109
∑i=1nwi mod k=0\sum_{i=1}^nw_i\bmod k=0∑i=1n​wi​modk=0


题意:给定一颗,有点权,有边权,给定随机值K,你要删除一些边,
要求:

  1. 删除后剩余联通块点权和为K的倍数,
  2. 最小化所有联通快的边权和

题解:对于任意一个节点U,如果子树V的点权和为K的倍数,就可以删除边(U,V)
反之则保留边(U,V)

c++代码

int n, m, Q, K, w[MAXN];
struct Edge {
	int v, w;
} ;
vector<Edge> G[MAXN];
/**
 * 		最后状态一定是多个联通快,每个块都是K的倍数
 * 		对于任意一个点u, 如果u的子树v不是k的倍数,就留下边(u,v)
 * 		反之删除
 */
int ans;
int dfs(int u, int fa) {
	int sum = w[u];
	for(auto& ed : G[u]) {
		int v = ed.v, td = ed.w;
		if(v == fa) continue ;
		int tmp = dfs(v, u);
		if((tmp % K) != 0) {
			sum += tmp;
			ans += td;
		}
	}
	return sum;
}

完整代码

#define debug
#ifdef debug
#include <time.h>
#include "win_majiao.h"
#endif

#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>

#define MAXN ((int)1e6+7)
#define ll long long
#define int long long
#define INF (0x7f7f7f7f)
#define QAQ (0)

using namespace std;
typedef vector<vector<int> > VVI;

#define show(x...) \
	do { \
		cout << "[" << #x << " -> "; \
		err(x); \
	} while (0)

void err() { cout << "]" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }

namespace FastIO{

	char print_f[105];
	void read() {}
	void print() { putchar('\n'); }

	template <typename T, typename... T2>
	   inline void read(T &x, T2 &... oth) {
		   x = 0;
		   char ch = getchar();
		   ll f = 1;
		   while (!isdigit(ch)) {
			   if (ch == '-') f *= -1; 
			   ch = getchar();
		   }
		   while (isdigit(ch)) {
			   x = x * 10 + ch - 48;
			   ch = getchar();
		   }
		   x *= f;
		   read(oth...);
	   }
	template <typename T, typename... T2>
	   inline void print(T x, T2... oth) {
		   ll p3=-1;
		   if(x<0) putchar('-'), x=-x;
		   do{
				print_f[++p3] = x%10 + 48;
		   } while(x/=10);
		   while(p3>=0) putchar(print_f[p3--]);
		   putchar(' ');
		   print(oth...);
	   }
} // namespace FastIO
using FastIO::print;
using FastIO::read;

int n, m, Q, K, w[MAXN];
struct Edge {
	int v, w;
} ;
vector<Edge> G[MAXN];
/**
 * 		最后状态一定是多个联通快,每个块都是K的倍数
 * 		对于任意一个点u, 如果u的子树v不是k的倍数,就留下边(u,v)
 * 		反之删除
 */
int ans;
int dfs(int u, int fa) {
	int sum = w[u];
	for(auto& ed : G[u]) {
		int v = ed.v, td = ed.w;
		if(v == fa) continue ;
		int tmp = dfs(v, u);
		if((tmp % K) != 0) {
			sum += tmp;
			ans += td;
		}
	}
	return sum;
}

signed main() {
#ifdef debug
	freopen("test.txt", "r", stdin);
	clock_t stime = clock();
#endif
	read(n, K);
	for(int i=1; i<=n; i++) read(w[i]);
	int u, v, val;
	for(int i=1; i<n; i++) {
		read(u, v, val);
		G[u].push_back({v, val}),
		G[v].push_back({u, val});
	}
	dfs(1, 1);
	printf("%lld\n", ans);



#ifdef debug
	clock_t etime = clock();
	printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif 
	return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值