QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#519390#8078. Spanning TreeGrunrayWA 2ms7688kbC++204.8kb2024-08-14 19:34:272024-08-14 19:34:27

Judging History

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

  • [2024-08-14 19:34:27]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:7688kb
  • [2024-08-14 19:34:27]
  • 提交

answer

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;

//#define int long long

#define RG register

#define bit(x) (1LL << (x))
#define lowbit(x) (x & -x)
#define sq(x) ((x) * (x))

#define rep(a, b, c, d) for (int a = (b); a <= (c); a += (d))
#define fep(a, b, c, d) for (int a = (b); a >= (c); a -= (d))

#define PLL pair<long, long>

using unll = unsigned long long;
using ll = long long;

/*--fast read--*/
// use : n = read<int>();
template<typename T> T read() {
	T X = 0; bool flag = true; char ch = getchar();
	while (ch < '0' || ch > '9') { if (ch == '-') flag = false; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { X = (X << 1) + (X << 3) + ch - '0'; ch = getchar(); }
	if (flag) return X;
	return ~(X - 1);
}
// 毒瘤 只能Linux上用 win上报错
//template<typename T> T read() {
//    T X = 0; bool flag = true; char ch = getchar_unlocked(); // 比getchar还快
//    while (ch < '0' || ch > '9') { if (ch == '-') flag = false; ch = getchar_unlocked(); }
//    while (ch >= '0' && ch <= '9') { X = (X << 1) + (X << 3) + ch - '0'; ch = getchar_unlocked(); }
//    if (flag) return X;
//    return ~(X - 1);
//}
// use : write<ll>(n);
template<typename T> void write(T X) {
	if (X < 0) { putchar('-'); X = ~(X - 1); }
	int s[100], top = 0;
	while (X) { s[++top] = X % 10; X /= 10; }
	if (!top) s[++top] = 0;
	while (top) putchar(s[top--] + '0');
}
/*--const--*/
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const long long MODE = 998244353;

const int dx[] = { 1, 0,-1, 0,   1, 1,-1,-1 };
const int dy[] = { 0,-1, 0, 1,  -1, 1,-1, 1 };

const double eps = 1e-8;
double Pi = acos(-1.0);
/*--math--*/
long long qpow(long long x, long long y) {
	x %= MODE;
	long long res = 1;
	while (y) {
		if (y & 1) res = res * x % MODE;
		x = x * x % MODE;
		y >>= 1;
	}
	return res;
}
long long gcd(long long a, long long b) { // 最大公约数
	while (b ^= a ^= b ^= a %= b)
		;
	return a;
}
long long lcm(long long a, long long b) { // 最小公倍数
	return a / gcd(a, b) * b;
}

/*------------CODE------------*/

// int months[15] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };

//priority_queue<ll> pq;//这是一个大根堆q
//priority_queue<int, vector<int>, greater<int> >q;//这是一个小根堆q
//priority_queue<ll, vector<ll>, greater<ll> >pq; // 小根
const long long N = 1e6 + 50;
const long long M = 2e6 + 50;

ll n, m;

//vector<ll>adj[N];
vector<pair<ll, ll> >vp;
vector<ll>cst[N];

ll fa[N];
ll sz[N];
ll fp[N];

ll findfa(ll k) {
	if (k == fa[k]) return k;
	else return fa[k] = findfa(fa[k]);
}
ll p[N];

void dfs(ll x, ll fa)
{
	for (auto& y : cst[x])
		if (y != fa) {
			p[y] = x, dfs(y, x);
			fp[y] = fp[x] + 1;
		}

}

void solve() {

	cin >> n;

	rep(i, 1, n, 1) {
		fa[i] = i;
		sz[i] = 1;
	}

	m = n - 2;

	ll u, v;
	rep(i, 0, m, 1) {
		cin >> u >> v;

		vp.push_back({ u, v });
	}

	rep(i, 0, m, 1) {
		cin >> u >> v;

		cst[u].push_back(v);
		cst[v].push_back(u);
	}

	p[1] = 1;
	dfs(1, 0);

	//for(int i = 1;i <= n;++i) cout<<p[i]<<' '; cout<<'\n';

	ll ans = 1;

	rep(i, 0, m, 1) {
		u = vp[i].first;
		v = vp[i].second;

		ll fu = findfa(u);
		ll fv = findfa(v);

		if (fu == fv) continue;
		

		if ((findfa(p[fu]) != (fv) && findfa(p[fv]) != (fu)))
		{
			//cout << '0' << '\n';
			cout << "0\n";
			return;
		}

		
		if (fp[fu] < fp[fv]) swap(fu, fv);
		// if (findfa(p[fv]) != fu)
		// {
		// 	cout << "0\n";
		// 	return;
		// }

		//ans = (ans * qpow(sz[fu], MODE - 2) )% MODE;
		//ans = (ans * qpow(sz[fv], MODE - 2) )% MODE;

		ans = (ans * sz[fu]) % MODE;
		ans = (ans * sz[fv]) % MODE;

		//ans = (((ans * sz[fu]) % MODE) * sz[fv]) % MODE;

		//ans = qpow(ans, MODE - 2) % MODE;

		fu = findfa(u);
		fv = findfa(v);
		if(fu != fv) {
			fa[fv] = fu;
			sz[fu] = (sz[fu] + sz[fv]);
		}
		
		//ans *= sz[fv] % MODE;

		//cout << "  ans  " << ans << '\n';
		//cout << "  sz  " << sz[fu] << '\n';
		//ans %= MODE;
	}

	// rep(i, 1, n, 1) {
	//     cout << sz[i] << ' ';
	// }cout << '\n';
	cout << ans;
	// ll cnt = 1;
	// ll num = findfa(fa[1]);
	// //cout << "  fa  ";
	// rep(i, 2, n, 1) {
	// 	if (num == findfa(fa[i]))cnt++;
	// 	//cout << fa[i] << ' ';
	// 	// if (fa[i] == i) cnt++;
	// }//cout << '\n';

	// //cout << ans << '\n';
	// //cout << n << '\n';
	// //cout << cnt << '\n';
	// if (cnt == n) {
	// 	//cout << "*****   ";
	// 	//cout << qpow(ans, MODE - 2) % MODE << '\n';
		
	// }
	// else {
	// 	//cout << "  final  ";
	// 	cout << "0\n";
	// }



}

/*
3
1 2
2 1
1 2
1 3
*/

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	//freopen("wrt.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);

	int T = 1;
	//cin >> T;
	while (T--) {
		solve();
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 2ms
memory: 7688kb

input:

3
1 2
1 3
1 2
1 3

output:

2

result:

wrong answer 1st lines differ - expected: '499122177', found: '2'