QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#175849#6514. Is it well known in Poland?ucup-team134TL 1ms6484kbC++141.9kb2023-09-11 02:48:072023-09-11 02:48:07

Judging History

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

  • [2023-09-11 02:48:07]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:6484kb
  • [2023-09-11 02:48:07]
  • 提交

answer

#include<bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define ll long long
using namespace std;
typedef pair<ll,int> pii;

const int mod=998244353;
inline int add(int x,int y){int ret=x+y;if(ret>=mod)ret-=mod;return ret;}
inline int sub(int x,int y){int ret=x-y;if(ret<0)ret+=mod;return ret;}
inline int mul(int x,int y){return ((ll)x*y)%mod;}
inline int step(int base,int pw){int ret=1;while(pw){if(pw&1)ret=mul(ret,base);base=mul(base,base);pw>>=1;}return ret;}
inline int invv(int x){return step(x,mod-2);}

const int maxn=1e5+10;
int pos[maxn],par[maxn];
ll a[maxn];
vector<int>vect[maxn];
void prek(int x,int prv){

    par[x]=prv;
    for(int i=0;i<vect[x].size();i++){
        int id=vect[x][i];
        if(id==prv)continue;
        prek(id,x);
    }
}
int main(){


    ///freopen("test.txt","r",stdin);

    int n;
    scanf("%d",&n);
    ll sum=0;
    for(int i=1;i<=n;i++){
        scanf("%lld",&a[i]);
        sum+=a[i];
    }
    for(int i=1;i<n;i++){
        int  u,v;
        scanf("%d %d",&u,&v);
        vect[u].pb(v);
        vect[v].pb(u);
    }

    prek(1,0);

    pos[0]=1;
    set<pii>st;
    for(int i=1;i<=n;i++){
        st.insert({-a[i],i});
    }

    ll rez=0;
    ll coef=1;
    while(st.size()){

        int x=(*st.begin()).ss;
        st.erase(st.begin());
        if(pos[x])continue;

        if(pos[par[x]]){
            rez+=coef*a[x];
            coef*=-1;
            pos[x]=1;
        }
        else{

            int y=par[x];
            st.erase({-a[y],y});
            a[y]-=a[x];
            st.insert({-a[y],y});
            pos[x]=1;

            for(int j=0;j<vect[x].size();j++){
                int id=vect[x][j];
                if(pos[id])continue;
                vect[y].pb(id);
                par[id]=y;
            }

        }

    }

    printf("%lld\n",(rez+sum)/2);

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

5
1 5 3 2 4
1 2
1 3
2 4
2 5

output:

7

result:

ok 1 number(s): "7"

Test #2:

score: -100
Time Limit Exceeded

input:

20
530933472 920863930 136179483 46535289 291417568 338674057 731327836 375973098 986104110 203163644 489326483 785212564 712578139 801609721 666347686 282530991 823910542 217884304 785578553 116701831
8 3
18 19
11 20
2 18
8 13
5 15
12 16
7 8
10 9
13 6
17 10
17 18
13 15
18 4
13 12
1 14
17 15
15 14
5...

output:


result: