QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#579331#8078. Spanning TreeSDNUyuqiWA 81ms18588kbC++231.3kb2024-09-21 12:27:142024-09-21 12:27:14

Judging History

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

  • [2024-09-21 12:27:14]
  • 评测
  • 测评结果:WA
  • 用时:81ms
  • 内存:18588kb
  • [2024-09-21 12:27:14]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;

#define endl "\n"

typedef long long ll;

const int N = 1e6 + 100;

vector<int>nx[N];
ll fa[N],fr[N],d[N],si[N];

void dfs(int be, int ne)
{
	fr[ne] = be;
	d[ne] = d[be] + 1;
	for (auto i : nx[ne])
	{
		if (be == i)
		{
			continue;
		}
		dfs(ne, i);
	}
}

int find(int x)
{
	if (x != fa[x])
	{
		fa[x] = find(fa[x]);
	}
	return fa[x];
}

void hb(int x, int y)
{
	int a = find(x);
	int b = find(y);
	if (d[a] < d[b])
	{
		swap(a, b);
	}
	fa[a] = b;
	si[b] += si[a];
}

ll P = 998244353;

ll qp(ll a, ll b)
{
	ll an = 1;
	for (a %= P; b; a = a * a % P, b >>= 1)
	{
		if (b & 1)
		{
			an = an * a % P;
		}
	}
	return an;
}

int main()
{
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		fa[i] = i;
		si[i] = 1;
	}
	vector<pair<int, int>>vr(n-1);
	for (auto &[a,b]:vr)
	{
		cin >> a >> b;
	}
	for (int i = 2; i <= n; i++)
	{
		int a, b;
		cin >> a >> b;
		nx[a].push_back(b);
		nx[b].push_back(a);
	}
	dfs(0, 1);
	fr[1] = 1;
	ll an = 1;
	for (auto& [a, b] : vr)
	{
		int faa = find(a);
		int fab = find(b);
		if (find(fr[faa]) == fab || find(fr[fab] == faa))
		{
			an = an * si[faa]%P*si[fab] % P;
			hb(faa, fab);
		}
		else
		{
			cout << 0 << endl;
			return 0;
		}
	}
	cout << qp(an,P-2) << endl;
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
1 2
1 3
1 2
1 3

output:

499122177

result:

ok single line: '499122177'

Test #2:

score: -100
Wrong Answer
time: 81ms
memory: 18588kb

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'