Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Tanisha Pareek.
- // a=97, z=122, A=65, Z=90, '0'-48, '9'-57
- #include <bits/stdc++.h>
- #include<ext/pb_ds/assoc_container.hpp>
- #include<ext/pb_ds/tree_policy.hpp>
- using namespace __gnu_pbds;
- using namespace std;
- #define pb push_back
- typedef long long ll;
- typedef vector<ll> vll;
- 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.)
- typedef tree<ll, null_type, less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> multipbds;
- //--CONSTANTS--
- const ll mod = 1e9 + 7;
- //--USEFUL FUNCTIONS--
- 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;}
- bool isPrime(ll n){for(ll i=2;(i*i)<=n;i++){if((n%i) == 0) return false;}return true;}
- 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
- 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
- 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;}
- vll primes;
- void countPrimes(ll n)
- {
- vector<bool> is_prime((n+1),true);
- is_prime[0] = is_prime[1] = false;
- for(ll i=2;i<=n;i++)
- {
- if((is_prime[i])&&((i*i)<=n))
- {
- for(ll j=(i*i);j<=n;j+=i) is_prime[j]=false;
- }
- }
- for(ll i=1;i<=n;i++)
- {
- if(is_prime[i]) primes.pb(i);
- }
- return;
- }
- ll inverse(ll i, ll m){
- if(i==1) return 1;
- return (m - ((m/i)*inverse(m%i,m))%m+m)%m;
- }
- // ---- Solution ----
- void bfs(ll source, vector<vll> &shortestPath, vector<vll> &graph, ll n)
- {
- queue<ll> q;
- q.push(source);
- vll vis(n+1,0);
- shortestPath[source][source] = 0;
- vis[source] = 1;
- while(!q.empty())
- {
- ll x = q.front();
- q.pop();
- for(auto j:graph[x])
- {
- if(vis[j] == 1) continue;
- vis[j] = 1;
- q.push(j);
- shortestPath[source][j] = shortestPath[source][x] + 1;
- }
- }
- }
- int main()
- {
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- cout.tie(0);
- //Please read the question carefully again!
- ll n;
- cin>>n;
- vector<vll> graph(n+1,vll());
- vll isSelfLoop(n+1,0);
- for(ll i=1;i<=n;i++)
- {
- ll x;
- for(ll j=1;j<=n;j++)
- {
- cin>>x;
- if(x == 1) graph[i].pb(j);
- if((x==1)&&(j==i)) isSelfLoop[i]++;
- }
- }
- vector<vll> shortestPath(n+1,vll(n+1,1e6));
- for(ll i=1;i<=n;i++)
- {
- bfs(i,shortestPath,graph,n);
- // for(ll j=1;j<=n;j++) cout<<shortestPath[i][j]<<" ";
- // cout<<"\n";
- }
- for(ll i=1;i<=n;i++)
- {
- if(isSelfLoop[i] == 1) cout<<"1\n";
- else
- {
- ll mini = LLONG_MAX;
- for(ll j=1;j<=n;j++)
- {
- if(j == i) continue;
- mini = min(mini, (shortestPath[i][j] + shortestPath[j][i]));
- }
- if(mini >= 1e6) cout<<"NO WAY\n";
- else cout<<mini<<"\n";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment