#include<bits/stdc++.h>
using namespace std;
int DistanceSum(int n, int x[], int y[]) {
int ans = 0;
map<pair<int, int>, int> mp;
for (int i = 0; i < n; i++) {
mp[{x[i], y[i]}] = i;
}
vector<vector<int>> adj(n);
for (auto c : mp) {
auto [i, j] = c.first;
if (mp.find({i+1, j}) != mp.end()) {
adj[c.second].push_back(mp[{i+1, j}]);
}
if (mp.find({i-1, j}) != mp.end()) {
adj[c.second].push_back(mp[{i-1, j}]);
}
if (mp.find({i, j+1}) != mp.end()) {
adj[c.second].push_back(mp[{i, j+1}]);
}
if (mp.find({i, j-1}) != mp.end()) {
adj[c.second].push_back(mp[{i, j-1}]);
}
}
for (int i = 0; i < n; i++) {
queue<int> q;
vector<int> dist(n, INT_MAX);
q.push(i);
dist[i] = 0;
while(!q.empty()) {
int a = q.front();
q.pop();
for (int b : adj[a]) {
if (dist[b] == INT_MAX) {
dist[b] = dist[a]+1;
q.push(b);
}
}
}
for (int j = 0; j < n; j++) {
ans += dist[j];
}
}
return ans/2;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
372 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
348 KB |
Output is correct |
2 |
Correct |
13 ms |
584 KB |
Output is correct |
3 |
Correct |
28 ms |
600 KB |
Output is correct |
4 |
Correct |
31 ms |
604 KB |
Output is correct |
5 |
Correct |
49 ms |
604 KB |
Output is correct |
6 |
Correct |
55 ms |
604 KB |
Output is correct |
7 |
Correct |
59 ms |
600 KB |
Output is correct |
8 |
Correct |
59 ms |
692 KB |
Output is correct |
9 |
Correct |
45 ms |
604 KB |
Output is correct |
10 |
Correct |
40 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1066 ms |
3164 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1031 ms |
3160 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |