今天很累,根本不想写代码

今天一天基本等于没写代码,主要是自己感觉头沉沉的,什么也不想做。傻傻的过了一个下午。我不喜欢这种空虚的感觉,感觉自己没什么可写的东西。

#include <iostream> #include <map> #include <vector> #include <unordered_map> #include <algorithm> #include <cmath> #define int long long using namespace std; const int N = 1e5; vector<vector<int>> e(N + 1); vector<int> a(N + 1); int dfs(int u, int op, int res) { if(e[u].empty()) return res; int max_a = -1e9; int min_a = 1e9; int x, y; for(auto v : e[u]) { if(max_a <= a[v] && x > v) { max_a = a[v]; x = v; } if(min_a >= a[v] && y > v) { min_a = a[v]; y = v; } } if(op) { return dfs(x, op ^ 1, res + max_a); } else { return dfs(y, op ^ 1, res + min_a); } } signed main() { int n, k; cin >> n >> k; for(int i = 2; i <= n; i ++) { int p; cin >> p; e[p].push_back(i); } for(int i = 1; i <= n; i ++) { cin >> a[i]; } cout << dfs(1, 1, k); return 0; }#include <iostream> #include <map> #include <vector> #include <unordered_map> #include <algorithm> #include <cmath> #define int long long using namespace std; const int N = 1e5; vector<vector<int>> e(N + 1); vector<int> a(N + 1); int dfs(int u, int op, int res) { if(e[u].empty()) return res; int max_a = -1e9; int min_a = 1e9; int x, y; for(auto v : e[u]) { if(max_a < a[v] || max_a == a[v] && x > v) { max_a = a[v]; x = v; } if(min_a > a[v] || min_a == a[v] && y > v) { min_a = a[v]; y = v; } } if(op) { return dfs(x, op ^ 1, res + max_a); } else { return dfs(y, op ^ 1, res + min_a); } } signed main() { int n, k; cin >> n >> k; for(int i = 2; i <= n; i ++) { int p; cin >> p; e[p].push_back(i); } for(int i = 1; i <= n; i ++) { cin >> a[i]; } cout << dfs(1, 1, k); return 0; }为什么上面的代码没超时,下面的代码超时了
最新发布
04-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值