UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#196572#2765. 幽幽子与妖梦wosile100483ms72340kbC++1.6kb2023-10-28 11:03:592023-10-28 12:06:12

answer

#include<bits/stdc++.h>
using namespace std;
int n;
int read(){
	int x=0;
	char c=getchar();
	while(c<'0' || c>'9')c=getchar();
	while(c>='0' && c<='9'){
		x=x*10+c-48;
		c=getchar();
	}
	return x;
}
vector<pair<int,int> >T[500005];
int sz[500005],id[500005];
vector<int>c[500005];
int b[500005],b1[500005];
void dfs1(int x,int fa){
	sz[x]=1;
	for(int i=0;i<T[x].size();i++){
		pair<int,int>e=T[x][i];
		if(e.first==fa)continue;
		dfs1(e.first,x);
		sz[x]+=sz[e.first];
	}
	b[x]=sz[x];
}
void dfs2(int x,int fa,int fw){
	int tmp=id[fw];
	id[fw]=x;
	for(int i=0;i<T[x].size();i++){
		pair<int,int>e=T[x][i];
		if(e.first==fa)continue;
		dfs2(e.first,x,e.second);
	}
	id[fw]=tmp;
//	printf("del %d %d %d %d\n",x,fw,tmp,sz[x]);
	if(tmp==1)b1[fw]-=sz[x];
	else b[tmp]-=sz[x];
	c[fw].push_back(b[x]);
	if(x==1)for(int i=1;i<=n;i++)c[i].push_back(b1[i]);
}
int main(){
	n=read();
	for(int i=1;i<n;i++){
		int u,v,w;
		u=read(),v=read(),w=read();
		T[u].push_back(make_pair(v,w));
		T[v].push_back(make_pair(u,w));
	}
	for(int i=1;i<=n;i++)id[i]=1;
	dfs1(1,0);
	for(int i=1;i<=n;i++)b1[i]=n;
	dfs2(1,0,0);
//	for(int i=1;i<=n;i++)printf("%d ",sz[i]);printf("\n");
//	for(int i=1;i<=n;i++)printf("%d ",b1[i]);printf("\n");
//	for(int i=1;i<=n;i++)printf("%d ",b[i]);printf("\n");
	long long ans=0;
	for(int i=1;i<=n;i++){
		long long sum=1LL*n*n;
//		for(int j=0;j<c[i].size();j++)printf("%d ",c[i][j]);printf("\n");
		for(int j=0;j<c[i].size();j++)sum-=1LL*c[i][j]*c[i][j];
		ans+=sum;
	}
	printf("%lld",ans);
	return 0;
	//quod erat demonstrandum
}

Details

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

Test #1:

score: 10
Accepted
time: 3ms
memory: 24680kb

input:

300
2 1 15
3 1 10
4 1 1
5 3 2
6 1 8
7 1 16
8 6 17
9 5 16
10 6 3
11 2 16
12 10 7
13 12 8
14 9 2
15 7 ...

output:

995128

result:

ok single line: '995128'

Test #2:

score: 10
Accepted
time: 0ms
memory: 24684kb

input:

300
2 1 8
3 2 16
4 2 8
5 3 1
6 2 16
7 5 12
8 5 10
9 1 8
10 4 5
11 8 6
12 8 17
13 1 3
14 2 16
15 7 2
...

output:

1003386

result:

ok single line: '1003386'

Test #3:

score: 10
Accepted
time: 4ms
memory: 24684kb

input:

300
2 1 15
3 1 15
4 2 8
5 4 4
6 3 5
7 6 1
8 3 4
9 2 5
10 1 4
11 2 1
12 9 7
13 10 3
14 7 4
15 10 6
16...

output:

979308

result:

ok single line: '979308'

Test #4:

score: 10
Accepted
time: 22ms
memory: 32472kb

input:

50000
1 2 142
2 3 135
3 4 103
4 5 17
5 6 13
6 7 149
7 8 4
8 9 206
9 10 40
10 11 133
11 12 173
12 13 ...

output:

552565399476

result:

ok single line: '552565399476'

Test #5:

score: 10
Accepted
time: 20ms
memory: 32476kb

input:

50000
1 2 211
2 3 135
3 4 121
4 5 90
5 6 154
6 7 37
7 8 190
8 9 126
9 10 106
10 11 126
11 12 174
12 ...

output:

552552414980

result:

ok single line: '552552414980'

Test #6:

score: 10
Accepted
time: 11ms
memory: 29028kb

input:

50000
1 2 56
1 3 200
1 4 109
1 5 132
1 6 178
1 7 132
1 8 46
1 9 27
1 10 29
1 11 118
1 12 217
1 13 10...

output:

4988587626

result:

ok single line: '4988587626'

Test #7:

score: 10
Accepted
time: 12ms
memory: 29024kb

input:

50000
1 2 15
1 3 164
1 4 210
1 5 3
1 6 192
1 7 134
1 8 76
1 9 118
1 10 148
1 11 204
1 12 220
1 13 16...

output:

4988587498

result:

ok single line: '4988587498'

Test #8:

score: 10
Accepted
time: 15ms
memory: 29308kb

input:

50000
2 1 75
3 2 141
4 1 111
5 1 133
6 2 146
7 5 201
8 7 32
9 6 46
10 6 130
11 9 27
12 5 51
13 1 133...

output:

509207879442

result:

ok single line: '509207879442'

Test #9:

score: 10
Accepted
time: 190ms
memory: 72340kb

input:

500000
2 1 692
3 1 414
4 3 348
5 3 162
6 3 334
7 6 589
8 6 314
9 3 309
10 2 380
11 3 620
12 2 418
13...

output:

171527774112372

result:

ok single line: '171527774112372'

Test #10:

score: 10
Accepted
time: 206ms
memory: 72340kb

input:

500000
2 1 625
3 1 678
4 1 636
5 4 289
6 2 328
7 5 683
8 3 116
9 4 281
10 8 58
11 9 378
12 8 279
13 ...

output:

171458440760230

result:

ok single line: '171458440760230'