strange tle in eagle1 despite O(N)
source link: https://codeforces.com/blog/entry/125403
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
I am getting tle in https://www.spoj.com/problems/EAGLE1/, can anyone please point out the issue —
include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int,int>>> g(1e5+5); vector dp1(1e5+5,0); vector dp2(1e5+5,0); vector a(1e5+5); void dfs(int u, int p,int dogs) { dp1[u] = 0; dp2[u] = dogs; for(auto e:g[u]) {
if(e.first != p) { dfs(e.first,u,dogs+e.second); dp1[u] = max(dp1[e.first]+e.second,dp1[u]); } }
void dfs2(int u,int p) { a[u] = max(dp1[u],dp2[u]); for(auto e:g[u]) { if(e.first!= p) { dfs2(e.first,u); dp2[e.first] = dp2[u] + dp1[e.first]; } } }
int main() { int t; cin >> t; while(t--) { int n; cin >> n; for(int i = 0;i < n-1;i++) { int u,v,d; cin >> u >> v >> d; g[u].push_back(make_pair(v,d)); g[v].push_back(make_pair(u,d)); } dfs(1,-1,0); dfs2(1,-1); for(int i = 1; i <=n;i++) { cout << a[i] << " "; } } }
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK