Submission #846335

#TimeUsernameProblemLanguageResultExecution timeMemory
846335vjudge1Torrent (COI16_torrent)C++17
0 / 100
62 ms11900 KiB
#include <iostream>
#include <vector>

using namespace std;

vector<int> graph[100001];
int visited[100001];

int dfs(int node, int parent) {
    int maxTime = 0;
    
    for(int i = 0; i < graph[node].size(); i++) {
        int nextNode = graph[node][i];
        if(nextNode == parent) continue;
        int time = dfs(nextNode, node) + 1;
        maxTime = max(maxTime, time);
    }
    
    return maxTime;
}

int main() {
    int n, a, b;
    cin >> n >> a >> b;
    
    for(int i = 0; i < n - 1; i++) {
        int x, y;
        cin >> x >> y;
        graph[x].push_back(y);
        graph[y].push_back(x);
    }
    
    int timeA = dfs(a, -1);
    int timeB = dfs(b, -1);
    
    int totalTime = timeA + timeB;
    
    cout << totalTime << endl;
    
    return 0;
}

Compilation message (stderr)

torrent.cpp: In function 'int dfs(int, int)':
torrent.cpp:12:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   12 |     for(int i = 0; i < graph[node].size(); i++) {
      |                    ~~^~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...