/***
Md. Namzul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size) for(int i=0 ; i<size ; i++) cin>>array[i] ;
#define new_line pf("\n")
#define highest_int 2147483647
#define n_INF -99999999
vector<int> G[10001] ;
int dis1[10001] , dis2[10001] ;
int visited[10001] ;
int src ,des ,mx;
void reset(int n)
{
for(int i=0 ; i<=n ; i++)
{
G[i].clear() ;
visited[i] =0 ;
}
}
void bfs(int n , int dis[])
{
queue<int> q ;
visited[n] =1 ;
dis[n] =0 ;
q.push(n) ;
while(!q.empty())
{
int u = q.front() ;
q.pop() ;
for(int i=0 ; i<G[u].size() ; i++)
{
int v = G[u][i] ;
if(!visited[v])
{
visited[v] =1 ;
dis[v] = dis[u] +1 ;
q.push(v) ;
}
}
}
}
int main()
{
int test , cnt =1 ;
cin>>test ;
while(test--)
{
int node , edge ;
cin>>node>>edge ;
reset(node) ;
for(int i=0 ; i<edge ; i++)
{
int u ,v ;
cin>>u>>v ;
G[u].pb(v) ;
G[v].pb(u) ;
}
cin>>src>>des ;
mx = n_INF ;
memset(visited , 0 , sizeof(visited)) ;
bfs(src,dis1) ;
//mx = n_INF ;
memset(visited , 0 , sizeof(visited)) ;
bfs(des,dis2) ;
//mx = n_INF ;
for(int i=0 ; i<node ; i++)
{
mx = max(mx , dis1[i] + dis2[i]) ;
}
pf("Case %d: %d\n",cnt++,mx) ;
}
return 0;
}
Md. Namzul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size) for(int i=0 ; i<size ; i++) cin>>array[i] ;
#define new_line pf("\n")
#define highest_int 2147483647
#define n_INF -99999999
vector<int> G[10001] ;
int dis1[10001] , dis2[10001] ;
int visited[10001] ;
int src ,des ,mx;
void reset(int n)
{
for(int i=0 ; i<=n ; i++)
{
G[i].clear() ;
visited[i] =0 ;
}
}
void bfs(int n , int dis[])
{
queue<int> q ;
visited[n] =1 ;
dis[n] =0 ;
q.push(n) ;
while(!q.empty())
{
int u = q.front() ;
q.pop() ;
for(int i=0 ; i<G[u].size() ; i++)
{
int v = G[u][i] ;
if(!visited[v])
{
visited[v] =1 ;
dis[v] = dis[u] +1 ;
q.push(v) ;
}
}
}
}
int main()
{
int test , cnt =1 ;
cin>>test ;
while(test--)
{
int node , edge ;
cin>>node>>edge ;
reset(node) ;
for(int i=0 ; i<edge ; i++)
{
int u ,v ;
cin>>u>>v ;
G[u].pb(v) ;
G[v].pb(u) ;
}
cin>>src>>des ;
mx = n_INF ;
memset(visited , 0 , sizeof(visited)) ;
bfs(src,dis1) ;
//mx = n_INF ;
memset(visited , 0 , sizeof(visited)) ;
bfs(des,dis2) ;
//mx = n_INF ;
for(int i=0 ; i<node ; i++)
{
mx = max(mx , dis1[i] + dis2[i]) ;
}
pf("Case %d: %d\n",cnt++,mx) ;
}
return 0;
}
No comments:
Post a Comment