이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <tuple>
#include <cmath>
#include <string>
#include <cstring>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <random>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <algorithm>
#include <vector>
#include <fstream>
#include <iomanip>
#include <ctime>
#include <cctype>
#include <climits>
#include <chrono>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector< vector <int> > vvi;
typedef pair<int, int> pii;
typedef pair < pair < int, int >, int > piii;
typedef pair < pair <int, int > , pair <int, int> > piiii;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
#define FOR(i,a,b) for(int i = a; i < b; i ++)
#define RFOR(i,a,b) for(int i = a-1; i >= b; i --)
#define all(a) a.begin(), a.end()
#define endl '\n';
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
template <typename T>
void pr(vector<T> &v) {
FOR(i, 0, sz(v)) cout << v[i] << " ";
cout << endl;
}
template <typename T>
void pr(vector<vector<T> > &v) {
FOR(i, 0, sz(v)) { pr(v[i]); }
}
template <typename T>
void re(T &x) {
cin >> x;
}
template <typename T>
void re(vector<T> &a) {
FOR(i, 0, sz(a)) re(a[i]);
}
template <class Arg, class... Args>
void re(Arg &first, Args &... rest) {
re(first);
re(rest...);
}
template <typename T>
void pr(T x) {
cout << x << endl;
}
template <class Arg, class... Args>
void pr(const Arg &first, const Args &... rest) {
cout << first << " ";
pr(rest...);
cout << endl;
}
void ps() { cout << endl; }
template<class T, class... Ts>
void ps(const T& t, const Ts&... ts) {
cout << t; if (sizeof...(ts)) cout << " "; ps(ts...);
}
const ll MOD = 1000000007;
#define inf 1e18;
#define INF INT_MAX
long double PI = 4*atan(1);
long double eps = 1e-16;
int n,m,q;
vvi adj(100005);
vvi rev(100005);
vector<vector<pii>> chain(100005);
vi bad(100005);
int BLOCK = 300;
void precomp() {
int seen[100005] = {};
FOR(i,0,n) {
int nb = rev[i].size();
vi ptr(nb);
priority_queue<pair<int, pii>> pq;
pq.push({0, {i, -1}});
FOR(j,0,rev[i].size()) {
int x = rev[i][j];
pq.push({chain[x][0].ff + 1, {chain[x][0].ss, j}});
}
FOR(j,0,BLOCK) {
if(pq.empty()) break;
pair<int, pii> c = pq.top();
pq.pop();
if(seen[c.ss.ff] == 0) {
chain[i].pb({c.ff, c.ss.ff});
seen[c.ss.ff] = 1;
}
int idx = c.ss.ss;
if(idx >= 0) {
ptr[idx]++;
if(ptr[idx] < chain[rev[i][idx]].size()) {
pii ch = chain[rev[i][idx]][ptr[idx]];
pq.push({ch.ff + 1, {ch.ss, idx}});
}
}
}
FOR(j,0,chain[i].size()) {
seen[chain[i][j].ss] = 0;
}
}
// FOR(i,0,n) {
// cout << i << ": ";
// for(auto x : chain[i]) {
// cout << "(" << x.ff << "," << x.ss << ") ";
// }
// cout << endl;
// }
}
void brute(int t) {
vi dp(t+1, -1e9);
queue<pii> q;
q.push({t,0});
while(!q.empty()) {
pii curr = q.front(); q.pop();
int u = curr.ff;
int d = curr.ss;
dp[u] = max(dp[u], d);
for(auto v : rev[u]) {
q.push({v, d + 1});
}
}
int ans = -1;
FOR(i,0,t+1) {
if(dp[i] >= 0 && bad[i] == 0) ans = max(ans, dp[i]);
}
cout << ans << endl;
}
void solve() {
int t, y; cin >> t >> y; t--;
vi busy(y);
FOR(i,0,y) {
cin >> busy[i];
busy[i]--;
bad[busy[i]] = 1;
}
if(y >= BLOCK) {
brute(t);
} else {
int ans = -1;
for(auto x : chain[t]) {
if(bad[x.ss] == 0) ans = max(ans, x.ff);
}
cout << ans << endl;
}
FOR(i,0,y) {
bad[busy[i]] = 0;
}
}
int main() {
auto start = chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);cin.tie(0);
//ofstream cout("output.txt");
//ifstream cin("input.txt");
#ifdef DEBUG
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
cin >> n >> m >> q;
FOR(i,0,m) {
int u,v; cin >> u >> v; u--; v--;
adj[u].pb(v);
rev[v].pb(u);
}
precomp();
FOR(i,0,q) {
solve();
}
// auto stop = chrono::high_resolution_clock::now();
// auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);
// cout << duration.count() << endl;
//cin.close();
//cout.close();
}
컴파일 시 표준 에러 (stderr) 메시지
bitaro.cpp: In function 'void precomp()':
bitaro.cpp:38:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | #define FOR(i,a,b) for(int i = a; i < b; i ++)
......
115 | FOR(j,0,rev[i].size()) {
| ~~~~~~~~~~~~~~~~~
bitaro.cpp:115:9: note: in expansion of macro 'FOR'
115 | FOR(j,0,rev[i].size()) {
| ^~~
bitaro.cpp:131:29: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
131 | if(ptr[idx] < chain[rev[i][idx]].size()) {
bitaro.cpp:38:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | #define FOR(i,a,b) for(int i = a; i < b; i ++)
......
138 | FOR(j,0,chain[i].size()) {
| ~~~~~~~~~~~~~~~~~~~
bitaro.cpp:138:9: note: in expansion of macro 'FOR'
138 | FOR(j,0,chain[i].size()) {
| ^~~
bitaro.cpp: In function 'int main()':
bitaro.cpp:199:10: warning: variable 'start' set but not used [-Wunused-but-set-variable]
199 | auto start = chrono::high_resolution_clock::now();
| ^~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |