#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void * wmem;
char memarr[ 96000000 ] ;
template < class S, class T> inline S max_L( S a,T b) {
return a>= b? a: b;
}
template < class T> inline void walloc1d( T ** arr, int x, void ** mem = & wmem) {
static int skip[ 16 ] = { 0 , 15 , 14 , 13 , 12 , 11 , 10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 } ;
( * mem) = ( void * ) ( ( ( char * ) ( * mem) ) + skip[ ( ( unsigned long long ) ( * mem) ) & 15 ] ) ;
( * arr) = ( T* ) ( * mem) ;
( * mem) = ( ( * arr) + x) ;
}
struct graph{
int N;
int * es;
int ** edge;
void setEdge( int N__, int M, int A[ ] , int B[ ] , void ** mem = & wmem) {
int i;
N = N__;
walloc1d( & es, N, mem) ;
walloc1d( & edge, N, mem) ;
for ( i= ( 0 ) ; i< ( N) ; i++ ) {
es[ i] = 0 ;
}
for ( i= ( 0 ) ; i< ( M) ; i++ ) {
es[ A[ i] ] ++ ;
es[ B[ i] ] ++ ;
}
for ( i= ( 0 ) ; i< ( N) ; i++ ) {
walloc1d( & edge[ i] , es[ i] , mem) ;
}
for ( i= ( 0 ) ; i< ( N) ; i++ ) {
es[ i] = 0 ;
}
for ( i= ( 0 ) ; i< ( M) ; i++ ) {
edge[ A[ i] ] [ es[ A[ i] ] ++ ] = B[ i] ;
edge[ B[ i] ] [ es[ B[ i] ] ++ ] = A[ i] ;
}
}
void getDist( int root, int res[ ] , void * mem = wmem) {
int i;
int j;
int k;
int * q;
int s;
int z;
walloc1d( & q, N, & mem) ;
for ( i= ( 0 ) ; i< ( N) ; i++ ) {
res[ i] = - 1 ;
}
res[ root] = 0 ;
s= 0 ;
z= 1 ;
q[ 0 ] = root;
while ( z) {
i= q[ s++ ] ;
z-- ;
for ( j= ( 0 ) ; j< ( es[ i] ) ; j++ ) {
k= edge[ i] [ j] ;
if ( res[ k] >= 0 ) {
continue ;
}
res[ k] = res[ i] + 1 ;
q[ s+ z++ ] = k;
}
}
}
}
;
#define main dummy_main
int main( ) {
wmem = memarr;
return 0 ;
}
#undef main
int N;
int M;
int A[ 20000 ] ;
int B[ 20000 ] ;
int dist[ 20000 ] ;
class Solution{
public :
int treeDiameter( vector< vector< int >> & edges) {
int i;
dummy_main( ) ;
graph g;
N = edges.size ( ) + 1 ;
M = edges.size ( ) ;
for ( i= ( 0 ) ; i< ( M) ; i++ ) {
A[ i] = edges[ i] [ 0 ] ;
B[ i] = edges[ i] [ 1 ] ;
}
g.setEdge ( N, M, A, B) ;
g.getDist ( 0 , dist) ;
{
int KL2GvlyY;
int Q5VJL1cS = 0 ;
int e98WHCEY;
int cTE1_r3A;
int RZTsC2BF;
for ( KL2GvlyY= ( 0 ) ; KL2GvlyY< ( ( ( N) - 1 ) + 1 ) ; KL2GvlyY++ ) {
cTE1_r3A = dist[ KL2GvlyY] ;
if ( Q5VJL1cS== 0 || e98WHCEY< cTE1_r3A) {
e98WHCEY = cTE1_r3A;
Q5VJL1cS = 1 ;
RZTsC2BF = KL2GvlyY;
}
}
g.getDist ( RZTsC2BF, dist) ;
}
{
int FmcKpFmN;
int xr20shxY;
if ( N== 0 ) {
xr20shxY = 0 ;
}
else {
xr20shxY = dist[ 0 ] ;
for ( FmcKpFmN= ( 1 ) ; FmcKpFmN< ( N) ; FmcKpFmN++ ) {
xr20shxY = max_L( xr20shxY, dist[ FmcKpFmN] ) ;
}
}
return xr20shxY;
}
}
}
;
// cLay varsion 20191102-1
// --- original code ---
// #define main dummy_main
// {}
// #undef main
//
// int N, M, A[2d4], B[2d4];
// int dist[2d4];
//
// class Solution {
// public:
// int treeDiameter(vector<vector<int>>& edges) {
// dummy_main();
// graph g;
// N = edges.size() + 1;
// M = edges.size();
// rep(i,M) A[i] = edges[i][0], B[i] = edges[i][1];
// g.setEdge(N, M, A, B);
// g.getDist(0, dist);
// g.getDist(argmax(dist(N)), dist);
// return max(dist(N));
// }
// };
I3ByYWdtYSBHQ0Mgb3B0aW1pemUgKCJPZmF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T0oMCk7aTwoTik7aSsrKXsKICAgICAgZXNbaV0gPSAwOwogICAgfQogICAgZm9yKGk9KDApO2k8KE0pO2krKyl7CiAgICAgIGVkZ2VbQVtpXV1bZXNbQVtpXV0rK10gPSBCW2ldOwogICAgICBlZGdlW0JbaV1dW2VzW0JbaV1dKytdID0gQVtpXTsKICAgIH0KICB9CiAgdm9pZCBnZXREaXN0KGludCByb290LCBpbnQgcmVzW10sIHZvaWQgKm1lbSA9IHdtZW0pewogICAgaW50IGk7CiAgICBpbnQgajsKICAgIGludCBrOwogICAgaW50KnE7CiAgICBpbnQgczsKICAgIGludCB6OwogICAgd2FsbG9jMWQoJnEsIE4sICZtZW0pOwogICAgZm9yKGk9KDApO2k8KE4pO2krKyl7CiAgICAgIHJlc1tpXT0tMTsKICAgIH0KICAgIHJlc1tyb290XT0wOwogICAgcz0wOwogICAgej0xOwogICAgcVswXT1yb290OwogICAgd2hpbGUoeil7CiAgICAgIGk9cVtzKytdOwogICAgICB6LS07CiAgICAgIGZvcihqPSgwKTtqPChlc1tpXSk7aisrKXsKICAgICAgICBrPWVkZ2VbaV1bal07CiAgICAgICAgaWYocmVzW2tdPj0wKXsKICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgIH0KICAgICAgICByZXNba109cmVzW2ldKzE7CiAgICAgICAgcVtzK3orK109azsKICAgICAgfQogICAgfQogIH0KfQo7CiNkZWZpbmUgbWFpbiBkdW1teV9tYWluCmludCBtYWluKCl7CiAgd21lbSA9IG1lbWFycjsKICByZXR1cm4gMDsKfQojdW5kZWYgbWFpbgppbnQgTjsKaW50IE07CmludCBBWzIwMDAwXTsKaW50IEJbMjAwMDBdOwppbnQgZGlzdFsyMDAwMF07CmNsYXNzIFNvbHV0aW9uewogIHB1YmxpYzoKICBpbnQgdHJlZURpYW1ldGVyKHZlY3Rvcjx2ZWN0b3I8aW50Pj4mIGVkZ2VzKXsKICAgIGludCBpOwogICAgZHVtbXlfbWFpbigpOwogICAgZ3JhcGggZzsKICAg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