Cod sursa(job #3150174)

Utilizator KRISTY06Mateiu Ianis Cristian Vasile KRISTY06 Data 15 septembrie 2023 09:08:52
Problema Diametrul unui arbore Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;

ifstream fin("darb.in");
ofstream fout("darb.out");

const int MAX_LENGTH = 100000;

int maxLen;

vector<int> graph[MAX_LENGTH + 1];

void gen(int nodesNr, int currentNode, int prevNode, int len) {
    for (vector<int>::iterator it = graph[currentNode].begin(); it < graph[currentNode].end(); ++it) {
        if (*it != prevNode) {
            gen(nodesNr, *it, currentNode, len + 1);
            if (len + 1 > maxLen) {
                maxLen = len + 1;
            }
        }
    }
}

int main() {
    int nodesNr;
    fin >> nodesNr;
    for (int i = 1; i < nodesNr; ++i) {
        int start, end;
        fin >> start >> end;
        graph[start].push_back(end);
        graph[end].push_back(start);
    }
    for (int node = 1; node <= nodesNr; ++node) {
        gen(nodesNr, node, 0, 1);
    }
    fout << maxLen;
    return 0;
}
/*
 11
 1 2
 1 3
 1 4
 2 5
 3 6
 4 7
 5 8
 5 9
 6 10
 10 11
 =>
 8
 
 6
 2 3
 6 2
 6 1
 6 5
 5 4
 =>
 5
 */