Skip to content

Commit

Permalink
Update usaco-1159.mdx
Browse files Browse the repository at this point in the history
Resolve comments
  • Loading branch information
TheRickyZhang authored Dec 27, 2024
1 parent add61c7 commit 0bc9df1
Showing 1 changed file with 23 additions and 26 deletions.
49 changes: 23 additions & 26 deletions solutions/silver/usaco-1159.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@ author: Nathan Gong, Jesse Choe
using namespace std;

using ll = long long;
using vi = vector<int>;

void solve() {
ll n, m;
int n, m;
cin >> n >> m;

// Adjacency list
vector<vector<ll>> adj(n);
vector<vector<int>> adj(n);
for (int i = 0; i < m; ++i) {
ll u, v;
int u, v;
cin >> u >> v;
--u;
--v; // Convert to 0-based indexing
Expand All @@ -38,43 +37,43 @@ void solve() {
}

// Connected components parent and count
vector<ll> vis(n, -1);
ll cnt = 0;
vector<int> vis(n, -1);
int cnt = 0;

// Identify connected components using DFS
for (int i = 0; i < n; ++i) {
if (vis[i] != -1) continue;
// DFS using a stack
stack<ll> s;
stack<int> s;
s.push(i);
while (!s.empty()) {
ll u = s.top();
int u = s.top();
s.pop();
if (vis[u] != -1) continue;
vis[u] = cnt;
for (ll v : adj[u]) {
if (vis[v] == -1) { s.push(v); }
if (vis[v] == -1) s.push(v);
}
}
cnt++;
}

// Mapping each component to its nodes
vector<vector<ll>> components(cnt);
for (ll i = 0; i < n; ++i) { components[vis[i]].push_back(i); }
vector<vector<int>> components(cnt);
for (int i = 0; i < n; ++i) { components[vis[i]].push_back(i); }

// Components containing the start (0) and end (n-1) barns
vector<ll> start_component = components[vis[0]];
vector<ll> end_component = components[vis[n - 1]];
vector<int> start_component = components[vis[0]];
vector<int> end_component = components[vis[n - 1]];

// Initialize distance vectors with infinity
vector<ll> dist1(cnt, LLONG_MAX);
vector<ll> dist2(cnt, LLONG_MAX);
vector<int> dist1(cnt, INT_MAX);
vector<int> dist2(cnt, INT_MAX);

// Calculate minimum distances to start_component
ll a = 0;
for (ll i = 0; i < n; ++i) {
ll dist = abs(start_component[a] - i);
int a = 0;
for (int i = 0; i < n; ++i) {
int dist = abs(start_component[a] - i);
while (a < start_component.size() - 1 &&
abs(start_component[a + 1] - i) < dist) {
a++;
Expand All @@ -83,9 +82,9 @@ void solve() {
}

// Calculate minimum distances to end_component
ll b = 0;
for (ll i = 0; i < n; ++i) {
ll dist = abs(end_component[b] - i);
int b = 0;
for (int i = 0; i < n; ++i) {
int dist = abs(end_component[b] - i);
while (b < end_component.size() - 1 && abs(end_component[b + 1] - i) < dist) {
b++;
}
Expand All @@ -94,16 +93,14 @@ void solve() {

// Compute the result by finding the minimum sum of squared distances
ll res = LLONG_MAX;
for (ll i = 0; i < cnt; ++i) {
res = min(res, dist1[i] * dist1[i] + dist2[i] * dist2[i]);
for (int i = 0; i < cnt; ++i) {
res = min(res, 1LL * dist1[i] * dist1[i] + 1LL * dist2[i] * dist2[i]);
}
cout << res << '\n';
}

int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll t;
int t;
cin >> t;
for (int i = 0; i < t; ++i) { solve(); }
}
Expand Down

0 comments on commit 0bc9df1

Please sign in to comment.