QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#601570#4885. Triangular Cactus PathszltWA 11ms81000kbC++142.9kb2024-09-30 07:51:222024-09-30 07:51:23

Judging History

你现在查看的是最新测评结果

  • [2024-09-30 07:51:23]
  • 评测
  • 测评结果:WA
  • 用时:11ms
  • 内存:81000kb
  • [2024-09-30 07:51:22]
  • 提交

answer

#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;

const int maxn = 1000100;
const int logn = 21;
const int N = 1000000;
const ll mod = 998244353;

inline ll qpow(ll b, ll p) {
	ll res = 1;
	while (p) {
		if (p & 1) {
			res = res * b % mod;
		}
		b = b * b % mod;
		p >>= 1;
	}
	return res;
}

int n, m, q;
ll fac[maxn], ifac[maxn];

inline ll C(ll n, ll m) {
	if (n < m || n < 0 || m < 0) {
		return 0;
	} else {
		return fac[n] * ifac[m] % mod * ifac[n - m] % mod;
	}
}

int dfn[maxn], low[maxn], tim, stk[maxn], top, nt, f[maxn], g[maxn];
vector<int> G[maxn], T[maxn];

void dfs(int u, int fa) {
	dfn[u] = low[u] = ++tim;
	stk[++top] = u;
	for (int v : G[u]) {
		if (v == fa) {
			continue;
		}
		if (!dfn[v]) {
			dfs(v, u);
			low[u] = min(low[u], low[v]);
			if (low[v] >= dfn[u]) {
				++nt;
				while (1) {
					int x = stk[top--];
					T[x].pb(nt);
					T[nt].pb(x);
					if (x == v) {
						break;
					}
				}
				T[u].pb(nt);
				T[nt].pb(u);
			}
		} else {
			low[u] = min(low[u], dfn[v]);
		}
	}
}

int st[logn][maxn], fa[maxn];

inline int get(int i, int j) {
	return dfn[i] < dfn[j] ? i : j;
}

inline int qlca(int x, int y) {
	if (x == y) {
		return x;
	}
	x = dfn[x];
	y = dfn[y];
	if (x > y) {
		swap(x, y);
	}
	++x;
	int k = __lg(y - x + 1);
	return get(st[k][x], st[k][y - (1 << k) + 1]);
}

inline int qf(int x, int y) {
	int z = qlca(x, y);
	return f[x] + f[y] - f[z] - f[fa[z]];
}

inline int qg(int x, int y) {
	int z = qlca(x, y);
	return g[x] + g[y] - g[z] - g[fa[z]];
}

void dfs2(int u, int fa) {
	f[u] = f[fa] + (u <= n);
	g[u] = g[fa] + (u > n && (int)T[u].size() == 3);
	dfn[u] = ++tim;
	::fa[u] = fa;
	st[0][tim] = fa;
	for (int v : T[u]) {
		if (v == fa) {
			continue;
		}
		dfs2(v, u);
	}
}

void solve() {
	fac[0] = 1;
	for (int i = 1; i <= N; ++i) {
		fac[i] = fac[i - 1] * i % mod;
	}
	ifac[N] = qpow(fac[N], mod - 2);
	for (int i = N - 1; ~i; --i) {
		ifac[i] = ifac[i + 1] * (i + 1) % mod;
	}
	scanf("%d%d%d", &n, &m, &q);
	nt = n;
	while (m--) {
		int u, v;
		scanf("%d%d", &u, &v);
		G[u].pb(v);
		G[v].pb(u);
	}
	dfs(1, -1);
	tim = 0;
	dfs2(1, 0);
	for (int j = 1; (1 << j) <= nt; ++j) {
		for (int i = 1; i + (1 << j) - 1 <= nt; ++i) {
			st[j][i] = get(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
		}
	}
	while (q--) {
		int x, y, k;
		scanf("%d%d%d", &x, &y, &k);
		int d = qf(x, y) - 1;
		if (k < d) {
			puts("0");
			continue;
		}
		int t = qg(x, y);
		printf("%lld\n", C(t, k - d));
	}
}

int main() {
	int T = 1;
	// scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 11ms
memory: 81000kb

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

result:

wrong answer Answer contains longer sequence [length = 6], but output contains 1 elements