QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#807622 | #4000. Dynamic Reachability | user10086 | TL | 2ms | 11900kb | C++23 | 3.0kb | 2024-12-10 09:32:16 | 2024-12-10 09:32:17 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, B = 64, K = N / B + 10;
struct Q1
{
int t, k;
};
struct Q2
{
int id, t, u, v;
};
int n, m, q, fl[N], used[N], ans[N], idx[N], vis[N];
array<int, 2> el[N];
vector<Q2> q2[K];
int sz[K];
vector<Q1> q1[K];
vector<int> g[N], g2[N];
bitset<B * 3> from[N], to[N];
int tick, sccn, dfn[N], low[N], col[N], instk[N];
vector<int> stk;
void tarjan(int x)
{
tick++, dfn[x] = low[x] = tick;
stk.push_back(x), instk[x] = true;
for (int y : g[x])
{
if (!dfn[y]) tarjan(y), low[x] = min(low[x], low[y]);
else if (instk[y]) low[x] = min(low[x], dfn[y]);
}
if (low[x] == dfn[x])
{
sccn++;
int u;
do
{
u = stk.back(); stk.pop_back(); instk[u] = false;
col[u] = sccn;
to[sccn].set(idx[u]), from[sccn].set(idx[u]);
} while (u != x);
}
}
int ind[N];
void pre()
{
queue<int> q;
vector<int> topo;
for (int i = 1; i <= sccn; i++)
if (!ind[i]) q.push(i);
while (!q.empty())
{
int k = q.front(); q.pop();
topo.push_back(k);
for (int x : g2[k])
{
ind[x]--;
if (!ind[x]) q.push(x);
}
}
reverse(topo.begin(), topo.end());
for (int x : topo)
for (int y : g2[x])
to[x] |= to[y];
}
int ver;
bool dfs(int x, int y, const vector<array<int, 2>>& tmp)
{
vis[x] = ver;
if (to[x][idx[y]]) return true;
for (auto e : tmp)
{
if (to[x][idx[e[0]]] && vis[e[1]] != ver)
if (dfs(e[1], y, tmp)) return true;
}
return false;
}
signed main()
{
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m >> q;
for (int i = 1; i <= m; i++) cin >> el[i][0] >> el[i][1], fl[i] = true;
int k = q / B, cnt2 = 0;
for (int i = 1; i <= q; i++)
{
int op, k, u, v; cin >> op;
if (op == 1) cin >> k, q1[i / B].push_back({i, k});
if (op == 2) cin >> u >> v, cnt2++, q2[i / B].push_back({cnt2, i, u, v});
}
for (int i = 0; i <= k; i++)
{
set<int> qp;
for (Q1 q : q1[i]) qp.insert(el[q.k][0]), qp.insert(el[q.k][1]);
for (Q2 q : q2[i]) qp.insert(q.u), qp.insert(q.v);
int tmp = 0; for (int x : qp) tmp++, idx[x] = tmp;
for (int i = 1; i <= n; i++) g[i].clear();
for (Q1 q : q1[i]) used[q.k] = true;
for (int i = 1; i <= m; i++)
if (!used[i]) g[el[i][0]].push_back(el[i][1]);
for (Q1 q : q1[i]) used[q.k] = false;
tick = sccn = 0; memset(dfn, 0, sizeof dfn);
for (int i = 1; i <= n; i++)
if (!dfn[i]) tarjan(i);
for (int i = 1; i <= sccn; i++)
for (int j : g[i])
if (col[i] != col[j]) g2[col[i]].push_back(col[j]), ind[col[j]]++;
pre();
for (Q2 q : q2[i])
{
int t = q.t, u = q.u, v = q.v;
if (to[col[u]][idx[v]])
{
ans[q.id] = true;
continue;
}
vector<array<int, 2>> tmp;
for (Q1 q : q1[i])
{
if (q.t > t) break;
fl[q.k] ^= 1;
}
for (Q1 q : q1[i]) if (fl[q.k]) tmp.push_back({el[q.k][0], el[q.k][1]});
ver++, ans[q.id] = dfs(u, v, tmp);
}
}
for (int i = 1; i <= cnt2; i++) cout << (ans[i] ? "YES" : "NO") << '\n';
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 11900kb
input:
5 6 7 1 2 1 3 2 4 3 4 3 5 4 5 2 1 5 2 2 3 1 3 1 4 2 1 4 1 3 2 1 5
output:
YES NO NO YES
result:
ok 4 lines
Test #2:
score: -100
Time Limit Exceeded
input:
50000 100000 100000 36671 44121 25592 44321 13226 46463 13060 25694 14021 20087 22881 38333 34655 47774 22868 26462 31154 48710 27491 32365 5874 47497 17622 28600 1886 14193 22315 23656 14973 22704 1335 25384 22612 34915 2852 48213 23334 25519 24342 28784 6238 36125 14598 39494 33069 34250 2123 3059...