# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
44397 | Bruteforceman | Bitaro’s Party (JOI18_bitaro) | C++11 | 2013 ms | 258552 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.
#include <bits/stdc++.h>
using namespace std;
struct pii {
int first, second;
pii (int first, int second) : first(first), second(second) {};
pii () {}
};
const int magic = 400; // sqrt(100000);
vector <pii> dp[100010];
vector <int> g[100010];
vector <int> grp[100010];
bool done[100010];
bool aux[100010];
vector <pii> merge(vector <pii> &a, vector <pii> &b) {
vector <pii> ans;
int l = 0;
int r = 0;
while(l < a.size() || r < b.size()) {
if(ans.size() == magic) break;
if(l == a.size()) {
if(aux[b[r].first] == false) ans.push_back(b[r]);
aux[b[r].first] = true;
++r;
}
else if (r == b.size()) {
if(aux[a[l].first] == false) ans.push_back(a[l]);
aux[a[l].first] = true;
++l;
}
else if (a[l].second < b[r].second) {
if(aux[b[r].first] == false) ans.push_back(b[r]);
aux[b[r].first] = true;
++r;
}
else {
if(aux[a[l].first] == false) ans.push_back(a[l]);
aux[a[l].first] = true;
++l;
}
}
for(auto i : ans) {
aux[i.first] = false;
}
return ans;
}
void dfs(int x) {
if(done[x]) return ;
done[x] = true;
dp[x].push_back(pii(x, 0));
for(auto i : g[x]) {
dfs(i);
vector <pii> v;
for(auto j : dp[i]) {
v.push_back(pii(j.first, j.second + 1));
}
dp[x] = merge(dp[x], v);
}
}
bool bad[100010];
int arr[100010];
int root;
const int inf = 1000000000;
void conquer(int x) {
if(arr[x] != -1) return ;
if(root == x) {
arr[x] = 0;
return ;
}
arr[x] = -inf;
for(auto i : grp[x]) {
conquer(i);
arr[x] = max(arr[x], 1 + arr[i]);
}
return ;
}
int main() {
int n, m, q;
scanf("%d %d %d", &n, &m, &q);
for(int i = 1; i <= m; i++) {
int p, q;
scanf("%d %d", &p, &q);
g[q].push_back(p);
grp[p].push_back(q);
}
for(int i = 1; i <= n; i++) {
dfs(i);
}
for(int i = 1; i <= q; i++) {
int t, y;
scanf("%d %d", &t, &y);
vector <int> v;
for(int j = 0; j < y; j++) {
int x;
scanf("%d", &x);
v.push_back(x);
bad[x] = true;
}
int ans = -1;
if(y < magic) {
for(auto j : dp[t]) {
if(!bad[j.first]) {
ans = j.second;
break;
}
}
} else {
memset(arr, -1, sizeof arr);
root = t;
for(int j = 1; j <= n; j++) {
conquer(j);
if(!bad[j]) ans = max(ans, arr[j]);
}
}
printf("%d\n", ans);
for(int j : v) {
bad[j] = false;
}
}
return 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... |