QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#109018#5418. Color the TreewhyWA 7ms1740kbC++172.2kb2023-05-27 09:15:472023-05-27 09:15:49

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-27 09:15:49]
  • 评测
  • 测评结果:WA
  • 用时:7ms
  • 内存:1740kb
  • [2023-05-27 09:15:47]
  • 提交

answer

#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+86;
const ll INF=0x3f3f3f3f3f3f3f3f;

int T,n;
ll a[N],b[N];
struct edge{int to,nxt;}e[N<<1];
int head[N],tot;
void add(int u,int v){e[++tot]={v,head[u]},head[u]=tot;}

struct node{int hson,dep,mdep;}p[N];
void dfs1(int u,int fa)
{
    p[u].hson=0;
    p[u].dep=p[fa].dep+1;
    p[u].mdep=p[u].dep;
    for(int i=head[u];i;i=e[i].nxt)
    {
        int v=e[i].to;
        if(v==fa) continue;
        dfs1(v,u);
        p[u].mdep=max(p[u].mdep,p[v].mdep);
        if(!p[u].hson||p[p[u].hson].mdep<p[v].mdep) p[u].hson=v;
    }
}

ll f[N][50],cnt=1;
void dfs2(int u,int fa,int top)
{//printf("%d\n",u);
    if(p[u].hson) dfs2(p[u].hson,u,top);
    for(int i=head[u];i;i=e[i].nxt)
    {
        int v=e[i].to;
        if(v==fa||v==p[u].hson) continue;
        cnt++;
        for(int j=0;j<=p[v].mdep-p[v].dep;j++)
            f[cnt][j]=0;
        // memset(f[cnt],0,(p[v].mdep-p[v].dep+1)<<3);
        dfs2(v,u,v);
        for(int j=0;j<=p[v].mdep-p[v].dep;j++)
            f[cnt-1][p[v].dep-p[top].dep+j]=min(a[p[v].dep-p[u].dep+j],f[cnt-1][p[v].dep-p[top].dep+j]+f[cnt][j]);//printf("%d %d %lld\n",u,p[v].dep-p[top].dep+j,f[cnt-1][p[v].dep-p[top].dep+j]);
        cnt--;
    }
    f[cnt][p[u].dep-p[top].dep]=b[p[u].dep-p[top].dep];
}

template<typename T>
inline void read(T &x)
{
    T k=1;char ch=getchar();x=0;
    while(ch<'0'||ch>'9'){if(ch=='-') k=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    x*=k;
}

signed main()
{
    read(T);
    while(T--)
    {
        read(n);
        memset(head+1,0,n<<2);
        tot=0;
        cnt=1;
        for(int i=0;i<=n-1;i++)
            read(a[i]),b[i]=min(a[i],i>0?b[i-1]:INF);
        for(int i=1,u,v;i<=n-1;i++)
            read(u),read(v),add(u,v),add(v,u);
        dfs1(1,0);
        dfs2(1,0,1);
        ll sum=0;
        for(int i=0;i<=p[1].mdep-1;i++)
            f[1][i]=min(f[1][i],a[i]),sum+=f[1][i];
        printf("%lld\n",sum);
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 1740kb

input:

3
4
10 15 40 1
1 2
2 3
2 4
5
10 5 1 100 1000
1 2
2 3
2 4
4 5
4
1000 200 10 8
1 2
2 3
3 4

output:

35
17
1218

result:

ok 3 number(s): "35 17 1218"

Test #2:

score: -100
Wrong Answer
time: 7ms
memory: 1496kb

input:

3000
54
43 44 11 49 17 14 7 30 35 12 34 14 15 8 29 47 30 31 39 17 26 23 26 45 34 50 49 13 35 18 29 15 13 44 47 5 22 20 19 46 30 22 26 13 47 46 43 27 48 48 13 14 30 44
1 2
1 3
2 4
3 5
4 6
2 7
4 8
1 9
3 10
1 11
8 12
2 13
9 14
1 15
1 16
15 17
15 18
7 19
1 20
19 21
13 22
10 23
17 24
9 25
9 26
24 27
8 28...

output:

180
179
222
230
177
318
254
126
110
81
165
73
159
127
174
128
266
230
132
249
153
171
80
282
262
293
195
211
119
199
211
233
88
299
239
238
192
182
195
146
109
178
180
217
268
210
182
97
245
59
56
31
115
221
203
172
139
225
53
140
189
187
175
137
233
101
200
273
80
350
160
133
189
159
252
283
137
22...

result:

wrong answer 2nd numbers differ - expected: '168', found: '179'