QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#760581#6848. City UpgradingetherealCaOWA 208ms6208kbC++141.2kb2024-11-18 17:42:022024-11-18 17:42:03

Judging History

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

  • [2024-11-18 17:42:03]
  • 评测
  • 测评结果:WA
  • 用时:208ms
  • 内存:6208kb
  • [2024-11-18 17:42:02]
  • 提交

answer

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;

const int N=1e5+10;

int n,m;
int h[N],e[N],ne[N],idx,w[N];
int f[N][2];
bool st[N];

void add(int a,int b)
{
    e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
void dfs(int u)
{
    f[u][0]=0;
    f[u][1]=w[u];
    for(int i=h[u];~i;i=ne[i])
    {
        int j=e[i];
        dfs(j);
        f[u][0]+=f[j][1];
        f[u][1]+=min(f[j][1],f[j][0]);
    }

}
int main()
{
	int T;
	cin>>T;
    while(T--)
    {
    	cin>>n;
        memset(h,-1,sizeof h);
        for(int i=1;i<=n;i++)
        	cin>>w[i];
        idx=0;
        memset(st,0,sizeof st);
		int id,ver;
        for(int i=1;i<n;i++)
        {	
			cin>>id>>ver;
            add(id,ver);
            st[ver]=true;
        }
        int root=1;
        while(st[root])root++;
//        cout<<root<<endl;
        dfs(root);
//        cout<<f[root][0]<<f[root][1]<<endl;
        printf("%d\n",min(f[root][0],f[root][1]));
    }
    return 0;
}

//作者:etherealCaO
//链接:https://www.acwing.com/activity/content/code/content/8916094/
//来源:AcWing
//著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 208ms
memory: 6208kb

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:

451120
96295
393584
678918
114964
796814
512238
627433
626098
121326
90136
690477
278318
56546
456930
170095
362551
413611
825535
399702
766891
358972
422444
932206
832716
789705
211982
49049
7285
267080
237411
101498
785365
463372
573516
0
787463
713667
671263
119999
406471
529347
80158
193760
6236...

result:

wrong answer 1st lines differ - expected: '419504', found: '451120'