이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 < (int)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 < (int)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);
}
if (r.size())
l.insert(l.end(), r.begin(), r.end());
sort(l.begin(), l.end());
for (int i = 0; i < N - 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(3);
B.push_back(l[i].first + 1);
}
visited = vector<int>(N);
int sz = DFS(0);
if (sz != N) return 0;
build(U, V, A, B);
return 1;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |