이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "fun.h"
#include <algorithm>
#include <iostream>
#include <cassert>
#include <numeric>
#include <vector>
#include <queue>
typedef long long llong;
const int MAXN = 100000 + 10;
const int INF = 1e9;
int n;
int sz[MAXN];
int pos[MAXN];
int type[MAXN];
int perm[MAXN];
int depth[MAXN];
bool isActive[MAXN];
std::vector <int> g[MAXN];
std::vector <int> t[MAXN];
int waitlistType = -1;
struct Element
{
int type;
int depth;
int node;
friend bool operator < (const Element &a, const Element & b)
{
return a.depth < b.depth || (a.depth == b.depth && a.node > b.node);
}
};
std::priority_queue <Element> pq;
std::queue <Element> q;
void calcSize(int node, int par)
{
sz[node] = 1;
for (const int &u : g[node])
{
if (u == par)
{
continue;
}
calcSize(u, node);
sz[node] += sz[u];
}
}
int findCentroid(int node, int par)
{
for (const int &u : g[node])
{
if (u != par && sz[u] > n / 2)
{
return findCentroid(u, node);
}
}
return node;
}
void assign(int node, int par, int t)
{
type[node] = t;
depth[node] = depth[par] + 1;
for (const int &u : g[node])
{
if (u == par)
{
continue;
}
assign(u, node, t);
}
}
std::vector <int> dfsThree(int node, int par)
{
std::vector <int> res;
res.push_back(node);
for (const int &u : t[node])
{
if (u == par)
{
continue;
}
if (t[u].size() == 1)
{
res.push_back(u);
}
}
for (const int &u : t[node])
{
if (u == par || t[u].size() == 1)
{
continue;
}
std::vector <int> curr = dfsThree(u, node);
std::reverse(curr.begin(), curr.end());
for (const int &v : curr)
{
res.push_back(v);
}
}
return res;
}
std::vector <int> createFunTour(int N, int Q)
{
n = N;
for (int i = 1 ; i <= n ; ++i)
{
for (int j = i + 1 ; j <= n ; ++j)
{
if (hoursRequired(i - 1, j - 1) == 1)
{
g[i].push_back(j);
g[j].push_back(i);
}
}
}
calcSize(1, 0);
int cntr = findCentroid(1, 0);
for (int idx = 0 ; idx < g[cntr].size() ; ++idx)
{
assign(g[cntr][idx], cntr, idx);
}
std::vector <int> answer;
for (int i = 1 ; i <= n ; ++i)
{
if (i == cntr)
{
continue;
}
pq.push({type[i], depth[i], i});
}
while (!pq.empty())
{
auto [t, d, node] = pq.top();
pq.pop();
if (waitlistType == t)
{
q.push({t, d, node});
continue;
}
if (waitlistType != -1)
{
answer.push_back(node);
answer.push_back(q.front().node);
q.pop();
if (q.empty())
{
waitlistType = -1;
}
continue;
}
if (answer.size() && type[answer.back()] == t)
{
q.push({t, d, node});
waitlistType = t;
continue;
}
answer.push_back(node);
}
if (q.size() && type[answer.back()] != q.front().type)
{
answer.push_back(q.front().node);
q.pop();
}
if (q.size())
{
q.push({q.front().type, 0, cntr});
int cntActive = q.size() + 2;
isActive[answer.back()] = true;
while (!q.empty())
{
isActive[q.front().node] = true;
q.pop();
}
for (int i = 1 ; i <= n ; ++i)
{
if (!isActive[i]) continue;
for (const int &u : g[i])
{
if (isActive[u])
{
t[i].push_back(u);
}
}
}
std::vector <int> toAdd = dfsThree(answer.back(), 0);
answer.pop_back();
for (const int &u : toAdd)
{
answer.push_back(u);
}
} else
{
answer.push_back(cntr);
}
for (int &u : answer)
{
u--;
}
return answer;
}
컴파일 시 표준 에러 (stderr) 메시지
fun.cpp: In function 'std::vector<int> createFunTour(int, int)':
fun.cpp:136:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
136 | for (int idx = 0 ; idx < g[cntr].size() ; ++idx)
| ~~~~^~~~~~~~~~~~~~~~
fun.cpp:196:13: warning: unused variable 'cntActive' [-Wunused-variable]
196 | int cntActive = q.size() + 2;
| ^~~~~~~~~
# | 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... |