QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#387948#4885. Triangular Cactus PathsGiga_Cronos#WA 44ms16004kbC++233.5kb2024-04-13 07:21:242024-04-13 07:21:26

Judging History

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

  • [2024-04-13 07:21:26]
  • 评测
  • 测评结果:WA
  • 用时:44ms
  • 内存:16004kb
  • [2024-04-13 07:21:24]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define int     long long
#define pb      push_back
#define all(x)  (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fs      first
#define sc      second
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
#define fl '\n'

int n, m;
vi g[200005];
vi t[200005];

bool mk[200005];
int low[200005], num[200005];
int cc = 1;
int prnt[200005];
bool lt[200005];
bool ld[200005];
int dd[200005];

void dfs(int u, int p) {
	mk[u] = true;
	num[u] = cc;
	low[u] = cc;
	cc++;
	for (auto v : g[u]) {
		if (v == p)
			continue;
		if (!mk[v]) {
			prnt[v] = u;
			dfs(v, u);
			t[u].pb(v);
		}
		low[u] = min(low[u], low[v]);
	}
	if (low[u] == num[p]) {
		lt[u] = 1;
	}
	if (low[u] == num[prnt[p]]) {
		ld[u] = 1;
	}
}

int pr[200005][25];
int lvl[200005];
int dp[200005];

int f[200005];

void build(int u) {
	if (lt[u])
		dp[u]++;
	
	//cout<<u<<" "<<ld[u]<<fl;
	if (ld[u])
		dd[u]++;
	for (int i = 1; i <= __lg(lvl[u]); i++) {
		pr[u][i] = pr[pr[u][i - 1]][i - 1];
	}
	for (auto v : t[u]) {
		pr[v][0] = u;
		lvl[v] = lvl[u] + 1;
		dp[v] = dp[u];
		dd[v] = dd[u];
		build(v);
	}
}

int kthp(int u, int k) {
	int l = __lg(k);
	while (k) {
		u = pr[u][l];
		k -= (1ll << l);
		l = __lg(k);
	}
	return u;
}

int lca(int a, int b) {
	if (lvl[a] < lvl[b])
		swap(a, b);
	a = kthp(a, lvl[a] - lvl[b]);
	if (a == b)
		return a;
	for (int i = __lg(lvl[a]); i >= 0; i--) {
		if (pr[a][i] != pr[b][i]) {
			a = pr[a][i];
			b = pr[b][i];
		}
	}
	return pr[a][0];
}

int mod = 998244353;
int qpow(int a, int b) {
	if (b == 0)
		return 1;
	if (b == 1)
		return a;
	int r = qpow(a, b / 2);
	int ret = r * r % mod;
	if (b % 2 == 0) {
		return ret;
	} else {
		return ret * a % mod;
	}
}

int inv(int a, int b) { return a * qpow(b, mod - 2) % mod; }

int C(int n, int k) {
	if (k > n)
		return 0;
	return inv(f[n], f[k] * f[n - k] % mod);
}

void solve() {
	cin >> n >> m;

	f[0] = 1;
	for (int i = 1; i <= n; i++) {
		f[i] = (f[i - 1] * i) % mod;
	}

	for (int i = 0; i < m; i++) {
		int a, b;
		 cin >> a >> b;
		// /*
		// if (i < 1e5) {
		// 	a = i + 1;
		// 	b = i + 1 + 1;
		// } else {
		// 	a = 2 * (i - 1e5) + 1;
		// 	b = 2 * (i - 1e5) + 2 + 1;
		// }
		// */
		g[a].pb(b);
		g[b].pb(a);
	}

	dfs(1, -1);
	lvl[1] = 1;
	build(1);
	int q;
	cin >> q;
	for (int i = 0; i < q; i++) {
		int a, b, k;
		cin >> a >> b >> k;
		if (a == b) {
			if (k == 0)
				cout << 1 << fl;
			else
				cout << 0 << fl;
			continue;
		}

		int lc = lca(a, b);

		int rs = lvl[a] - lvl[lc] + lvl[b] - lvl[lc];
		rs -= dd[a] - dd[lc];
		rs -= dd[b] - dd[lc];
		//if ((a == lc || b == lc) && a != b && lt[lc])
		//	rs--;

		//if (lt[a])
		//	rs++;
		//if (lt[b])
		//	rs++;

		if(a != lc && ld[kthp(a, lvl[a] - lvl[lc] - 1)])rs++;
		if(b != lc && ld[kthp(b, lvl[b] - lvl[lc] - 1)])rs++;



		int ck = dp[a] - dp[lc] + dp[b] - dp[lc];
		if ((a != lc && low[kthp(a, lvl[a] - lvl[lc] - 1)] == num[prnt[lc]] &&
		     lc != 1) ||
		    (b != lc && low[kthp(b, lvl[b] - lvl[lc] - 1)] == num[prnt[lc]] &&
		     lc != 1)) {
			ck++;
		}
		//cout<<a<<" "<<b<<" :"<<rs<<" "<<ck<<fl;
		if (rs <= k) {
			cout << C(ck, k - rs) << fl;
		} else {
			cout << 0 << fl;
		}
		// cout << C(50000, k - 50000) << "\n";
	}
}

int32_t main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	solve();
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 13988kb

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: 0ms
memory: 13992kb

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: 0
Accepted
time: 44ms
memory: 13936kb

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
0
0
0
0
0
0
0
3
0
0
0
0
0
4
0
0
15
5
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
10
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
7
0
6
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
0...

result:

ok 200000 numbers

Test #4:

score: -100
Wrong Answer
time: 0ms
memory: 16004kb

input:

99 147
26 66
27 92
75 90
91 64
69 15
70 61
52 59
3 86
41 99
47 71
26 43
99 83
43 80
42 69
4 75
66 71
68 38
31 57
5 91
74 2
90 4
32 11
1 31
77 5
76 8
12 60
79 42
48 89
22 14
36 76
45 91
43 65
99 86
69 16
85 11
19 28
15 52
49 85
84 68
28 85
93 68
16 15
42 9
71 51
72 92
84 2
13 50
9 44
97 78
11 60
98 3...

output:

0
0
0
38
1
0
0
830301496
12620256
0
635936961
12620256
0
205077935
48903492
0
0
0
0
48903492
0
38
0
0
0
0
472733756
406711445
0
0
0
0
501942
0
0
278598664
2760681
0
0
0
163011640
163011640
0
0
710986442
0
703
0
73815
0
8436
423728531
0
635936961
0
0
0
0
703
710986442
0
0
501942
685354923
423728531
0...

result:

wrong answer 4th numbers differ - expected: '231867712', found: '38'