/*
Shahadat Hossain
I.C.T Department
Comilla University
Session: 2013-2014
.
Problem Site: LightOj
Problem No: 1174
Problem Name: Commandos
Using BFS
*/
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<algorithm>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<iostream>
#include<fstream>
#include<numeric>
#include<string>
#include<vector>
#include<cstring>
#include<map>
#include<iterator>
#define valid(x,y) x>=0 && y>=0 && x<row && y<col
int fx[]= {1,-1,0,0};
int fy[]={0,0,1,-1};
#define pb push_back
#define mp make_pair
#define fs first
#define se second
#define pi 2*acos(0)
#define PI 3.14159265358979323846264338
#define modulo 1000000007
#define SMA 300010
#define MAX 500
#define input freopen("input.txt", "r", stdin)
#define output freopen("output.txt", "w", stdout)
#define ms(a,b) memset(a, b, sizeof(a))
#define sc(n) scanf("%d",&n)
#define loop(i,n) for(int i = 0; i<n; i++)
#define nloop(i,n) for(int i = 1; i<=n; i++)
#define prln(n) printf("%d\n",n)
#define prsp(n) printf("%d ",n)
#define pr(n) printf("%d",n)
using namespace std;
typedef long long ll;
typedef pair<double, double> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
vector<int> edge[110];
int visit[110], cost[110];
int bfs(int start, int end){
queue<int> sq;
sq.push(start);
while (!sq.empty()) {
int temp = sq.front();
visit[temp] = 1;
sq.pop();
if(temp == end){
return cost[temp];
}
for(int i = 0; i<edge[temp].size(); i++){
if(visit[edge[temp][i]] == 0){
visit[edge[temp][i]] = 1;
sq.push(edge[temp][i]);
cost[edge[temp][i]] = cost[temp]+1;
}
}
}
return 0;
}
int main(){
input;
int t,cas = 1;
cin>>t;
while (t--) {
int n;
cin>>n;
int path;
cin>>path;
for(int i = 0; i<=101; i++)
edge[i].clear();
for(int i = 0; i<path; i++){
int uu,vv;
cin>>uu>>vv;
edge[uu].push_back(vv);
edge[vv].push_back(uu);
}
int start, end;
cin>>start>>end;
int sum, mx = 0;
for(int i = 0; i<n; i++){
sum = 0;
ms(visit, 0);
ms(cost, 0);
sum += bfs(start, i);
ms(visit, 0);
ms(cost, 0);
sum += bfs(end, i);
if(mx < sum)
mx = sum;
}
printf("Case %d: %d\n", cas, mx);
cas++;
}
}
Comments
Post a Comment