이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <algorithm>
using ll = long long;
int const nmax = 100000;
int const threshold = 360;
std::vector<int> g[5 + nmax];
int cap[5 + nmax];
std::pair<int, int> dp[5 + nmax][5 + threshold];
std::pair<int,int> aux[5 + threshold * 2];
namespace lowmap{
int frec[5 + nmax];
int timestamp = 1;
void reset() {
++timestamp;
}
bool find(int val) {
return frec[val] == timestamp;
}
void insert(int val) {
frec[val] = timestamp;
}
};
int dpBrut[5 + nmax];
int brute(int target) {
for(int i = 1;i <= target; i++) {
if(lowmap::find(i) == 0)
dpBrut[i] = 1;
else
dpBrut[i] = 0;
for(int h = 0; h < g[i].size(); h++) {
int to = g[i][h];
if(0 < dpBrut[to])
dpBrut[i] = std::max(dpBrut[i], dpBrut[to] + 1);
}
}
return dpBrut[target] - 1;
}
int main() {
std::ios::sync_with_stdio(0);
std::cin.tie(0);
int n, m, q;
std::cin >> n >> m >> q;
for(int i = 1;i <= m; i++) {
int x, y;
std::cin >> x >> y;
g[y].push_back(x);
}
for(int i = 1;i <= n; i++) {
cap[i] = 1;
dp[i][1] = {0, i};
for(int h = 0; h < g[i].size(); h++) {
int to = g[i][h];
std::merge(dp[i] + 1, dp[i] + cap[i] + 1,
dp[to] + 1, dp[to] + cap[to] + 1,
aux + 1, std::greater<std::pair<int,int>>() );
lowmap::reset();
int lim = cap[i] + cap[to];
cap[i] = 0;
for(int j = 1;j <= lim && cap[i] < threshold; j++) {
if(lowmap::find(aux[j].second) == 0) {
lowmap::insert(aux[j].second);
dp[i][++cap[i]] = aux[j];
}
}
}
for(int j = 1;j <= cap[i]; j++)
dp[i][j].first++;
}
for(int i = 1;i <= q; i++) {
int x, k;
std::cin >> x >> k;
lowmap::reset();
for(int j = 1;j <= k; j++) {
int val;
std::cin >> val;
lowmap::insert(val);
}
if(k < threshold) {
bool solved = false;
for(int j = 1; j <= cap[x]; j++)
if(lowmap::find(dp[x][j].second) == 0) {
std::cout << dp[x][j].first -1 << '\n';
solved = true;
break;
}
if(solved == false)
std::cout << -1 << '\n';
} else {
std::cout << brute(x) << '\n';
}
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
bitaro.cpp: In function 'int brute(int)':
bitaro.cpp:37:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for(int h = 0; h < g[i].size(); h++) {
| ~~^~~~~~~~~~~~~
bitaro.cpp: In function 'int main()':
bitaro.cpp:60:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for(int h = 0; h < g[i].size(); h++) {
| ~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |