QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#748360#9110. Zayin and TreeOOBMABTRAMS#AC ✓185ms40004kbC++171.1kb2024-11-14 20:09:332024-11-14 20:09:34

Judging History

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

  • [2024-11-14 20:09:34]
  • 评测
  • 测评结果:AC
  • 用时:185ms
  • 内存:40004kb
  • [2024-11-14 20:09:33]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1000013;
const int P=998244353;
int dp[N][4],a[N];
int ans;
vector<int>mp[N];

void dfs(int x,int fa) {
    int nw[4]={1,1-a[x],1+a[x],1};
    int g[4]={1,1-a[x],1+a[x],1};
    for(auto i:mp[x])if(i^fa) {
        dfs(i,x);
        //nw+i->ans
        for(int j:{0,1,2,3})ans=min(ans,nw[3^j]+dp[i][j]);
        //{x}+nw->nw
        nw[0]=min(nw[0],dp[i][0]+g[0]);
        nw[1]=min({nw[1],dp[i][1]+g[0],dp[i][0]+g[1]});
        nw[2]=min({nw[2],dp[i][2]+g[0],dp[i][0]+g[2]});
        nw[3]=min({nw[3],dp[i][3]+g[0],dp[i][0]+g[3],dp[i][1]+g[2],dp[i][2]+g[1]});
    }
    for(int j:{0,1,2,3})dp[x][j]=nw[j];
    ans=min(ans,dp[x][3]);
}

void solve(){
    ans=1e9;
    int n;cin>>n;
    for(int i=1;i<=n;i++)mp[i].clear();
    for(int i=1;i<=n;i++)cin>>a[i];
    for(int i=1;i<=n;i++)for(int j=0;j<4;j++)dp[i][j]=1e9;
    for(int i=1,x,y;i<n;i++)cin>>x>>y,mp[x].push_back(y),mp[y].push_back(x);
    dfs(1,0);
    cout<<ans<<'\n';
}
int main(){
    ios::sync_with_stdio(false);
    int T=1;
    cin>>T;
    while(T--)solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 185ms
memory: 40004kb

input:

3009
5
4 5 3 4 2
1 2
2 3
3 4
3 5
5
4 4 1 1 2
1 2
2 3
3 4
3 5
10
5 8 1 0 8 7 5 2 0 4
2 4
3 8
3 9
1 2
1 3
3 6
4 5
5 7
6 10
10
6 8 8 4 8 0 6 6 0 2
7 10
1 7
2 9
2 3
3 4
1 5
1 6
6 8
1 2
10
9 0 4 0 4 6 0 2 0 0
1 5
1 3
1 7
2 6
1 2
1 9
1 4
5 8
7 10
10
8 8 1 2 7 4 8 6 0 8
1 6
1 7
1 5
7 9
1 3
1 2
2 10
3 4
1 8...

output:

0
-1
-6
-6
-7
-6
-7
-4
-3
-7
-5
-6
-5
-4
-6
-3
-4
-7
-4
-4
-6
-6
-6
-5
-4
-5
-6
-6
-7
-7
-5
-7
-6
-6
-7
-6
-5
-5
-4
-6
-6
-5
-6
-6
-6
-6
-3
-6
-3
-6
-4
-6
-7
-6
-7
-6
-6
-5
-7
-6
-4
-7
-3
-5
-5
-6
-4
-5
-7
-6
-5
-5
-4
-3
-5
-3
-4
-2
-6
-5
-7
-4
-5
-5
-7
-7
-4
-6
-5
-4
-6
-5
-5
-6
-3
-6
-7
-7
-7
-6
-...

result:

ok 3009 lines