This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Challenge: Accepted
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#define ll long long
#define maxn 100005
#define pii pair<int, int>
#define ff first
#define ss second
using namespace std;
const int siz = 320;
vector<int> adj[maxn], from[maxn];
vector<pii> pnt[maxn], tmp;
inline bool cmp (pii a, pii b) {
return a.ss > b.ss;
}
int dp[maxn];
bool part[maxn], found[maxn];
void getdp(int n) {
for (int i = 1;i <= n;i++) dp[i] = 0;
for (int i = 1;i <= n;i++) {
for (int v:adj[i]) {
dp[v] = max(dp[v], (dp[i] == 0 ? (part[i] ? -1 : 0) : dp[i]) + 1);
}
}
}
int main() {
ios_base::sync_with_stdio(0);cin.tie(0);
int n, m, q;
cin >> n >> m >> q;
for (int i = 0;i < m;i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
from[b].push_back(a);
}
for (int i = 1;i <= n;i++) {
for (int v:from[i]) {
for (auto it:pnt[v]) tmp.push_back(make_pair(it.ff, it.ss + 1));
}
tmp.push_back(make_pair(i, 0));
sort(tmp.begin(), tmp.end(), cmp);
for (int j = 0;j < tmp.size();j++) {
if (!found[tmp[j].ff]) {
found[tmp[j].ff] = 1;
pnt[i].push_back(tmp[j]);
if (pnt[i].size() >= siz) break;
}
}
//cout << i << endl;
for (auto v:pnt[i]) found[v.ff] = 0;
tmp.clear();
}
getdp(n);
while (q--) {
int ind, yi;
cin >> ind >> yi;
vector<int> val;
for (int i = 0;i < yi;i++) {
int x;
cin >> x;
part[x] = 1;
val.push_back(x);
}
if (yi >= siz) {
getdp(n);
if (part[ind] && dp[ind] == 0) cout << -1 << "\n";
else cout << dp[ind] << "\n";
} else {
int ans = -1;
for (auto v:pnt[ind]) {
if (!part[v.ff]) ans = max(ans, v.ss);
}
cout << ans << "\n";
}
for (int i:val) part[i] = 0;
}
}
/*
5 6 3
1 2
2 4
3 4
1 3
3 5
4 5
4 1 1
5 2 2 3
2 3 1 4 5
*/
Compilation message (stderr)
bitaro.cpp: In function 'int main()':
bitaro.cpp:47:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for (int j = 0;j < tmp.size();j++) {
| ~~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |