I am working on this problem : http://www.usaco.org/index.php?page=viewproblem2&cpid=944
My code works on the first 6 test cases but it times out on the last 4. Can someone show me how to make this code more efficient?
Strategy: I am using a DFS to find all the cows in a group. And I store the cow numbers of all the cows in the group. Then I calculate the perimeter. Also, to make this more efficient I remove all the cows that I visited in the DFS because a DFS on the other cows in the same system would result in the exact same perimeter.
Code:
const int MN = 1e5 + 10;
vector adj_list[MN];
bool visited[MN];
int N, M;
vector current;
void dfs(long node) {
visited[node] = true;
for (long u:adj_list[node]) {
if (!visited[u]) {
current.push_back(u);
dfs(u);
}
}
}
int main() {
freopen(“fenceplan.in”, “r”, stdin);
freopen(“fenceplan.out”, “w”, stdout);
cin >> N >> M;
map<long, pair<long, long>> inputs;
for (int i = 0; i < N; i++) {
long x, y;
cin >> x >> y;
inputs.insert({i + 1, make_pair(x, y)});
}
for (int i = 0; i < M; i++) {
int cow1, cow2;
cin >> cow1 >> cow2;
adj_list[cow1].push_back(cow2);
adj_list[cow2].push_back(cow1);
}
vector<int> cows;
for (int i = 1; i <= N; i++) {
cows.push_back(i);
}
long ans = 1000000000000;
long x, y;
long minX = 100000000;
long miny = 1000000000;
long maxX = -1000000000;
long maxY = -1000000000;
while (cows.size() != 0) {
current.clear();
current.push_back(cows[0]);
dfs(cows[0]);
minX = 100000000;
miny = 1000000000;
maxX = -1000000000;
maxY = -1000000000;
for (int i = 0; i < current.size(); i++) {
cows.erase(std::remove(cows.begin(), cows.end(), current[i]), cows.end());
x = inputs[current[i]].first;
y = inputs[current[i]].second;
minX = min(x, minX);
miny = min(y, miny);
maxX = max(x, maxX);
maxY = max(y, maxY);
}
ans = min(ans, 2 * (maxX - minX) + 2 * (maxY - miny));
}
cout << ans;
return 0;
}
how can I make this faster?