Submission #770084

#TimeUsernameProblemLanguageResultExecution timeMemory
770084vjudge1Paths (RMI21_paths)C++17
0 / 100
49 ms8096 KiB
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") using namespace std; #define vi vector<int> #define vl vector<long long> #define vii vector<pair<int,int>> #define vll vector<pair<long long,long long>> #define pb push_back #define ll long long #define ld long double #define nl '\n' #define boost ios::sync_with_stdio(false) #define mp make_pair #define se second #define fi first #define fore(i, y) for(int i = 0; i < y; i++) #define forr(i,x,y) for(int i = x;i<=y;i++) #define forn(i,y,x) for(int i = y; i >= x; i--) #define all(v) v.begin(),v.end() #define sz(v) v.size() #define clr(v,k) memset(v,k,sizeof(v)) #define rall(v) v.rbegin() , v.rend() #define pii pair<int,int> #define pll pair<ll , ll> const ll MOD = 1e9 + 7; const ll INF = 1e18 + 1; ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;} // greatest common divisor (PGCD) ll lcm(ll a , ll b) {return a * (b / gcd(a , b));} // least common multiple (PPCM) // HERE IS THE SOLUTION const int MAX_NODES = 100001; vi in(MAX_NODES , 0); vi out(MAX_NODES , 0); void dfs1(vector<vii> &v, int u, int parent) { in[u] = 0; for (auto child : v[u]) { if (child.fi == parent) continue; dfs1(v, child.fi, u); in[u] = max(in[u], child.se + in[child.fi]); } } void dfs2(vector<vii> &v, int u, int parent) { int mx1 = -1, mx2 = -1; for (auto child : v[u]) { if (child.fi == parent) continue; if (in[child.fi] >= mx1) { mx2 = mx1; mx1 = in[child.fi]; } else if (in[child.fi] > mx2) mx2 = in[child.fi]; } for (auto child : v[u]) { if (child.fi == parent) continue; int longest = mx1; if (mx1 == in[child.fi]) longest = mx2; out[child.fi] = child.se + max(out[u], child.se + longest); dfs2(v, child.fi, u); } } int main() { cin.tie(0); cout.tie(0); boost; // freopen("inp.txt" , "r" , stdin); int n , k; cin>>n>>k; vector<vii> adj(n); fore(i , n - 1) { int u , v,c; cin>>u>>v>>c; u--; v--; adj[u].pb({v,c}); adj[v].pb({u,c}); } dfs1(adj , 0 , -1); dfs2(adj , 0 , -1); fore(i , n) { cout<<max(in[i] , out[i])<<nl; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...