이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(0); cin.tie(0)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sz(a) (int) (a).size()
#define ll long long
#define ld long double
#define pb push_back
struct P{
ll x, y;
};
void dbg_out() { cerr << endl; }
template <typename H, typename... T>
void dbg_out(H h, T... t) { cerr << ' ' << h; dbg_out(t...); }
#define dbg(...) { cerr << #__VA_ARGS__ << ':'; dbg_out(__VA_ARGS__); }
const int mx=1e5+1;
const int k=100;
vector<int> g[mx];
vector<pair<int, int>> best[mx];
int blocked[mx];
signed main(){
fastio;
int n, m, q;
cin >> n >> m >> q;
for (int i=0; i<m; i++){
int a, b;
cin >> a >> b;
g[b].push_back(a);
}
stack<int> removed;
vector<pair<int, int>> cur;
for (int i=1; i<=n; i++){
cur.push_back({0, i});
for (auto u: g[i]){
for (const auto& [city, d]: best[u]){
cur.push_back({d+1, city});
}
}
sort(rall(cur));
for (const auto& [d, city]: cur){
if (blocked[city]) continue;
removed.push(city);
blocked[city] = 1;
best[i].push_back({city, d});
if (sz(best[i]) == k) break;
}
cur.clear();
while (!removed.empty()){
blocked[removed.top()] = 0;
removed.pop();
}
}
vector<int> dp (n+1);
while (q--){
int city, y;
cin >> city >> y;
while (y--){
int x; cin >> x;
removed.push(x);
blocked[x] = 1;
}
int ans=-1;
if (sz(removed) < k){
for (const auto& [b, d]: best[city]){
if (!blocked[b]){
ans = d;
break;
}
}
}
else{
for (int i=city; i; i--){
if (!dp[i] && i != city) continue;
for (auto u: g[i]) dp[u] = max(dp[u], dp[i]+1);
if (!blocked[i]) ans = max(ans, dp[i]);
}
for (int i=city; i; i--) dp[i] = 0;
}
cout << ans << '\n';
while (!removed.empty()){
blocked[removed.top()] = 0;
removed.pop();
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |