#include "parks.h"
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> adj;
vector<int> visited;
int DFS(int index)
{
int sz = 1;
visited[index] = 1;
for (auto node : adj[index])
{
if (visited[node]) continue;
sz += DFS(node);
}
return sz;
}
int construct_roads(vector<int> x, vector<int> y)
{
if (x.size() == 1)
{
build({}, {}, {}, {});
return 1;
}
// x[i] = 2
const int N = x.size();
vector<pair<int, int>> l, r;
for (int i = 0; i < N; i++)
{
if (x[i] == 2)
l.push_back({y[i], i});
else
r.push_back({y[i], i});
}
sort(l.begin(), l.end());
sort(r.begin(), r.end());
adj = vector<vector<int>>(N);
vector<int> U, V, A, B;
for (int i = 0; i < l.size() - 1; i++)
{
if (l[i].first + 2 != l[i+1].first) continue;
int a = l[i].second;
int b = l[i+1].second;
adj[a].push_back(b);
adj[b].push_back(a);
U.push_back(a);
V.push_back(b);
A.push_back(1);
B.push_back(l[i].first + 1);
}
for (int i = 0; i < r.size() - 1; i++)
{
if (r[i].first + 2 != r[i+1].first) continue;
int a = r[i].second;
int b = r[i+1].second;
adj[a].push_back(b);
adj[b].push_back(a);
U.push_back(a);
V.push_back(b);
A.push_back(5);
B.push_back(r[i].first + 1);
}
l.insert(l.end(), r.begin(), r.end());
sort(l.begin(), l.end());
for (int i = 0; i < l.size() - 1; i++)
{
if (l[i].first != l[i+1].first) continue;
int a = l[i].second;
int b = l[i+1].second;
adj[a].push_back(b);
adj[b].push_back(a);
U.push_back(a);
V.push_back(b);
A.push_back(l[i].first + 1);
B.push_back(3);
}
visited = vector<int>(N);
int sz = DFS(0);
if (sz != N) return 0;
build(U, V, A, B);
return 1;
}
Compilation message
parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:44:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for (int i = 0; i < l.size() - 1; i++)
| ~~^~~~~~~~~~~~~~
parks.cpp:58:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (int i = 0; i < r.size() - 1; i++)
| ~~^~~~~~~~~~~~~~
parks.cpp:75:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for (int i = 0; i < l.size() - 1; i++)
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |