This is for the problem USACO
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
//#define int long
#define pii pair<int,int>
#define vi vector
#define mii map<int,int>
#define pqb priority_queue
#define pqs priority_queue<int,vi,greater>
#define MAXN 1005
#define mod 1000000007
#define inf 100000000000000000
#define ps(x,y) fixed<<setprecision(y)<<x
#define mk(arr,n,type) type *arr=new type[];
#define w(x) int x; cin>>x; while(x–)
const int MAXCOWS = 205;
using namespace std;
int numCows;
int pairwiseDistance[MAXCOWS][MAXCOWS];
int originalcow;
struct cow
{
int x;
int y;
double powersq;
int count;
};
bool visited[MAXCOWS];
cow cows[MAXCOWS];
void DFS(int node)
{
visited[node] = true;
for (int a = 0; a < numCows; a++)
{
if ((pairwiseDistance[node][a] <= cows[node].powersq) && !visited[a])
{
++cows[originalcow].count;
DFS(a);
}
}
}
int main()
{
freopen(“moocast.in”, “r”, stdin);
freopen(“moocast.out”, “w”, stdout);
cin >> numCows;
for (int i = 0; i < numCows; i++)
{
cin >> cows[i].x;
cin >> cows[i].y;
double dist;
cin >> dist;
cows[i].powersq = dist * dist;// cout << "cow " << i << " power " << cows[i].power;
cows[i].count = 0;
}
for(int i = 0; i < numCows; i++)
{
for (int j = 0; j < numCows; j++)
{
double xdiff = cows[i].x - cows[j].x;
double ydiff = cows[i].y - cows[j].y;
double distsq = xdiff * xdiff + ydiff * ydiff;
pairwiseDistance[i][j] = distsq;
pairwiseDistance[j][i] = distsq;
}
}
int maxcows = 0;
for (int i = 0; i < numCows; i++)
{
cows[i].count = 0;
}
for (int i = 0; i < numCows; i++)
{
for (int j = 0; j < numCows; j++)
{
visited[i] = false;
}
originalcow = i;
++cows[originalcow].count;
DFS(i);
}
for (int i = 0; i < numCows; i++)
{
maxcows = max(maxcows, cows[i].count);
}
cout << maxcows;
}
It works for Test Cases 1, 5, 6, and 10, but not the others and I’m not sure why.