codeforces 369C

本文介绍了一种修复树状路网中坏路的算法,通过遍历树结构,找到最小节点集合,使得修复这些节点就能修复整棵树的所有路径。

题意:给出一棵树, 边有两种 类型1, 代表路是好的, 类型2代表路是坏的, 我们要修复所有的路, 当我们修复结点x的时候, 那么它到结点1之间的所有路都会被修复,求最小的修路节点的集合

思路:对于节点x,它到父节点的边为类型2,那么如果节点x的所有子树都不存在类型为2的边,则将节点x加入子集,否则任意一个子树中的边得到维修,x到父节点的边也一定会被维修,这种情况x就不加入子集。

#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
const int qq = 1e5+10;
int top=0;
int num[qq];
int dp[qq];
int vis[qq];
struct Node{
	int to,type;
};
vector<Node>v[qq];
int dfs(int rt, int flag){
	int len = v[rt].size();
	int s=0;			//s是标记变量, s=0代表其子树中没有类型2的边,否则的话就代表有 
	vis[rt] = 1;		// 返回的变量可以消除标记、 
	for(int i=0; i<len; ++i){
		int son = v[rt][i].to;
		if(vis[son])	continue;
		if(v[rt][i].type==2)	s+=dfs(son, 1);
		else	s+=dfs(son, 0);
	}
	if(flag && !s){
		num[top++] = rt;
		return 1;
	}
	return s;
}
int main(){
	int n;scanf("%d",&n);
	Node tmp;
	for(int i=0; i<n-1; ++i){
		int a,b,c;scanf("%d%d%d",&a,&b,&c);
		tmp.to = b;
		tmp.type = c;
		v[a].push_back(tmp);
		tmp.to = a;
		v[b].push_back(tmp);
	}
	dfs(1, 0);
	printf("%d\n", top);
	for(int i=0; i<top; ++i)
		printf("%d ", num[i]);
	return 0;
}


### Codeforces Problem 1332C Explanation The provided references pertain specifically to problem 742B on Codeforces rather than problem 1332C. For an accurate understanding and solution approach for problem 1332C, it's essential to refer directly to its description and constraints. However, based on general knowledge regarding competitive programming problems found on platforms like Codeforces: Problem 1332C typically involves algorithmic challenges that require efficient data structures or algorithms such as dynamic programming, graph theory, greedy algorithms, etc., depending upon the specific nature of the task described within this particular question[^6]. To provide a detailed explanation or demonstration concerning **Codeforces problem 1332C**, one would need direct access to the exact statement associated with this challenge since different tasks demand tailored strategies addressing their unique requirements. For obtaining precise details about problem 1332C including any sample inputs/outputs along with explanations or solutions, visiting the official Codeforces website and navigating to contest number 1332 followed by examining section C is recommended. ```python # Example pseudo-code structure often seen in solving competitive coding questions. def solve_problem_1332C(input_data): # Placeholder function body; actual logic depends heavily on the specifics of problem 1332C. processed_result = process_input(input_data) final_answer = compute_solution(processed_result) return final_answer input_example = "Example Input" print(solve_problem_1332C(input_example)) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值