https://vjudge.net/problem/UVA-10687
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
const int N=1e3+10;
const int inf=30;
int x[N],y[N];
bool flag[N];
vector<int> v[N];
void dfs(int u){
flag[u]=1;
for(auto vex:v[u]){
//printf("%d %d\n",u,vex) ;
if(flag[vex])continue;
dfs(vex);
}
}
inline int aa(int a){
return a*a;
}
int dist(int s,int t){
return aa(x[s]-x[t])+aa(y[s]-y[t]) ;
}
bool cmp(int i,int j,int k) {
if(dist(i,k)!=dist(j,k))return dist(i,k)<dist(j,k);
if(x[i]!=x[j])return x[i]<x[j];
return y[i]<y[j];
}
int main(){
int n;
while(scanf("%d",&n),n){
for(int i=0;i<n;i++)
scanf("%d%d",&x[i],&y[i]);
x[n]=y[n]=inf;
for(int i=0;i<n;i++)
v[i].clear();
for(int i=0;i<n;i++){
int s=n,t=n;
for(int j=0;j<n;j++){
if(j==i)continue;
if(!cmp(t,j,i))t=j;
if(!cmp(s,t,i))swap(s,t);
}
v[i].push_back(s);
v[i].push_back(t);
}
memset(flag,0,sizeof(flag));
int cnt=0;
dfs(0);
for(int i=0;i<n;i++)
if(flag[i])cnt++;
if(n==cnt)puts("All stations are reachable.");
else puts("There are stations that are unreachable.");
}
}