当前位置:网站首页>codeforces Valera and Elections (这思维题是做不明白了)
codeforces Valera and Elections (这思维题是做不明白了)
2022-08-09 06:36:00 【先求一个导】
题目
题意: 给定一棵以1为根的树,有的边坏了。选择尽量少的点,这些点可以修复其到1的路径上坏的边。
思路: dfs。如果某个叶子对应的边恰好是坏的,那必定修复。之后我想的是从这里回溯到点1,把对应的边修好。但是时间复杂度高而且比较难实现。所以可以拿一个变量维护某个点对应的子树中是否存在点被选择过,这样即使他头上的边是坏的,也不会额外计数,而某个点对应子树中没有点被选过,他也是必选的。这种带返回值的dfs写的比较少,着实菜。
时间复杂度: O(n)
代码:
// Problem: C. Valera and Elections
// Contest: Codeforces - Codeforces Round #216 (Div. 2)
// URL: https://codeforces.com/problemset/problem/369/C
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
int n,m,k,T;
struct node{
int v,op;
bool operator<(const node&rhs)
{
return op > rhs.op;
}
};
vector<node> va[N];
int ans;
vector<int> res;
int dfs(int cur,int fa,int last) //上一条边
{
bool t = 0; //是否有节点被选
bool leaf = 1;
for(auto tmp:va[cur])
{
int j = tmp.v;
if(j==fa) continue; leaf = 0;
int op = tmp.op;
t |= dfs(j,cur,op);
}
// cout<<cur<<" "<<fa<<":"<<t<<endl;
if(leaf&&last==2)
{
res.push_back(cur); return true;}
if(!t && last==2) {
res.push_back(cur); t = true;} //没有孩子选
return t;
}
void solve()
{
cin>>n;
for(int i=0;i<n-1;++i)
{
int x,y,op; cin>>x>>y>>op;
va[x].push_back({
y,op}),va[y].push_back({
x,op});
}
// for(int i=1;i<=n;++i) sort(va[i].begin(),va[i].end());
dfs(1,0,1);
cout<<res.size()<<"\n";
for(int i=0;i<res.size();++i)
{
if(i) cout<<" ";
cout<<res[i];
}
}
signed main(void)
{
solve();
return 0;
}
边栏推荐
- 按图搜索1688商品接口(item_search_img-按图搜索1688商品(拍立淘接口)代码对接教程
- The working principle of the transformer (illustration, schematic explanation, understand at a glance)
- ByteDance Written Exam 2020 (Douyin E-commerce)
- crc calculation
- cut命令的使用实例
- Transaction concluded
- 思维方法 解决问题的能力
- 运放-运算放大器经典应用电路大全-应用电路大全
- Thread Pool Summary
- sql problem solving statement to create a table
猜你喜欢
A test engineer with an annual salary of 35W was laid off. Personal experience: advice that you have to listen to
Integer 线程安全的
变压器的工作原理(图解,原理图讲解,一看就懂)
leetcode 之 70 爬楼梯问题 (斐波那契数)
报错:FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS重大开销和将disab补充道
DevNet: Deviation Aware Networkfor Lane Detection
中英文说明书丨CalBioreagents 山羊抗人白蛋白,IgG组分
线程的6种状态
leetcode 之盛水问题
The solution that does not work and does not take effect after VScode installs ESlint
随机推荐
io.lettuce.core。RedisCommandTimeoutException命令超时
Use of PlantUML plugin in idea
APP商品详情源数据接口(淘宝/京东/拼多多/苏宁/抖音等平台详情数据分析接口)代码对接教程
2022-08-08: Given an array arr, it represents the height of the missiles that will appear in order from morning to night.When the cannon shoots missiles, once the cannon is set to shoot at a certain h
P7 Alibaba Interview Questions 2020.07 Sliding Window Algorithm (Alibaba Cloud Interview)
Gao Zelong, a famous digital collection expert and founder of the Digital Collection Conference, was interviewed by China Entrepreneur Magazine
Zero shift of leetcode
static静态关键字和继承
Simple Factory Pattern
CalBioreagents超全Id 蛋白兔单克隆抗体,助力科研
报错jinja2.exceptions.UndefinedError: ‘form‘ is undefined
mysql summary
The JVM thread state
报错:FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS重大开销和将disab补充道
阿里巴巴官方技术号
AD picture PCB tutorial 20 minutes clear label shop operation process, copper network
长沙学院2022暑假训练赛(一)六级阅读
VS2019常用快捷键
如何 认识与学习BASH
Singleton DCL (double check the lock) full han mode and the hungry