QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#519382#8078. Spanning TreeTimeless123WA 37ms43280kbC++173.9kb2024-08-14 19:22:062024-08-14 19:22:06

Judging History

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

  • [2024-08-14 19:22:06]
  • 评测
  • 测评结果:WA
  • 用时:37ms
  • 内存:43280kb
  • [2024-08-14 19:22:06]
  • 提交

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;

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

ll fa[N];
ll sz[N];
ll d[N];

ll find(ll x) {
	return fa[x]=(fa[x]==x?x:find(fa[x]));
}
ll p[N];

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

}

void solve() {

	cin >> n;

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

	m = n - 2;

   

	ll u, v;
    vp.push_back({0,0});
	 for(int i = 1; i < n;++ i) 
     {
		cin >> u >> v;
		vp.push_back({ u, v });
	}

	 for(int i = 1; i < n;++ i) 
     {
		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;

	 for(int i = 1; i < n;++ i) 
     {
		u = vp[i].first;
		v = vp[i].second;

		ll fu = find(u);
		ll fv = find(v);
        //cout<< fu << ' ' << fv <<'\n';

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

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


        if (d[fu] > d[fv]) swap(fu, fv);
		fa[fv] = fu;
		sz[fu] += sz[fv];
	}

	//cout << ans << '\n';

	cout << qpow(ans, MODE - 2) % MODE << '\n';
	
}


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: 100
Accepted
time: 0ms
memory: 32680kb

input:

3
1 2
1 3
1 2
1 3

output:

499122177

result:

ok single line: '499122177'

Test #2:

score: 0
Accepted
time: 37ms
memory: 40876kb

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:

330864231

result:

ok single line: '330864231'

Test #3:

score: 0
Accepted
time: 27ms
memory: 42584kb

input:

100000
2431 82555
18148 99107
3941 5966
1071 47115
15967 53202
27778 80193
17295 91006
21981 69106
8487 92235
7229 17953
6531 81170
18678 84896
25646 98753
3087 34488
4008 80883
18556 30802
250 3839
10378 92520
27250 87378
18484 75316
12912 82770
6142 78903
18637 58366
5716 82933
25060 76764
7868 78...

output:

207623640

result:

ok single line: '207623640'

Test #4:

score: 0
Accepted
time: 37ms
memory: 39260kb

input:

100000
3677 53591
26136 80586
14460 75864
5626 70315
2145 17733
681 92982
19076 64996
1672 82873
7909 80493
1362 87683
3121 81083
1881 92956
19448 31658
26638 78109
17888 74591
31114 97492
30263 93965
15990 51796
25387 47368
26827 50839
21100 85993
8497 95004
3017 3343
12299 97797
16723 59533
19266 ...

output:

315464493

result:

ok single line: '315464493'

Test #5:

score: 0
Accepted
time: 31ms
memory: 41152kb

input:

100000
337 80784
277 63525
309 68073
50 65780
223 85983
229 99952
419 97414
210 73844
149 98533
19 92591
254 75626
413 28608
31 13397
355 9807
248 86299
177 92507
75 77718
238 94072
440 73716
34 33639
39 92470
177 76740
210 93812
460 86098
79 38556
500 4414
306 93377
341 46557
260 18020
170 70281
99...

output:

782103077

result:

ok single line: '782103077'

Test #6:

score: 0
Accepted
time: 35ms
memory: 40904kb

input:

100000
77 61768
159 75045
189 96344
27 90011
80 70404
100 10876
196 6710
83 69730
122 61478
95 34246
121 66123
76 91590
183 84813
30 22860
21 64208
197 52093
136 95550
104 74693
141 43684
76 24573
180 96323
27 63135
78 77847
110 49459
67 89691
82 98960
53 62386
94 95145
15 72896
28 92579
145 94344
9...

output:

574681591

result:

ok single line: '574681591'

Test #7:

score: 0
Accepted
time: 35ms
memory: 42480kb

input:

100000
7 93472
18 98769
12 97619
17 73647
10 16818
1 91671
7 87544
6 72485
3 92155
8 72652
13 69083
18 95717
19 96402
6 18778
9 99294
18 90017
17 79729
14 97967
11 92694
8 63979
10 97859
7 44859
2 85081
4 58827
18 98750
6 75751
11 94889
4 22678
8 84842
15 84960
5 76729
2 90310
19 55608
12 87558
16 9...

output:

45611347

result:

ok single line: '45611347'

Test #8:

score: 0
Accepted
time: 19ms
memory: 42868kb

input:

100000
7 58890
3 58280
7 27055
7 93537
1 91565
5 88595
4 84795
3 87969
5 98167
3 89215
8 68189
1 9565
2 75124
5 57314
2 80828
6 69452
7 94539
5 95670
2 69584
7 58461
2 88826
4 60983
4 64741
5 94797
5 85145
3 63668
3 29256
2 80180
3 1473
7 34495
3 25487
6 49093
4 92049
6 32240
6 60954
1 23309
2 7334
...

output:

359179257

result:

ok single line: '359179257'

Test #9:

score: 0
Accepted
time: 31ms
memory: 43280kb

input:

100000
7 52065
5 79374
8 93846
5 98472
10 72503
4 54043
2 81067
5 25410
9 66642
4 96261
2 69048
5 53093
8 72619
5 25253
7 92182
10 85942
2 96401
10 56808
1 65642
4 94067
8 76278
5 92554
5 16028
8 95421
9 96769
9 38660
4 37431
6 89486
5 12160
5 96259
3 84728
1 51495
4 96438
6 49380
1 95929
1 27527
8 ...

output:

407203766

result:

ok single line: '407203766'

Test #10:

score: 0
Accepted
time: 24ms
memory: 41432kb

input:

100000
78 73288
63 78335
71 60255
14 89232
33 31513
96 31412
63 64415
87 60736
56 60604
9 91650
29 86482
65 90995
94 91106
15 93202
56 15471
69 40408
83 88007
42 94851
33 68754
81 58316
87 69603
1 17260
95 78585
8 39063
58 31243
20 13440
94 53628
17 88933
58 98863
65 93116
22 82543
28 86883
27 59282...

output:

517010519

result:

ok single line: '517010519'

Test #11:

score: 0
Accepted
time: 33ms
memory: 42952kb

input:

100000
442 69813
189 43985
356 86661
74 29978
381 51503
808 84079
837 91324
12 91061
238 90167
910 98749
28 45930
834 97170
636 56426
592 99796
752 67695
489 66396
688 49989
151 94252
280 5651
363 78766
223 93772
937 84602
336 78368
264 74532
701 54957
277 78254
891 97873
637 22363
582 43788
887 873...

output:

968562725

result:

ok single line: '968562725'

Test #12:

score: -100
Wrong Answer
time: 28ms
memory: 42828kb

input:

100000
31518 99113
23679 31891
224 4623
1819 48802
11333 71708
11245 92859
8309 75148
10033 89063
297 93596
18282 88408
16020 98356
27633 30998
15471 53295
8023 78199
25209 44744
27250 66553
27805 70208
31835 62514
8589 42520
10650 99871
2095 21829
21341 40322
4584 98667
25361 87279
25248 30530
2625...

output:

873358425

result:

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