# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1021680 | NoLove | Bitaro’s Party (JOI18_bitaro) | C++14 | 2100 ms | 12696 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
* author : Lăng Trọng Đạt
* created: 12-07-2024
**/
#include <bits/stdc++.h>
using namespace std;
#ifndef LANG_DAT
#define db(...) ;
#endif // LANG_DAT
#define int long long
#define mp make_pair
#define f first
#define se second
#define pb push_back
#define all(v) (v).begin(), (v).end()
using pii = pair<int, int>;
using vi = vector<int>;
#define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
void mx(int& a, int b) { if (b > a) a = b; }
void mi(int& a, int b) { if (b < a) a = b; }
#define si(x) (int)(x.size())
const int INF = 1e18;
const int MOD = 1e9 + 7;
const int MAXN = 1e5 + 5;
const int B = 300;
vector<int> adj[MAXN];
int g[MAXN], in[MAXN];
int n, m, q, a, b;
vector<pii> len[MAXN]; // {end node, longest length}
bool block[MAXN];
int dp[MAXN];
void dfs(int v, int type) {
for (int u : adj[v]) {
if (type == 1) {
dfs(u, type);
if (dp[u] != -1) mx(dp[v], dp[u] + 1);
} else if (dp[v] != -1) {
dfs(u, type);
}
}
if (!block[v]) mx(dp[v], 0);
if (type == 0) dp[v] = -1;
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
if (fopen("hi.inp", "r")) {
freopen("hi.inp", "r", stdin);
// freopen("hi.out", "w", stdout);
}
cin >> n >> m >> q;
FOR(i, 1, m) {
cin >> a >> b;
adj[b].push_back(a);
in[a]++;
}
vector<int> node;
FOR(i, 1, n) if (in[i] == 0) node.push_back(i);
vector<int> order;
while (si(node)) {
int v = node.back(); node.pop_back();
order.push_back(v);
for (int u : adj[v]) {
if (--in[u] == 0) {
node.push_back(u);
}
}
}
reverse(all(order));
vector<int> pos(n + 1, -1);
for (int v : order) {
len[v].push_back({v, 0});
for (int u : adj[v]) {
for (auto[a, b] : len[u]) {
if (pos[a] != -1) mx(len[v][pos[a]].se, b + 1);
else len[v].push_back({a, b + 1});
}
sort(all(len[v]), [](pii& a, pii& b) -> bool {return a.se > b.se;});
while (si(len[v]) > B) {pos[len[v].back().f] = -1; len[v].pop_back(); }
FOR(id, 0, si(len[v]) - 1) {
pos[len[v][id].f] = id;
}
}
FOR(id, 0, si(len[v]) - 1) {
pos[len[v][id].f] = -1;
}
}
// FOR(i, 1, n) {
// db(i, len[i]);
// }
memset(dp, -1, sizeof(dp));
FOR(i, 1, q) {
int source, t;
cin >> source >> t;
vector<int> c(t);
for (int& v : c) {
cin >> v;
block[v] = 1;
}
db(source, t, c)
int ans = -1;
if (t > B) {
dfs(source, 1);
ans = dp[source];
dfs(source, 0);
} else {
for (auto[u, l] : len[source]) {
if (!block[u]) {
ans = l;
break;
}
}
}
cout << ans << "\n";
for (int v : c) {
block[v] = 0;
}
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |