QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#51632 | #4885. Triangular Cactus Paths | zhoukangyang# | WA | 62ms | 15488kb | C++11 | 2.6kb | 2022-10-03 09:21:35 | 2022-10-03 09:21:37 |
Judging History
answer
#include<bits/stdc++.h>
#define L(i, j, k) for(int i = (j); i <= (k); ++i)
#define R(i, j, k) for(int i = (j); i >= (k); --i)
#define ll long long
#define ull unsigned long long
#define vi vector <int>
#define sz(a) ((int) (a).size())
#define me(f, x) memset(f, x, sizeof(f))
using namespace std;
const int N = 5e5 + 7, mod = 998244353;
int qpow(int x, int y = mod - 2) {
int res = 1;
for(; y; x = (ll) x * x % mod, y >>= 1) if(y & 1) res = (ll) res * x % mod;
return res;
}
int fac[N], ifac[N], inv[N];
void init(int x) {
fac[0] = ifac[0] = inv[1] = 1;
L(i, 2, x) inv[i] = (ll) (mod - mod / i) * inv[mod % i] % mod;
L(i, 1, x) fac[i] = (ll) fac[i - 1] * i % mod, ifac[i] = (ll) ifac[i - 1] * inv[i] % mod;
}
int C(int x, int y) {
return x < y || y < 0 ? 0 : (ll) fac[x] * ifac[y] % mod * ifac[x - y] % mod;
}
int n, m, q, cnt;
int Ehd[N], Ev[N], Enx[N], Eid;
void Eadd(int u, int v) { ++Eid, Enx[Eid] = Ehd[u], Ev[Eid] = v, Ehd[u] = Eid; }
vi e[N];
int low[N], dfn[N], stk[N], stot, idtot;
void dfs(int x, int fa) {
low[x] = dfn[x] = ++idtot, stk[++stot] = x;
for(int i = Ehd[x]; i; i = Enx[i]) if(Ev[i] != fa) {
int v = Ev[i];
if(!dfn[v]) {
dfs(v, x), low[x] = min(low[x], low[v]);
if(low[v] >= dfn[x]) {
++cnt;
vi qwq = {x};
for(int u = 0; u != v; ) u = stk[stot--], qwq.emplace_back(u);
if(sz(qwq) > 2) {
for(const int &t : qwq) e[t].emplace_back(cnt), e[cnt].emplace_back(t);
} else {
e[qwq[0]].emplace_back(qwq[1]),
e[qwq[1]].emplace_back(qwq[0]);
}
}
}
else if(dfn[v] < dfn[x]) low[x] = min(low[x], dfn[v]);
}
}
int up[20][N], fa[N], dis1[N], dis2[N], dep[N];
void sdfs(int x) {
if(x <= n) dis1[x] += 1;
else dis2[x] += 1;
for(const int &v : e[x]) if(v != fa[x])
up[0][v] = fa[v] = x, dis1[v] = dis1[x], dis2[v] = dis2[x], dep[v] = dep[x] + 1, sdfs(v);
}
int lcca(int x, int y) {
if(dep[x] < dep[y]) swap(x, y);
R(i, 19, 0) if(dep[x] - dep[y] >= (1 << i)) x = up[i][x];
R(i, 19, 0) if(up[i][x] != up[i][y]) x = up[i][x], y = up[i][y];
return x == y ? x : up[0][x];
}
int main() {
ios :: sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> m, cnt = n, init(n * 2);
L(i, 1, m) {
int u, v;
cin >> u >> v;
Eadd(u, v), Eadd(v, u);
}
dfs(1, 0), sdfs(1);
L(i, 1, 19) L(j, 1, n) up[i][j] = up[i - 1][up[i - 1][j]];
cin >> q;
while(q--) {
int u, v, k;
cin >> u >> v >> k;
int lca = lcca(u, v);
int d1 = dis1[u] + dis1[v] - dis1[lca] - dis1[fa[lca]] - 1;
int d2 = dis2[u] + dis2[v] - dis2[lca] - dis2[fa[lca]];
// cout << "d1 = " << d1 << ' ' << d2 << '\n';
k -= d1;
cout << C(d2, k) << '\n';
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 15488kb
input:
8 10 1 2 2 3 3 1 3 4 4 5 5 6 6 4 4 7 7 8 8 4 6 1 1 0 1 1 1 1 4 3 6 2 4 5 7 4 3 4 2
output:
1 0 1 2 1 0
result:
ok 6 numbers
Test #2:
score: 0
Accepted
time: 2ms
memory: 15464kb
input:
2 1 1 2 8 1 1 0 1 1 1 1 2 0 1 2 1 2 1 0 2 1 1 2 2 0 2 2 1
output:
1 0 0 1 0 1 1 0
result:
ok 8 numbers
Test #3:
score: -100
Wrong Answer
time: 62ms
memory: 15420kb
input:
50 70 41 24 9 15 29 19 21 11 1 14 5 27 34 48 10 32 34 49 46 3 22 33 34 39 16 30 22 45 7 16 25 30 43 17 22 44 5 25 41 49 29 32 39 25 10 4 45 27 13 38 29 7 3 35 14 30 50 2 8 11 13 35 18 26 34 40 38 36 7 19 12 3 25 26 30 42 21 8 12 46 44 33 14 31 47 2 25 46 20 19 49 24 15 43 18 25 13 36 27 22 4 32 30 3...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 10 0 0 0 0 0 0 3 0 0 0 0 0 4 0 0 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 6 0 0 0 0 0 0 0 0 7 0 0 0 0 3 0 6 0 0 0 0 11 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 15 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 ...
result:
wrong answer 26th numbers differ - expected: '0', found: '10'