Language: C++

#include<bits/stdc++.h>
#define watch(x) cout<<(#x)<<" is "<<(x)<<endl
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define pb push_back
#define pq priority_queue
using namespace std;
using ll=long long;
using ld=long double;
using pll=pair<ll,ll>;
using pss=pair<string,string>;
using graph=vector<vector<int>>;
using wgraph=vector<vector<pll>>;

const ll MOD=1000000007;
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
const int ex[8]={1,1,0,-1,-1,-1,0,1};
const int ey[8]={0,1,1,1,0,-1,-1,-1};
const ld E=1e-10;

ll abs1(ll a){
  if(a>=0)return a;
  else return -a;
}

ll pow1(ll a,ll b,ll p=LLONG_MAX){
  ll ret=1;
  while(b!=0){
    if((b&1)==1){
      ret*=a;
    }
    a*=a;
    if(a>=p)a%=p;
    if(ret>=p)ret%=p;
    b=b>>1;
  }
  return ret;
}

int digit(ll a,ll b=10){
  int i=0;
  while(a!=0){
    a/=b;
    i++;
  }
  return i;
}

ll gcd1(ll a,ll b){
  if(a==0 && b==0)return 0;
  ll n;
  while(a%b!=0){
    n=a%b;
    a=b;
    b=n;
  }
  return b;
}

ll solveLinear(ll n,ll m,ll c){
  if(n==0 && m==0)return 0;
  ll a[100],b[100];
  a[0]=n;b[0]=m;
  int i=1;
  while(b[i-1]!=0){
    a[i]=b[i-1];
    b[i]=a[i-1]%b[i-1];
    i++;
  }
  n=i;
  if(c%a[n-1]!=0){
    return 0;
  }
  ll x=c/a[n-1],y=0;
  for(i=1;i<n;i++){
    ll cnt=x;
    x=y;
    y=cnt-(a[n-i-1]/b[n-i-1])*y;
  }
  return x;
}

ll modRev(ll a,ll p=MOD){
  a%=p;
  ll n=solveLinear(a,p,1);
  n%=p;
  if(n<0)return n+p;
  return n;
}

ll fact(ll n,ll p=MOD){
  if(n<0)return 0;
  int i;
  ll ret=1;
  for(i=0;i<n;i++){
    ret=ret*(i+1);
    if(ret>=p)ret%=p;
  }
  return ret;
}

ll perm(ll n,ll k,ll p=MOD){
  if(n<0 || k<0 || n<k)return 0;
  int i;
  ll ret=1;
  for(i=0;i<k;i++){
    ret=ret*(n-i);
    if(ret>=p)ret%=p;
  }
  return ret;
}

ll binom(ll n,ll k,ll p=MOD){
  if(n<0 || k<0 || n<k)return 0;
  int i;
  ll ret=1,fact=1;
  for(i=0;i<k;i++){
    ret=ret*(n-i);
    fact*=i+1;
    if(ret>=p)ret%=p;
    if(fact>=p)fact%=p;
  }
  return (ret*modRev(fact))%p;
}

void pushHeap(vector<ll>& h,int n){
  int i=h.size();
  h.push_back(n);
  while(i!=0 && h[(i-1)/2]<h[i]){
    swap(h[i],h[(i-1)/2]);
    i=(i-1)/2;
  }
  h[i]=n;
}

ll popHeap(vector<ll>& h){
  int n=h.size()-1;
  if(n==-1)return 0;
  ll i,ret=h[0];
  swap(h[0],h[n]);
  h.pop_back();
  for(i=0;i<n;){
    int j=2*i+1;
    if(j>=n)break;
    if(j!=n-1 && h[j]<h[j+1])j++;
    if(h[i]<h[j]){
      swap(h[i],h[j]);
    }
    i=j;
  }
  return ret;
}

int bis(vector<int> v,int key){
  int left=-1,right=v.size();
  while(abs1(right-left)>1){
    int mid=left+(right-left)/2;
    if(v[mid]<=key)left=mid;
    else right=mid;
  }
  return left;
}

vector<bool> ver;
vector<int> color;
void dfs(const graph& g,int v,int c){
  ver[v]=1;
  for(auto i:g[v]){
    if(color[i]==-1)color[i]=1-c;
    if(color[i]==c){
      cout<<-1<<endl;
      exit(0);
    }
    if(ver[i])continue;
    dfs(g,i,1-c);
  }
}

int main(){
  int i,j,k,l,cnt=0;
  int n;
  cin>>n;
  graph g(n);
  for(i=0;i<n;i++){
    for(j=0;j<n;j++){
      char x;
      cin>>x;
      if(x=='1'){
        g[i].pb(j);
        g[j].pb(i);
      }
    }
  }
  ver.assign(n,0);
  color.assign(n,-1);
  color[0]=0;
  dfs(g,0,0);
  
  queue<int> q,d;
  vector<ll> dist(n,INT_MAX);
  dist[0]=0;
  ver.assign(n,0);
  q.push(0);d.push(0);
  while(!q.empty()){
    int v=q.front(),p=d.front();
    q.pop();d.pop();
    if(ver[v])continue;
    ver[v]=1;
    for(auto i:g[v]){
      if(dist[i]<=p+1)continue;
      dist[i]=min(dist[i],p+1);
      q.push(i);d.push(p+1);
    }
  }
  
  ll mx=0;
  for(i=0;i<n;i++){
    if(mx<dist[i]){
      mx=dist[i];
      cnt=i;
    }
  }
  dist.assign(n,INT_MAX);
  dist[cnt]=0;
  ver.assign(n,0);
  q.push(cnt);d.push(0);
  while(!q.empty()){
    int v=q.front(),p=d.front();
    q.pop();d.pop();
    if(ver[v])continue;
    ver[v]=1;
    for(auto i:g[v]){
      if(dist[i]<=p+1)continue;
      dist[i]=min(dist[i],p+1);
      q.push(i);d.push(p+1);
    }
  }
  mx=0;
  for(i=0;i<n;i++){
    mx=max(mx,dist[i]);
  }
  cout<<mx+1<<endl;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:213:18: error: no matching function for call to 'min(__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type&, int)'
  213 |       dist[i]=min(dist[i],p+1);
      |               ~~~^~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:213:18: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
  213 |       dist[i]=min(dist[i],p+1);
      |               ~~~^~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:213:18: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
  213 |       dist[i]=min(dist[i],p+1);
      |               ~~~^~~~~~~~~~~~~
a.cc:236:18: error: no matching function for call to 'min(__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type&, int)'
  236 |       dist[i]=min(dist[i],p+1);
      |               ~~~^~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:236:18: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
  236 |       dist[i]=min(dist[i],p+1);
      |               ~~~^~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:236:18: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
  236 |       dist[i]=min(dist[i],p+1);
      |               ~~~^~~~~~~~~~~~~