QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#588049#8078. Spanning TreexljxWA 80ms45192kbC++141.4kb2024-09-24 23:57:142024-09-24 23:57:15

Judging History

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

  • [2024-09-24 23:57:15]
  • 评测
  • 测评结果:WA
  • 用时:80ms
  • 内存:45192kb
  • [2024-09-24 23:57:14]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 1e6 + 5;
const int mod = 998244353;

int n, ans = 1;
int a[maxn], b[maxn], id[maxn], fa[maxn], cnt[maxn], depth[maxn];

vector<int> G[maxn];

int root(int x) { return id[x] == x ? x : id[x] = root(id[x]); }

void unite(int x, int y)
{
	x = root(x); y = root(y);
	if (x == y) return;
	id[x] = y;
	cnt[y] += cnt[x];
	depth[y] = min(depth[x], depth[y]);
}

void dfs(int u, int f, int d)
{
	fa[u] = f; depth[u] = d;
	for (int i = 0; i < G[u].size(); i++)
		if (G[u][i] != f)
			dfs(G[u][i], u, d + 1);
}

int qpow(int x, int p)
{
	int t = 1;
	while (p)
	{
		if (p & 1) (t *= x) %= mod;
		(x *= x) %= mod;
		p >>= 1;
	}
	return t;
}

signed main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
		id[i] = i, cnt[i] = 1;
	for (int i = 1; i < n; i++)
		cin >> a[i] >> b[i];
	for (int i = 1; i < n; i++)
	{
		int u, v;
		cin >> u >> v;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	dfs(1, 0, 1);
	for (int i = 1; i < n; i++)
	{
		int A = root(a[i]), B = root(b[i]);
		(ans *= cnt[A] * cnt[B]) %= mod;
		int u = depth[A] > depth[B] ? A : B, v = A + B - u;
		bool check = false;
		for (int j = 0; j < G[u].size(); j++)
			if (root(G[u][j]) == v)
				check = true;
		if (!check)
		{
			cout << 0;
			return 0;
		}
		unite(A, B);
	}
	cout << qpow(ans, mod - 2);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 38968kb

input:

3
1 2
1 3
1 2
1 3

output:

499122177

result:

ok single line: '499122177'

Test #2:

score: -100
Wrong Answer
time: 80ms
memory: 45192kb

input:

100000
2183 5543
22424 59062
8387 24544
14668 74754
15788 58136
26846 99981
13646 57752
29585 62862
27383 65052
2013 13116
24490 91945
26006 61595
19000 78817
31371 67837
29311 82989
4298 20577
23212 77962
16510 85839
26010 98714
25314 96063
17206 82359
7478 76540
13890 99556
23277 64321
25684 92613...

output:

0

result:

wrong answer 1st lines differ - expected: '330864231', found: '0'