QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#430772 | #6452. Ski Resort | 5ab | WA | 2ms | 9796kb | C++20 | 4.4kb | 2024-06-04 14:16:38 | 2024-06-04 14:16:38 |
Judging History
answer
/* name: lodge
* author: 5ab
* created at: 2024-06-04
*/
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define ssz(x) (int((x).size()))
auto chmax = [](auto& x, auto y) { if (x < y) x = y; };
auto chmin = [](auto& x, auto y) { if (y < x) x = y; };
using ll = long long;
const int N = 1e5, NN = 1e3, lgN = 17, EM = 50, M = N + EM, K = 4;
int hd[N], des[M], nxt[M], ord[N], ps[N], st[lgN][N], par[N], deg[N], dep[N], ec = 0, ind = 0;
ll f[N][K + 1];
vector<int> fp[N], tr[N], Tr[N];
bitset<NN> vis[NN];
bool gd[N], use_bitset;
void add(int s, int t)
{
des[ec] = t;
nxt[ec] = hd[s];
hd[s] = ec++;
deg[t]++;
}
void dfs0(int id, int fa)
{
ps[ord[id] = ind++] = id, st[0][ord[id]] = fa;
for (int x : tr[id])
{
dep[x] = dep[id] + 1;
dfs0(x, id);
}
}
void dfs(int id, int fa)
{
ps[ord[id] = ind++] = id, st[0][ord[id]] = fa;
par[id] = fa;
for (int p = hd[id], dst; p != -1; p = nxt[p])
{
dst = des[p];
if (ord[dst] == -1)
{
tr[id].push_back(dst);
dfs(dst, id);
}
else
fp[id].push_back(dst);
}
}
void dfs2(int id, int cx)
{
if (par[id] != -1 && ord[cx] < ord[par[id]])
par[id] = cx;
for (int x : tr[id])
dfs2(x, cx);
}
inline int cmp(int x, int y) {
return ord[x] < ord[y] ? x : y;
}
void lcainit(int n)
{
for (int l = 1, c = 2; c < n; c <<= 1, l++)
for (int i = l; i < n; i++)
st[l][i] = cmp(st[l - 1][i], st[l - 1][i - c / 2]);
}
inline int lca(int x, int y)
{
if (x == y)
return x;
x = ord[x], y = ord[y];
if (x > y)
swap(x, y);
int d = __lg(y - x);
return cmp(st[d][y], st[d][x + (1 << d)]);
}
bitset<NN> cn;
int k;
ll tmp[K + 1];
void dfs3(int id, int fa)
{
int cnt = 0;
if (use_bitset)
{
for (int x = id; x != fa; x = par[x])
{
cnt += !cn.test(x);
// cerr << (cn.test(x) ? -1 : x) << " ";
}
}
else
cnt = dep[id] - (fa == -1 ? -1 : dep[fa]);
// cerr << id << " " << fa << " " << cnt << endl;
fill(f[id], f[id] + k + 1, 0);
if (!gd[id])
{
for (int x : Tr[id])
dfs3(x, id);
f[id][0] = 1;
for (int x : Tr[id])
{
fill(tmp, tmp + k + 1, 0);
for (int i = 0; i <= k; i++)
for (int j = 0; j <= k - i; j++)
tmp[i + j] += f[id][i] * f[x][j];
copy(tmp, tmp + k + 1, f[id]);
}
}
f[id][1] += cnt;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, qq;
cin >> n >> m >> qq;
fill(hd, hd + n, -1);
for (int i = 0, x, y; i < m; i++)
{
cin >> x >> y, x--, y--;
add(x, y);
}
fill(ord, ord + n, -1);
dfs(0, -1);
lcainit(n);
for (int i = n - 1; i >= 0; i--)
{
int x = ps[i];
for (int y : fp[x])
{
int d = lca(x, y);
if (ord[d] > ord[par[x]])
d = par[x];
if (ord[d] < ord[par[y]])
par[y] = d;
// cerr << x << " " << y << " " << d << " " << par[y] << endl;
}
}
use_bitset = (n <= NN);
if (use_bitset)
{
queue<int> q;
for (int i = 0; i < n; i++)
if (deg[i] == 0)
q.push(i);
while (!q.empty())
{
int id = q.front(); q.pop();
vis[id].set(id);
for (int p = hd[id], dst; p != -1; p = nxt[p])
{
dst = des[p];
vis[dst] |= vis[id];
if (--deg[dst] == 0)
q.push(dst);
}
}
for (int i = 0; i < n; i++)
for (int x = i; x != -1; x = par[x])
vis[i].reset(x);
}
// for (int i = 0; i < n; i++)
// cerr << par[i] << " \n"[i == n - 1];
ind = 0;
for (int i = 0; i < n; i++)
tr[i].clear();
for (int i = 1; i < n; i++)
tr[par[i]].push_back(i);
dfs0(0, -1);
lcainit(n);
int c;
vector<pair<int, int>> ps;
while (qq--)
{
cin >> k >> c;
ps.resize(c);
cn.reset();
for (int i = 0, x; i < c; i++)
{
cin >> x, x--;
gd[x] = 1;
ps[i] = { ord[x], x };
cn |= vis[x];
}
sort(all(ps));
for (int i = 1, j = ssz(ps); i < j; i++)
{
int z = lca(ps[i - 1].second, ps[i].second);
ps.emplace_back(ord[z], z);
}
sort(all(ps));
ps.erase(unique(all(ps)), ps.end());
for (int i = 1; i < ssz(ps); i++)
{
Tr[lca(ps[i - 1].second, ps[i].second)].push_back(ps[i].second);
// cerr << lca(ps[i - 1].second, ps[i].second) << " ---> " << ps[i].second << endl;
}
dfs3(ps[0].second, -1);
cout << f[ps[0].second][k] << "\n";
for (auto [_, x] : ps)
Tr[x].clear(), gd[x] = 0;
}
return 0;
}
// started coding at: 06-04 10:58:56
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 7900kb
input:
4 4 4 1 2 1 3 2 4 3 4 1 1 4 2 1 4 1 1 3 2 2 3 2
output:
2 0 2 1
result:
ok 4 lines
Test #2:
score: 0
Accepted
time: 0ms
memory: 9704kb
input:
8 10 4 1 2 2 3 1 3 3 6 6 8 2 4 2 5 4 7 5 7 7 8 2 3 4 5 6 2 2 6 8 1 1 6 1 1 8
output:
0 0 3 2
result:
ok 4 lines
Test #3:
score: 0
Accepted
time: 0ms
memory: 9684kb
input:
8 9 4 1 2 1 3 3 6 6 8 2 4 2 5 4 7 5 7 7 8 2 3 4 5 6 2 2 6 8 1 1 6 1 1 8
output:
2 0 3 2
result:
ok 4 lines
Test #4:
score: 0
Accepted
time: 2ms
memory: 9664kb
input:
8 8 2 1 2 1 3 2 4 4 5 5 8 3 6 6 7 7 8 2 2 5 7 1 1 8
output:
9 2
result:
ok 2 lines
Test #5:
score: 0
Accepted
time: 2ms
memory: 9728kb
input:
8 8 1020 1 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6 4 6 1 2 3 4 5 6 2 2 1 5 3...
output:
1 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1 0 0 1 0 0 0 0 1 0 0 0 0 4 1 0 0 1 0 0 0 0 1 0 0 1 0 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 3 1 0 0 1 0 1 0 0 0 0 1 0 0 0 0 1 0 ...
result:
ok 1020 lines
Test #8:
score: -100
Wrong Answer
time: 2ms
memory: 9752kb
input:
8 9 2 1 2 1 3 2 4 4 5 5 8 3 6 6 7 7 8 6 2 2 2 5 7 1 1 8
output:
3 4
result:
wrong answer 2nd lines differ - expected: '2', found: '4'