# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1265406 | quangminh412 | Long Mansion (JOI17_long_mansion) | C++17 | 156 ms | 69808 KiB |
/*
Ben Watson
Quang Minh MasterDDDDD
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const string name = "test";
void solve();
signed main()
{
if (fopen((name + ".inp").c_str(), "r"))
{
freopen((name + ".inp").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
solve();
return 0;
}
// main program
const int maxn = 5001;
vector<int> keys[maxn];
int c[maxn], can[maxn][maxn];
int n, q;
void compute(int st)
{
vector<int> cur(n + 1, 0), vis(n + 1, 0);
vis[st] = 1;
for (int a : keys[st])
cur[a] = 1;
int l = st, r = st;
while (1)
{
bool proceed = false;
if (l != 1)
{
int need = c[l - 1];
if (cur[need])
{
l--; vis[l] = 1; proceed = true;
for (int a : keys[l])
cur[a] = 1;
}
}
if (r != n)
{
int need = c[r];
if (cur[need])
{
r++; vis[r] = 1; proceed = true;
for (int a : keys[r])
cur[a] = 1;
}
}
if (l != 1)
{
int need = c[l - 1];
if (cur[need])
{
l--; vis[l] = 1; proceed = true;
for (int a : keys[l])
cur[a] = 1;
}
}
if (r != n)
{
int need = c[r];
if (cur[need])
{
r++; vis[r] = 1; proceed = true;
for (int a : keys[r])
cur[a] = 1;
}
}
if (!proceed)
break;
}
for (int i = 1; i <= n; i++)
if (vis[i] == 1)
can[st][i] = 1;
}
void solve()
{
cin >> n;
for (int i = 1; i < n; i++)
cin >> c[i];
for (int i = 1; i <= n; i++)
{
int b; cin >> b;
for (int j = 0; j < b; j++)
{
int a; cin >> a;
keys[i].push_back(a);
}
}
for (int i = 1; i <= n; i++)
compute(i);
cin >> q;
while (q--)
{
int x, y; cin >> x >> y;
cout << (can[x][y] == 1 ? "YES\n" : "NO\n");
}
}
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... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |