이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//Smaug never desolated!!
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int MAXN = (int)100007;
const int block = 320;
const int infint = (int)1e8 + 3;
const int MOD = (int)1e9 + 7;
const ll inf = (ll)1e18;
int n, m, q, bad[MAXN], dp[MAXN], input[MAXN], prv[MAXN];
pair<int, int> DP[MAXN][block], tmp[block];
vector<int> in[MAXN];
bool cmp(pair<int, int> a, pair<int, int> b)
{
return a.second > b.second;
}
inline void merge(int a, int b)
{
/*int sz = 0;
vector<int> used;
for (int i = 0; i < block; i++)
tmp[i] = {-1, -1};
int ptr1 = 0, ptr2 = 0;
while(sz < block && (ptr1 < block || ptr2 < block))
{
while(ptr1 < block && DP[a][ptr1].first != -1 && prv[DP[a][ptr1].first])
ptr1++;
while(ptr2 < block && DP[b][ptr2].first != -1 && prv[DP[b][ptr2].first])
ptr2++;
pair<int, int> B = {-1, -1};
if(ptr2 != block && DP[b][ptr2].first != -1)
B = DP[b][ptr2], B.second++;
if(ptr1 != block && (ptr2 == block || cmp(DP[a][ptr1], B)))
{
tmp[sz] = DP[a][ptr1], ptr1++, sz++;
int idx = DP[a][ptr1].first;
if(idx != -1)
prv[idx] = 1, used.push_back(idx);
}
else
{
tmp[sz] = B, ptr2++, sz++;
int idx = B.first;
if(idx != -1)
prv[idx] = 1, used.push_back(idx);
}
}
for (int i = 0; i < block; i++)
DP[a][i] = tmp[i];
for (auto u : used)
prv[u] = 0;*/
for (int i = 0; i < block; i++)
tmp[i] = {-1, -1};
vector<pair<int, int> > all;
int ptr1 = 0, ptr2 = 0;
while(ptr1 < block || ptr2 < block)
{
pair<int, int> B = {-1, -1};
if(ptr2 != block && DP[b][ptr2].first != -1)
B = DP[b][ptr2], B.second++;
if(ptr1 < block && (ptr2 == block || cmp(DP[a][ptr1], B)))
all.push_back(DP[a][ptr1++]);
else
all.push_back(B), ptr2++;
}
int sz = 0;
for (auto u : all)
if(u.first != -1 && !prv[u.first])
tmp[sz++] = u, prv[u.first] = 1;
for (auto u : all)
if(u.first != -1)
prv[u.first] = 0;
for (int i = 0; i < block; i++)
DP[a][i] = tmp[i];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> m >> q;
for (int i = 0; i < m; i++)
{
int u, v;
cin >> u >> v;
in[v].push_back(u);
}
for (int i = 1; i <= n; i++)
for (int j = 0; j < block; j++)
DP[i][j] = {-1, -1};
DP[1][0] = {1, 0};
for (int i = 2; i <= n; i++)
{
DP[i][0] = {i, 0};
for (auto u : in[i])
merge(i, u);
}
for (int i = 0; i < q; i++)
{
int v, t;
cin >> v >> t;
for (int i = 0; i < t; i++)
{
cin >> input[i];
bad[input[i]] = 1;
}
if(t >= block)
{
memset(dp, -1, sizeof dp);
dp[v] = 0;
for (int i = v; i >= 1; i--)
if(dp[i] != -1)
for (auto u : in[i])
dp[u] = max(dp[u], dp[i] + 1);
int ans = -1;
for (int i = v; i >= 1; i--)
if(!bad[i])
ans = max(ans, dp[i]);
cout << ans << "\n";
}
else
{
int ans = -1;
for (int i = block - 1; i >= 0; i--)
if(DP[v][i].first != -1 && !bad[DP[v][i].first])
ans = DP[v][i].second;
cout << ans << "\n";
}
for (int i = 0; i < t; i++)
bad[input[i]] = 0;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |