Advertisement
Saleh127

Light OJ 1437 / Shortest Cycle

Mar 16th, 2022
680
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. /***
  2.  created: 2022-03-16-20.28.51
  3. ***/
  4.  
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. #define ll long long
  8. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  9. #define get_lost_idiot return 0
  10. #define nl '\n'
  11.  
  12. vector<ll>g[700];
  13.  
  14. int main()
  15. {
  16.     ios_base::sync_with_stdio(0);
  17.     cin.tie(0);
  18.     cout.tie(0);
  19.  
  20.  
  21.  
  22.     test
  23.     {
  24.         ll n,m,i,j,k,l,ans=INT_MAX;
  25.  
  26.  
  27.         cin>>n>>m;
  28.  
  29.         for(i=0;i<m;i++)
  30.         {
  31.             cin>>j>>k;
  32.  
  33.             g[j].push_back(k);
  34.             g[k].push_back(j);
  35.         }
  36.  
  37.         for(i=0;i<n;i++)
  38.         {
  39.             vector<ll>dis(n+2,INT_MAX);
  40.             vector<ll>par(n+2,-1);
  41.             queue<ll>q;
  42.  
  43.             dis[i]=0;
  44.             q.push(i);
  45.  
  46.             while(q.empty()==false)
  47.             {
  48.                 ll u=q.front();
  49.                 q.pop();
  50.  
  51.                 for(auto d:g[u])
  52.                 {
  53.                     if(dis[d]==INT_MAX)
  54.                     {
  55.                         dis[d]=1+dis[u];
  56.                         par[d]=u;
  57.                         q.push(d);
  58.                     }
  59.                     else if(par[d]!=u && par[u]!=d)
  60.                     {
  61.                         ans=min(ans,dis[u]+dis[d]+1);
  62.                     }
  63.                 }
  64.             }
  65.         }
  66.  
  67.         cout<<"Case "<<cs<<": ";
  68.  
  69.         if(ans==INT_MAX) cout<<"impossible"<<nl;
  70.         else cout<<ans<<nl;
  71.  
  72.         for(i=0;i<n+2;i++) g[i].clear();
  73.  
  74.     }
  75.  
  76.     get_lost_idiot;
  77. }
  78.  
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement