- #include <bits/stdc++.h>
- using namespace std;
- #define N 105
- #define dbg printf("comes here\n");
- vector<int> adj[N];
- int n, m;
- bool visited[N];
- int dis1[N], dis2[N];
- void clear(){
- for(int i = 0; i <=n; i++){
- adj[i].clear();
- dis1[i] = dis2[i] = INT_MAX;
- }
- }
- void bfs1(int source){
- for(int i = 0; i <n; i++) visited[i] = false;
- list<int>q;
- q.push_back(source);
- visited[source]= true;
- dis1[source] = 0;
- while(!q.empty()){
- int from = q.front();
- visited[from] = true;
- q.pop_front();
- for(int i = 0; i <adj[from].size(); i++){
- int to = adj[from][i];
- if(visited[to]) continue;
- visited[to] = true;
- q.push_back(to);
- dis1[to] = dis1[from] + 1;
- }
- }
- }
- void bfs2(int source){
- for(int i = 0; i <n; i++) visited[i] = false;
- list<int>q;
- q.push_back(source);
- visited[source]= true;
- dis2[source] = 0;
- while(!q.empty()){
- int from = q.front();
- q.pop_front();
- for(int i = 0; i <adj[from].size(); i++){
- int to = adj[from][i];
- if(visited[to]) continue;
- visited[to] = true;
- q.push_back(to);
- dis2[to] = dis2[from] + 1;
- }
- }
- }
- int main(){
- int ts;
- scanf("%d", &ts);
- for(int c = 1; c <= ts; c++){
- clear();
- scanf("%d%d", &n, &m);
- for(int i = 0; i <m; i++){
- int a, b;
- scanf("%d%d", &a, &b);
- //if(a == b) continue;
- adj[a].push_back(b);
- adj[b].push_back(a);
- }
- int s, t;
- scanf("%d%d", &s, &t);
- bfs1(s);
- bfs2(t);
- /*
- for(int i = 0; i <n; i++) cout<<dis1[i]<<" ";
- cout<<endl;
- for(int i = 0; i <n; i++) cout<<dis2[i]<<" ";
- cout<<endl;
- */
- int di = 0;
- for(int i = 0; i <n; i++) di = max(di,dis1[i] + dis2[i]);
- printf("Case %d: %d\n", c, di);
- }
- return 0;
- }
Friday 28 July 2017
Lightoj 1174 - Commandos
Subscribe to:
Post Comments (Atom)
Most Featured Post
Lightoj 1159 - Batman
http://lightoj.com/volume_showproblem.php?problem=1159 problem analysis: First i thought of this as if s1, s2 and s3 are those three str...
-
Problem link: Problem Analysis: It is actually a basic Bisection problem , as we can see here we can not actually find a formula fo...
-
http://lightoj.com/volume_showproblem.php?problem=1382 Problem analysis: This is a rare problem i wrote about so far. After much strugg...
No comments:
Post a Comment