Problem link:- https://www.spoj.com/problems/PT07Z
https://www.codechef.com/RC2018/problems/MAXDIS
///...................SUBHASHIS MOLLICK...................///
///.....DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING....///
///.............ISLAMIC UNIVERSITY,BANGLADESH.............///
///....................SESSION-(14-15)....................///
#include<bits/stdc++.h>
using namespace std;
#define sf(a) scanf("%lld",&a)
#define sf2(a,b) scanf("lld%lld",&a,&b)
#define sf3(a,b,c) scanf("lld%lld%lld",&a,&b,&c)
#define pf(a) printf("%lld",a)
#define pf2(a,b) printf("lld%lld",a,b)
#define pf3(a,b,c) printf("lld%lld%lld",a,b,c)
#define nl printf("\n")
#define ll long long
#define pb push_back
#define MPI map<int,int>mp
#define fr(i,n) for(i=0;i<n;i++)
#define fr1(i,n) for(i=1;i<=n;i++)
#define frl(i,a,b) for(i=a;i<=b;i++)
//freopen("Input.txt","r",stdin);
//freopen("Output.txt","w",stdout);
vector<long>vec[100005];
long n;
long bfs(long source)
{
queue<long>q;
long dist[100005]= {0},vis[100005]= {0},boroman=0,mx=0;
dist[source]=0;
vis[100005]=1;
q.push(source);
long i;
while(!q.empty())
{
long u=q.front();
if(mx<dist[u])
{
mx=dist[u];
boroman=u;
}
for(i=0; i<vec[u].size(); i++)
{
long v=vec[u][i];
if(vis[v]==0)
{
dist[v]=dist[u]+1;
vis[v]=1;
q.push(v);
}
}
q.pop();
}
return boroman;
}
long bfss(long source)
{
queue<long>q;
long vis[100005]= {0},dist[100005]= {0},i,mx=0;
q.push(source);
vis[source]=1;
dist[source]=0;
while(!q.empty())
{
long u=q.front();
vis[u]=1;
if(mx<dist[u])
{
mx=dist[u];
}
for(i=0; i<vec[u].size(); i++)
{
long v=vec[u][i];
if(vis[v]==0)
{
dist[v]=dist[u]+1;
// mx=max(mx,dist[v]);
vis[v]=1;
q.push(v);
}
}
q.pop();
}
return mx;
/*for(i=0; i<n; i++)
{
if(i==source)
{
printf("%ld\n",mx);
}
else
printf("%ld\n",dist[i]);
}*/
}
main()
{
while(scanf("%ld",&n)!=EOF)
{
long i,u,v,w,vis[100005]= {0},src;
for(i=1; i<n; i++)
{
scanf("%ld%ld",&u,&v);
vec[u].push_back(v);
vec[v].push_back(u);
vis[u]++,vis[v]++;
}
for(i=0; i<n; i++)
{
if(vis[i]==1)
{
src=i;
break;
}
}
long boro=bfs(src);
cout<<bfss(boro)<<endl;
for(i=0; i<=100000; i++)
{
vec[i].clear();
}
}
}
https://www.codechef.com/RC2018/problems/MAXDIS
///...................SUBHASHIS MOLLICK...................///
///.....DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING....///
///.............ISLAMIC UNIVERSITY,BANGLADESH.............///
///....................SESSION-(14-15)....................///
#include<bits/stdc++.h>
using namespace std;
#define sf(a) scanf("%lld",&a)
#define sf2(a,b) scanf("lld%lld",&a,&b)
#define sf3(a,b,c) scanf("lld%lld%lld",&a,&b,&c)
#define pf(a) printf("%lld",a)
#define pf2(a,b) printf("lld%lld",a,b)
#define pf3(a,b,c) printf("lld%lld%lld",a,b,c)
#define nl printf("\n")
#define ll long long
#define pb push_back
#define MPI map<int,int>mp
#define fr(i,n) for(i=0;i<n;i++)
#define fr1(i,n) for(i=1;i<=n;i++)
#define frl(i,a,b) for(i=a;i<=b;i++)
//freopen("Input.txt","r",stdin);
//freopen("Output.txt","w",stdout);
vector<long>vec[100005];
long n;
long bfs(long source)
{
queue<long>q;
long dist[100005]= {0},vis[100005]= {0},boroman=0,mx=0;
dist[source]=0;
vis[100005]=1;
q.push(source);
long i;
while(!q.empty())
{
long u=q.front();
if(mx<dist[u])
{
mx=dist[u];
boroman=u;
}
for(i=0; i<vec[u].size(); i++)
{
long v=vec[u][i];
if(vis[v]==0)
{
dist[v]=dist[u]+1;
vis[v]=1;
q.push(v);
}
}
q.pop();
}
return boroman;
}
long bfss(long source)
{
queue<long>q;
long vis[100005]= {0},dist[100005]= {0},i,mx=0;
q.push(source);
vis[source]=1;
dist[source]=0;
while(!q.empty())
{
long u=q.front();
vis[u]=1;
if(mx<dist[u])
{
mx=dist[u];
}
for(i=0; i<vec[u].size(); i++)
{
long v=vec[u][i];
if(vis[v]==0)
{
dist[v]=dist[u]+1;
// mx=max(mx,dist[v]);
vis[v]=1;
q.push(v);
}
}
q.pop();
}
return mx;
/*for(i=0; i<n; i++)
{
if(i==source)
{
printf("%ld\n",mx);
}
else
printf("%ld\n",dist[i]);
}*/
}
main()
{
while(scanf("%ld",&n)!=EOF)
{
long i,u,v,w,vis[100005]= {0},src;
for(i=1; i<n; i++)
{
scanf("%ld%ld",&u,&v);
vec[u].push_back(v);
vec[v].push_back(u);
vis[u]++,vis[v]++;
}
for(i=0; i<n; i++)
{
if(vis[i]==1)
{
src=i;
break;
}
}
long boro=bfs(src);
cout<<bfss(boro)<<endl;
for(i=0; i<=100000; i++)
{
vec[i].clear();
}
}
}
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন