Cod sursa(job #2348883)

Utilizator Anastasia11Susciuc Anastasia Anastasia11 Data 20 februarie 2019 02:36:28
Problema Diametrul unui arbore Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <fstream>
#include <cstdio>
#include <vector>
#include <cstring>
#define Nmax 100001

using namespace std;

ofstream g("darb.out");

int n, nod, ans;
bool seen[Nmax];
vector <int> v[Nmax];

FILE* fin=fopen("darb.in","r");
const unsigned maxb=30000192;
char buf[maxb];
unsigned ptr=maxb;

inline unsigned getInt()
{
    unsigned nr=0;
	while(buf[ptr]<'0'||'9'<buf[ptr])
		if(++ptr>=maxb)
			fread(buf,maxb,1,fin),ptr=0;
	while('0'<=buf[ptr]&&buf[ptr]<='9'){
		nr=nr*10+buf[ptr]-'0';
		if(++ptr>=maxb)
			fread(buf,maxb,1,fin),ptr=0;
	}
	return nr;
}

void dfs(int x, int nr)
{
    seen[x] = 1;
    if (nr > ans)
    {
        ans = nr;
        nod = x;
    }
    for (int i = 0, l=v[x].size(); i < l; i++)
        if (!seen[v[x][i]]) dfs(v[x][i], nr+1);

}
int main()
{
    n=getInt();
    //scanf("%d", &n);
    for (int i = 1, x, y; i <= n; i++)
    {
        x=getInt();
        y=getInt();
        //scanf("%d%d", &x, &y);
        v[x].push_back(y);
        v[y].push_back(x);
    }

    dfs(1, 1);
    memset(seen, 0, sizeof(seen));
    dfs(nod, 1);

    g << ans;
    return 0;
}