Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define N 100001
- using namespace std;
- ifstream fin("arborelantmaxim.in");
- ofstream fout("arborelantmaxim.out");
- int n, Maxim, sol;
- int Up[N], Down1[N], Down2[N], Sol[N];
- bool Use[N];
- vector < pair <int , int> >G[N];
- void citire()
- {
- fin >> n;
- for(int i=1; i<=n-1; i++)
- {
- int x, y, c;
- fin >> x >> y >> c;
- G[x].push_back(make_pair(y,c));
- G[y].push_back(make_pair(x,c));
- }
- }
- void DFS1(int Nod)
- {
- Use[Nod]=1;
- for(int i=0; i<(int)G[Nod].size(); ++i)
- {
- int Vecin = G[Nod][i].first;
- int Cost = G[Nod][i].second;
- if(!Use[Vecin])
- {
- DFS1(Vecin);
- int Drum = Down1[Vecin] + Cost;
- if(Drum >= Down1[Nod])
- {
- Down2[Nod] = Down1[Nod];
- Down1[Nod] = Drum;
- }
- else
- if(Drum > Down2[Nod])
- Down2[Nod]=Drum;
- }
- }
- }
- void DFS2(int Nod)
- {
- Use[Nod]=1;
- for(int i=0; i<(int)G[Nod].size(); ++i)
- {
- int Vecin = G[Nod][i].first;
- int Cost = G[Nod][i].second;
- if(!Use[Vecin])
- {
- Up[Vecin] = Up[Nod] + Cost;
- if(Down1[Nod] == (Down1[Vecin] + Cost))
- Up[Vecin] = max(Up[Vecin],Down2[Nod] + Cost);
- else
- Up[Vecin] = max(Up[Vecin],Down1[Nod] + Cost);
- DFS2(Vecin);
- }
- }
- Sol[Nod] = max(Up[Nod],Down1[Nod]);
- }
- int main()
- {
- citire();
- DFS1(1);
- memset(Use,0,sizeof(Use));
- DFS2(1);
- for(int i=1; i<=n; i++)
- if(Sol[i]>Maxim)
- Maxim=Sol[i];
- fout<<Maxim;
- return 0;
- }
Add Comment
Please, Sign In to add comment