Tanisha25

Untitled

Oct 7th, 2023
1,101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.15 KB | None | 0 0
  1. //Tanisha Pareek.
  2. // a=97, z=122, A=65, Z=90, '0'-48, '9'-57
  3.  
  4. #include <bits/stdc++.h>
  5. #include<ext/pb_ds/assoc_container.hpp>
  6. #include<ext/pb_ds/tree_policy.hpp>
  7.  
  8. using namespace __gnu_pbds;
  9. using namespace std;
  10.  
  11. #define pb push_back
  12.  
  13. typedef long long ll;
  14. typedef vector<ll> vll;
  15. typedef tree<ll, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> pbds; // find_by_order(k)--(returns iterator of ele. at index k), order_of_key(k)--(returns index of k if k was present in set.)
  16. typedef tree<ll, null_type, less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> multipbds;
  17.  
  18. //--CONSTANTS--
  19. const ll mod = 1e9 + 7;
  20.  
  21. //--USEFUL FUNCTIONS--
  22. bool    isPalindrome(string str){ll low = 0,high = str.length() - 1;while (low < high){if (str[low] != str[high])return false;low++,high--;}return true;}
  23. bool    isPrime(ll n){for(ll i=2;(i*i)<=n;i++){if((n%i) == 0) return false;}return true;}
  24. ll      highestSmallerPower(ll num, ll p){ll cnt=0,curr=1;while((curr*p)<=num) curr*=p,cnt++;return cnt;} // gives largest power value(say x) such that p^x <= num
  25. ll      modulerExpo(ll num,ll p,ll m){ll ans=1;num%=m;while(p){if(p&1) ans*=num;ans%=m;p = p>>1;num*=num;num%=m;}return ans;} // (num^p)%m
  26. ll      phin(ll n){ll result = n;for(ll i=2;(i*i)<=n;i++){if(n%i == 0){while(n%i == 0) n/=i;result -= result/i;}}if(n>1) result -= (result/n);return result;}
  27. vll primes;
  28. void countPrimes(ll n)
  29. {
  30.     vector<bool> is_prime((n+1),true);
  31.     is_prime[0] = is_prime[1] = false;
  32.  
  33.     for(ll i=2;i<=n;i++)
  34.     {
  35.         if((is_prime[i])&&((i*i)<=n))
  36.         {
  37.             for(ll j=(i*i);j<=n;j+=i) is_prime[j]=false;
  38.         }
  39.     }
  40.  
  41.     for(ll i=1;i<=n;i++)
  42.     {
  43.         if(is_prime[i]) primes.pb(i);
  44.     }
  45.     return;
  46. }
  47. ll inverse(ll i, ll m){
  48.     if(i==1) return 1;
  49.     return (m - ((m/i)*inverse(m%i,m))%m+m)%m;
  50. }
  51.  
  52. // ---- Solution ----
  53. void bfs(ll source, vector<vll> &shortestPath, vector<vll> &graph, ll n)
  54. {
  55.     queue<ll> q;
  56.     q.push(source);
  57.     vll vis(n+1,0);
  58.     shortestPath[source][source] = 0;
  59.     vis[source] = 1;
  60.  
  61.     while(!q.empty())
  62.     {
  63.         ll x = q.front();
  64.         q.pop();
  65.  
  66.         for(auto j:graph[x])
  67.         {
  68.             if(vis[j] == 1) continue;
  69.             vis[j] = 1;
  70.             q.push(j);
  71.             shortestPath[source][j] = shortestPath[source][x] + 1;
  72.         }
  73.     }
  74. }
  75.  
  76. int main()
  77. {
  78.     #ifndef ONLINE_JUDGE
  79.     freopen("input.txt", "r", stdin);
  80.     freopen("output.txt", "w", stdout);
  81.     #endif
  82.  
  83.     ios_base::sync_with_stdio(false);
  84.     cin.tie(0);
  85.     cout.tie(0);
  86.  
  87.     //Please read the question carefully again!
  88.  
  89.     ll n;
  90.     cin>>n;
  91.  
  92.     vector<vll> graph(n+1,vll());
  93.     vll isSelfLoop(n+1,0);
  94.     for(ll i=1;i<=n;i++)
  95.     {
  96.         ll x;
  97.         for(ll j=1;j<=n;j++)
  98.         {
  99.             cin>>x;
  100.             if(x == 1) graph[i].pb(j);
  101.             if((x==1)&&(j==i)) isSelfLoop[i]++;
  102.         }
  103.     }
  104.  
  105.     vector<vll> shortestPath(n+1,vll(n+1,1e6));
  106.  
  107.     for(ll i=1;i<=n;i++)
  108.     {
  109.         bfs(i,shortestPath,graph,n);
  110.         // for(ll j=1;j<=n;j++) cout<<shortestPath[i][j]<<" ";
  111.         // cout<<"\n";
  112.     }
  113.     for(ll i=1;i<=n;i++)
  114.     {
  115.         if(isSelfLoop[i] == 1) cout<<"1\n";
  116.         else
  117.         {
  118.             ll mini = LLONG_MAX;
  119.             for(ll j=1;j<=n;j++)
  120.             {
  121.                 if(j == i) continue;
  122.                 mini = min(mini, (shortestPath[i][j] + shortestPath[j][i]));
  123.             }
  124.            
  125.             if(mini >= 1e6) cout<<"NO WAY\n";
  126.             else cout<<mini<<"\n";
  127.         }
  128.     }
  129.     return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment