QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#579334#8078. Spanning TreeSDNUyuqiWA 80ms19600kbC++231.4kb2024-09-21 12:33:032024-09-21 12:33:04

Judging History

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

  • [2024-09-21 12:33:04]
  • 评测
  • 测评结果:WA
  • 用时:80ms
  • 内存:19600kb
  • [2024-09-21 12:33:03]
  • 提交

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,int f)
{
	d[ne] = f;
	for (auto i : nx[ne])
	{
		if (be == i)
		{
			continue;
		}
		fr[i] = ne;
		dfs(ne, i,f+1);
	}
}

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;
	fr[1] = 1;
	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(-1, 1,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;
}

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 9724kb

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: 19600kb

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'