UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#213504#573. t2nullptr_qwq1003720ms48328kbC++115.5kb2024-11-12 18:40:572024-11-12 23:06:34

answer

// 私は猫です

#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define pb push_back
#define mkp make_pair
#define fi first
#define se second
#define inf 1000000000
#define infll 1000000000000000000ll
#define pii pair<int,int>
#define rep(i,a,b,c) for(int i=(a);i<=(b);i+=(c))
#define per(i,a,b,c) for(int i=(a);i>=(b);i-=(c))
#define F(i,a,b) for(int i=a,i##end=b;i<=i##end;i++)
#define dF(i,a,b) for(int i=a,i##end=b;i>=i##end;i--)
#define cmh(sjy) while(sjy--)
#define lowbit(x) (x&(-x))
#define HH printf("\n")
#define eb emplace_back
#define poly vector<int>
#define SZ(x) ((int)x.size())
using namespace std;
ll read(){
	ll x=0,f=1;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-') f=-1;c=getchar();}
	while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+(c^48),c=getchar();
	return x*f;
}
namespace Fastio{struct Reader{template<typename T>Reader&operator>>(T&x){x=0;short f=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+(c^48),c=getchar();x*=f;return*this;}Reader&operator>>(double&x){x=0;double t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else{x*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return*this;}Reader&operator>>(long double&x){x=0;long double t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else{x*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return*this;}Reader&operator>>(__float128&x){x=0;__float128 t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else{x*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return*this;}Reader&operator>>(char&c){c=getchar();while(c==' '||c=='\n'||c=='\r')c=getchar();return*this;}Reader&operator>>(char*str){int len=0;char c=getchar();while(c==' '||c=='\n'||c=='\r')c=getchar();while(c!=' '&&c!='\n'&&c!='\r')str[len++]=c,c=getchar();str[len]='\0';return*this;}Reader&operator>>(string&str){str.clear();char c=getchar();while(c==' '||c=='\n'||c=='\r')c=getchar();while(c!=' '&&c!='\n'&&c!='\r')str.push_back(c),c=getchar();return*this;}Reader(){}}cin;const char endl='\n';struct Writer{const int Setprecision=6;typedef int mxdouble;template<typename T>Writer&operator<<(T x){if(x==0){putchar('0');return*this;}if(x<0)putchar('-'),x=-x;static short sta[40];short top=0;while(x>0)sta[++top]=x%10,x/=10;while(top>0)putchar(sta[top]+'0'),top--;return*this;}Writer&operator<<(double x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(double)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<Setprecision-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}Writer&operator<<(long double x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(long double)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<Setprecision-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}Writer&operator<<(__float128 x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(__float128)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<Setprecision-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}Writer&operator<<(char c){putchar(c);return*this;}Writer&operator<<(char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}Writer&operator<<(const char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}Writer&operator<<(string str){int st=0,ed=str.size();while(st<ed)putchar(str[st++]);return*this;}Writer(){}}cout;}using namespace Fastio;
#define cin Fastio::cin
#define cout Fastio::cout
#define endl Fastio::endl
template<typename T>inline void chkmax(T &x,const T &y){ x=std::max(x,y); }
template<typename T>inline void chkmin(T &x,const T &y){ x=std::min(x,y); }
const int mod=998244353,maxn=500005;
ll ans[maxn];
int fa[maxn],n,zsy,rk[maxn],siz[maxn],vis[maxn];
vector<int>fac[maxn];
vector<pii>vec[maxn];
int find(int x){ return fa[x]==x?x:fa[x]=find(fa[x]); }
void merge(int u,int v){
	u=find(u),v=find(v);
	if(u==v)return;
	if(rk[u]<rk[v])siz[fa[u]=v]+=siz[u],siz[u]=0;
	else siz[fa[v]=u]+=siz[v],siz[v]=0,rk[u]+=(rk[u]==rk[v]);
}
void Solve(int k){
	if(vec[k].empty())return;
	vector<int>V;
	for(auto it:vec[k])V.push_back(it.fi),V.push_back(it.se);
	for(int i:V)fa[i]=i,siz[i]=1,vis[i]=rk[i]=0;
	for(auto it:vec[k])merge(it.fi,it.se);
	for(int i:V)if(!vis[i]&&find(i)==i)vis[i]=1,ans[k]+=(1ll*siz[i]*(siz[i]-1))>>1;
}
void solve(){
	cin>>n>>zsy;
	F(i,1,n-1){
		int u,v,w; cin>>u>>v>>w;
		for(int i:fac[w])vec[i].push_back(mkp(u,v));
	}
	F(i,1,100000)Solve(i);
	F(i,1,zsy){
		int x; cin>>x; cout<<ans[x]<<endl;
	}
}
signed main(){
	// ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	F(i,1,100000)rep(j,i,100000,i)fac[j].push_back(i);
	int zsy=1;
	F(____,1,zsy)solve();
}

详细

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 30
Accepted

Test #1:

score: 30
Accepted
time: 52ms
memory: 33000kb

input:

50 50
48 29 49788
47 48 31142
35 48 28665
10 35 23889
39 35 6411
50 39 66666
43 35 27629
46 10 49173...

output:

2
1
0
0
0
2
0
0
0
0
0
0
1
10
0
0
1
0
0
2
0
1
0
2
0
0
0
0
0
2
0
0
0
0
2
0
0
0
1
0
0
1
0
2
1
2
0
0
0
0

result:

ok 50 tokens

Test #2:

score: 0
Accepted
time: 46ms
memory: 32972kb

input:

50 50
48 29 36145
47 29 82496
35 47 66171
10 47 40597
39 48 64355
50 48 98687
43 39 15472
46 35 3729...

output:

0
0
0
4
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
3
1
0
13
0
0
1
0
1
1
0
20
1
1
0
6
0
0
1

result:

ok 50 tokens

Test #3:

score: 0
Accepted
time: 53ms
memory: 33008kb

input:

50 50
48 29 38855
47 29 33850
35 29 87324
10 29 73658
39 48 22299
50 47 14355
43 29 86962
46 47 4177...

output:

0
0
1
0
9
0
0
1
0
0
0
0
0
2
0
0
0
0
1
0
0
2
2
0
0
0
0
0
0
2
0
0
0
0
0
0
0
0
3
58
0
0
0
0
0
3
0
0
0
0

result:

ok 50 tokens

Test #4:

score: 0
Accepted
time: 56ms
memory: 32992kb

input:

50 50
48 29 25212
47 48 68851
35 48 24830
10 47 90366
39 10 96596
50 10 30023
43 47 58452
46 29 2989...

output:

0
6
0
1
7
4
0
3
1
0
7
0
1
0
0
0
0
4
0
6
0
7
0
0
0
4
2
3
0
0
0
6
0
0
0
0
0
4
6
3
1
3
0
0
4
0
0
0
1
6

result:

ok 50 tokens

Test #5:

score: 0
Accepted
time: 50ms
memory: 32996kb

input:

50 50
48 29 27922
47 29 20205
35 29 45983
10 48 7074
39 10 54540
50 29 62044
43 10 29942
46 43 34375...

output:

0
0
0
0
14
0
0
0
2
0
0
2
1
0
0
0
0
0
0
3
0
0
0
2
0
1
1
0
0
18
0
0
0
0
0
0
3
1
1
0
1
0
0
0
2
0
0
25
1...

result:

ok 50 tokens

Test #6:

score: 0
Accepted
time: 50ms
memory: 33020kb

input:

50 50
48 29 14279
47 48 71559
35 48 83489
10 35 23782
39 47 12484
50 47 77712
43 50 1432
46 50 22499...

output:

0
0
1
3
24
0
0
0
1
2
0
10
2
0
0
0
1
0
0
0
0
0
10
0
0
0
0
0
0
1
0
0
1
0
1
2
0
0
0
0
0
0
0
93
0
0
0
0
...

result:

ok 50 tokens

Test #7:

score: 0
Accepted
time: 54ms
memory: 32956kb

input:

50 50
48 29 636
47 29 22913
35 47 4642
10 47 40490
39 29 70428
50 10 9733
43 48 72922
46 10 26976
14...

output:

2
0
0
0
3
0
0
7
0
1
1
0
99
0
0
0
0
0
0
0
0
0
0
0
0
99
0
2
2
0
6
3
0
0
2
0
0
3
4
0
0
3
0
0
1
0
1
1
2
0

result:

ok 50 tokens

Test #8:

score: 0
Accepted
time: 51ms
memory: 32988kb

input:

50 50
48 29 3346
47 48 57914
35 29 42148
10 29 73551
39 29 44725
50 39 25401
43 35 60765
46 35 15100...

output:

0
1225
0
0
0
0
0
0
0
0
0
1
1
0
0
1
0
2
0
0
0
0
0
0
0
0
3
0
0
2
0
0
1
0
0
0
0
0
2
0
0
0
0
4
0
1
0
0
0
2

result:

ok 50 tokens

Test #9:

score: 0
Accepted
time: 57ms
memory: 33012kb

input:

50 50
48 29 89703
47 29 9268
35 47 63301
10 35 90259
39 35 2669
50 48 41069
43 39 32255
46 47 19577
...

output:

0
0
0
2
0
0
0
0
0
1
0
5
1
0
0
1
0
2
0
1
2
0
5
0
6
0
3
4
0
0
0
0
3
0
0
0
5
2
0
3
0
0
1
0
0
0
0
0
0
0

result:

ok 50 tokens

Test #10:

score: 0
Accepted
time: 53ms
memory: 32992kb

input:

50 50
48 29 92413
47 48 60622
35 29 807
10 48 6967
39 35 60613
50 35 73090
43 29 3745
46 29 7701
14 ...

output:

0
0
0
0
0
0
0
0
1
1
1
0
1
0
0
1
2
1
2
0
0
4
0
0
0
0
0
0
0
2
0
0
0
0
7
0
1
0
0
0
0
0
0
0
6
0
2
0
0
0

result:

ok 50 tokens

Subtask #2:

score: 30
Accepted

Test #11:

score: 30
Accepted
time: 106ms
memory: 40312kb

input:

100000 100000
73595 40695 76
13615 40695 96
65545 13615 84
19391 13615 76
2353 73595 27
26730 40695 ...

output:

12815
1065
2028
53298
627586
19060
4345
1010
16097
1032
1044
1054
3191
16097
1055
627586
19060
978
9...

result:

ok 100000 tokens

Test #12:

score: 0
Accepted
time: 95ms
memory: 40412kb

input:

100000 100000
73595 40695 70
13615 73595 52
65545 73595 51
19391 73595 72
2353 19391 18
26730 13615 ...

output:

975
1028
15852
1040
15852
5571
23908
49914
1010
15852
646494
12387
4999950000
20056
2104
4999950000
...

result:

ok 100000 tokens

Test #13:

score: 0
Accepted
time: 103ms
memory: 40380kb

input:

100000 100000
73595 40695 64
13615 40695 61
65545 13615 71
19391 65545 15
2353 19391 9
26730 19391 4...

output:

1041
2062
14285
23281
49485
12419
996
1018
1011
19672
19672
1036
23281
3195
4999950000
12419
1018
20...

result:

ok 100000 tokens

Test #14:

score: 0
Accepted
time: 92ms
memory: 40452kb

input:

100000 100000
73595 40695 58
13615 73595 70
65545 40695 38
19391 13615 58
2353 13615 47
26730 40695 ...

output:

23392
993
1036
13972
5412
1022
19503
3237
48221
23392
981
958486
1004
980
23392
993
48221
1020
980
1...

result:

ok 100000 tokens

Test #15:

score: 0
Accepted
time: 104ms
memory: 40308kb

input:

100000 100000
73595 40695 52
13615 40695 26
65545 73595 58
19391 40695 1
2353 13615 38
26730 13615 9...

output:

1021
48268
4999950000
998
2011
94940
1049
19311
19311
33716
48268
15508
2124
1040
985
15508
8140
482...

result:

ok 100000 tokens

Test #16:

score: 0
Accepted
time: 101ms
memory: 40300kb

input:

100000 100000
73595 40695 46
13615 73595 35
65545 40695 25
19391 13615 97
2353 40695 76
26730 65545 ...

output:

12645
997
1042
4999950000
1060
23020
4222
974
5631
19302
2078
1030
48335
2033
1002
1986
1018
6806
19...

result:

ok 100000 tokens

Test #17:

score: 0
Accepted
time: 113ms
memory: 40276kb

input:

100000 100000
73595 40695 40
13615 40695 91
65545 73595 45
19391 73595 40
2353 40695 67
26730 2353 4...

output:

9577
5383
23807
997
941
1013
19428
1003
95552
1024
49914
12451
2082
1109
49914
95552
49914
499995000...

result:

ok 100000 tokens

Test #18:

score: 0
Accepted
time: 97ms
memory: 40320kb

input:

100000 100000
73595 40695 34
13615 40695 100
65545 13615 12
19391 65545 83
2353 65545 58
26730 73595...

output:

4999950000
4416
2028
94412
4999950000
14337
1002
12327
23226
1038
6629
998
5505
1059
14337
2064
1042...

result:

ok 100000 tokens

Test #19:

score: 0
Accepted
time: 108ms
memory: 40412kb

input:

100000 100000
73595 40695 28
13615 73595 56
65545 40695 79
19391 13615 26
2353 65545 96
26730 65545 ...

output:

1031
93090
15639
3190
19477
1041
32364
12280
14143
47814
3110
416410
32364
22889
19477
4999950000
49...

result:

ok 100000 tokens

Test #20:

score: 0
Accepted
time: 92ms
memory: 40180kb

input:

100000 100000
73595 40695 22
13615 40695 65
65545 13615 99
19391 40695 22
2353 73595 87
26730 19391 ...

output:

33139
4999950000
3015
33139
91207
2093
944
1051
1006
1009
91207
15171
5538
33139
1031
2021
1018
9120...

result:

ok 100000 tokens

Subtask #3:

score: 40
Accepted

Test #21:

score: 40
Accepted
time: 224ms
memory: 48328kb

input:

100000 100000
73595 40695 12816
13615 73595 81821
65545 40695 75866
19391 65545 1165
2353 73595 3737...

output:

8
2270
96901
1
2085
1584
4228
9112
6
0
2
0
11
1035
2833
0
4999950000
2
2
1294
6056
2221
4398
1587
49...

result:

ok 100000 tokens

Test #22:

score: 0
Accepted
time: 208ms
memory: 48088kb

input:

100000 100000
73595 40695 44510
13615 40695 64430
65545 73595 54386
19391 73595 2608
2353 19391 4081...

output:

5830
49192
1369
1384
6
1
9
1908
6158
104
4786
2590
4786
1205
1985
1268
3
9
6158
2
10
1433
1181
4544
...

result:

ok 100000 tokens

Test #23:

score: 0
Accepted
time: 221ms
memory: 48144kb

input:

100000 100000
73595 40695 76204
13615 73595 30686
65545 13615 16553
19391 40695 4051
2353 13615 6060...

output:

5
14002
1
3
21
16618
1046
2
2170
3188
4
2
1
1349
3
7
2
4346
1
6
3
3
1085
1
0
1
50389
552
4954
1936
1...

result:

ok 100000 tokens

Test #24:

score: 0
Accepted
time: 216ms
memory: 48196kb

input:

100000 100000
73595 40695 7898
13615 40695 13295
65545 73595 95073
19391 13615 5494
2353 13615 80398...

output:

6
1554
1510
10
2
2
2
1195
1
1972
0
3425
13
6473
0
1266
1515
4
0
17021
3
1
1628
2940
1515
1681
5551
1...

result:

ok 100000 tokens

Test #25:

score: 0
Accepted
time: 210ms
memory: 48084kb

input:

100000 100000
73595 40695 39592
13615 40695 79551
65545 13615 57240
19391 73595 23290
2353 40695 838...

output:

762017
4394
26
4394
3
3
7667
4186
0
4606
1619
3964
4999950000
0
2129
21
0
1619
1085
762017
1
4
1414
...

result:

ok 100000 tokens

Test #26:

score: 0
Accepted
time: 225ms
memory: 48056kb

input:

100000 100000
73595 40695 71286
13615 73595 62160
65545 40695 35760
19391 65545 24733
2353 40695 362...

output:

1
3594
11033
1
4200
5476
2
3
1030
2390
2
0
3893
1
1522
1258
1094
20754
5
0
1030
16814
1258
0
14
2
3
...

result:

ok 100000 tokens

Test #27:

score: 0
Accepted
time: 215ms
memory: 48120kb

input:

100000 100000
73595 40695 2980
13615 40695 28416
65545 73595 97927
19391 13615 26176
2353 65545 7065...

output:

4185
1154
5
54445
2083
0
1138
0
1322
1685
1134
5589
15
0
1
0
8
5
7
2905
1083
1313
2339
1935
0
3042
2...

result:

ok 100000 tokens

Test #28:

score: 0
Accepted
time: 215ms
memory: 48088kb

input:

100000 100000
73595 40695 34674
13615 73595 11025
65545 40695 60094
19391 40695 27619
2353 65545 268...

output:

0
5
24932
0
1298
8408
2
1037
1
1
0
3
3310
4
0
4062
1386
2441
3698
3410
1079
92899
1835
1639
7124
0
1...

result:

ok 100000 tokens

Test #29:

score: 0
Accepted
time: 228ms
memory: 48028kb

input:

100000 100000
73595 40695 98062
13615 73595 59890
65545 13615 781
19391 73595 46858
2353 73595 50085...

output:

1
0
2254
2
3
0
4
0
3853
5
3476
1901
41
4
1298
4600
0
5
0
5767
3
56
3959
1428
2
5
7339
2659
3959
3
68...

result:

ok 100000 tokens

Test #30:

score: 0
Accepted
time: 225ms
memory: 48048kb

input:

100000 100000
73595 40695 29756
13615 40695 26146
65545 40695 79301
19391 65545 48301
2353 19391 698...

output:

1393
0
5859
1549
7
1289
3848
1463
10016
1
2
3
12725
2139
3
5
2440
4295
11225
1762
0
1
2842
1
1289
5
...

result:

ok 100000 tokens