QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#761962#6848. City UpgradingetherealCaOAC ✓68ms14348kbC++171.0kb2024-11-19 11:55:202024-11-19 11:55:21

Judging History

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

  • [2024-11-19 11:55:21]
  • 评测
  • 测评结果:AC
  • 用时:68ms
  • 内存:14348kb
  • [2024-11-19 11:55:20]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=4e5+10;
int t,n;
int e[N],ne[N],h[N],idx,w[N];
bool st[N];
ll f[N][3];//i,0为父亲  i,1为儿子   i,2为自己 
void add(int a,int b)
{
	e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
void dfs(int u)
{
	f[u][2]=w[u];
//	cout<<w[u]<<endl;
	f[u][1]=1e11;
	for(int i=h[u];~i;i=ne[i])
	{
		int j=e[i];
		dfs(j);
		f[u][0]+=min(f[j][1],f[j][2]);
		f[u][2]+=min(min(f[j][1],f[j][0]),f[j][2]);
	}
	for(int i=h[u];~i;i=ne[i])
	{
		int j=e[i];
		f[u][1]=min(f[u][1],f[u][0]+f[j][2]-min(f[j][1],f[j][2]));
	}
}
int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(int i=1;i<=n;i++)
		{
			h[i]=-1;
			st[i]=f[i][1]=f[i][2]=f[i][0]=0;
		}
		idx=0;
		for(int i=1;i<=n;i++)cin>>w[i];
		for(int i=1;i<n;i++)
		{
			int a,b;
			cin>>a>>b;
			add(a,b);
			st[b]=1;
		}
		int root=1;
		while(st[root])root++;	
	//	cout<<root<<endl;
		dfs(root);
		cout<<min(f[root][1],f[root][2])<<endl; 
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 68ms
memory: 14348kb

input:

1000
23
97976 19679 92424 30861 84737 62896 66360 54204 29129 13621 23631 61405 66883 53853 23079 66231 77727 88749 71092 97425 85117 79396 67781
1 2
2 3
1 4
1 5
5 6
1 7
5 8
3 9
9 10
5 11
8 12
9 13
3 14
4 15
6 16
9 17
8 18
3 19
8 20
11 21
3 22
19 23
3
93601 96295 41363
1 2
2 3
26
19405 8067 19601 81...

output:

419504
96295
334958
636478
114964
628081
464114
560260
479222
121326
64291
607551
278318
56546
413182
159607
313038
362098
635804
380900
594905
358972
325402
765893
705879
755158
206101
49049
7285
244319
208205
77015
623675
348208
515431
96136
607270
610292
656473
119999
320041
403718
80158
141749
4...

result:

ok 1000 lines