hiho Week 11 register

Ended

Participants:433

Verdict:Accepted
Score:100 / 100
Submitted:2014-09-18 10:25:13

Lang:G++

Edit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int longDistance(vector<vector<int> > &node, int root, int &longPath, int preRoot){
    int maxDistance = 0;
    int maxPath = 0;
    int secondPath = 0;
    for(int i = 0; i < node[root].size(); i++){
        if(node[root][i] != preRoot){
            int path;
            maxDistance = max(maxDistance, longDistance(node, node[root][i], path, root));
            if(path + 1> maxPath){
                secondPath = maxPath;
                maxPath = path + 1;
            }
            else if(path + 1 > secondPath){
                secondPath = path + 1;
            }
        }
    }
    
    longPath = maxPath;
    return max(maxDistance, maxPath + secondPath);
}
int main(){
    int n;
    cin >> n;
    vector<vector<int> > node(n);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX