Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
You only care about distances to city . The product type does not affect how distance is computed.
All roads have length , so shortest paths from city can be found with one BFS. No Dijkstra needed; bringing a heap here is like using a flamethrower to light a candle.
After BFS, every city has a value : the shortest distance from the port to . Because the graph is undirected, this is also the shortest distance from to the port.
For each product type , scan all cities producing and keep the maximum .
The whole solution is: build graph, BFS from node , then compute ans[a[i]] = max(ans[a[i]], dist[i]). Complexity: .
Key Idea
The port is fixed: city . For every product type, we need the farthest city of that type, where distance means shortest path distance to city .
That wording can make the problem sound like we need to run shortest paths separately for each product type. Nope. That would be unnecessary and potentially too slow. The graph distance from a city to the port does not depend on the product type. Products are just labels slapped onto cities after distances are known.
So we compute all distances to city once.
Why BFS?
Every road has length exactly . In an unweighted graph, BFS from a source gives the shortest distance from that source to every node.
We run BFS starting from city and get:
shortest distance from city to city .
The graph is undirected, so the shortest distance from to is the same as from to . Nice and boring, which is exactly what we want.
Computing The Answers
Let be the product type produced by city .
After BFS, scan all cities:
That means for each product type, we keep the largest distance among all cities producing it.
Since every product type appears at least once, every answer will be filled naturally. Initializing answers to is fine because distances are never negative.
False Assumptions To Kill Immediately
Correctness Proof
We prove the algorithm outputs the required value for every product type.
First, BFS from city computes the shortest number of edges from city to every city , because the graph is unweighted and BFS explores nodes in increasing distance order.
Since the graph is undirected, any path from to can be traversed backward as a path from to with the same length. Therefore, is also the shortest distance from city to the port.
For a product type , the required answer is the maximum shortest distance to the port among all cities with . The algorithm scans every city exactly once and updates with for each such city. Therefore, after the scan, is exactly the maximum required distance for product type .
Thus, the algorithm is correct.
Complexity
Building the graph takes memory and time.
BFS takes time.
The final scan takes time.
Total complexity:
Memory complexity:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int n, m, k;
cin >> n >> m >> k;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) cin >> a[i];
vector<vector<int>> adj(n + 1);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<int> dist(n + 1, -1);
queue<int> q;
dist[1] = 0;
q.push(1);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v : adj[u]) {
if (dist[v] == -1) {
dist[v] = dist[u] + 1;
q.push(v);
}
}
}
vector<int> ans(k + 1, 0);
for (int i = 1; i <= n; i++) {
ans[a[i]] = max(ans[a[i]], dist[i]);
}
for (int c = 1; c <= k; c++) {
if (c > 1) cout << ' ';
cout << ans[c];
}
cout << '\n';
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int n, m, k;
cin >> n >> m >> k;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) cin >> a[i];
vector<vector<int>> adj(n + 1);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<int> dist(n + 1, -1);
queue<int> q;
dist[1] = 0;
q.push(1);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v : adj[u]) {
if (dist[v] == -1) {
dist[v] = dist[u] + 1;
q.push(v);
}
}
}
vector<int> ans(k + 1, 0);
for (int i = 1; i <= n; i++) {
ans[a[i]] = max(ans[a[i]], dist[i]);
}
for (int c = 1; c <= k; c++) {
if (c > 1) cout << ' ';
cout << ans[c];
}
cout << '\n';
}