Submission #52098250


Source Code Expand

Copy
#include<iostream>
#include<string>
#include<queue>
#include<vector>
#include<cassert>
#include<random>
#include<set>
#include<map>
#include<cassert>
using namespace std;
typedef long long ll;
const int inf=1<<30;
const ll INF=1LL<<62;
typedef pair<int,int> P;
typedef pair<int,P> PP;
const int mod = 998244353;
struct mint {
long long x; // typedef long long long long;
mint(long long x=0):x((x%mod+mod)%mod){}
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#include<iostream>
#include<string>
#include<queue>
#include<vector>
#include<cassert>
#include<random>
#include<set>
#include<map>
#include<cassert>
using namespace std;
typedef long long ll;
const int inf=1<<30;
const ll INF=1LL<<62;
typedef pair<int,int> P;
typedef pair<int,P> PP; 


const int mod = 998244353;
struct mint {
  long long x; // typedef long long long long;
  mint(long long x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
  mint operator+(const mint a) const { return mint(*this) += a;}//後ろのconstはメンバxを変更しないことを示す
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= a;}
  bool operator==(const mint a) const {return a.x==x;}
  mint pow(long long t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) const { return mint(*this) /= a;}
};

std::istream& operator>>(std::istream& is, mint& a) { return is >> a.x;}
std::ostream& operator<<(std::ostream& os, const mint& a) { return os << a.x;}



struct combination {
  std::vector<mint> fact, ifact;
  combination(int n):fact(n+1),ifact(n+1) {
    assert(n < mod);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n) return 0;
    return fact[n]*ifact[k]*ifact[n-k];
  }
};


const int dx[]={-1,0,1,0};
const int dy[]={0,-1,0,1};


int main(){
    int N;
    cin>>N;
    vector<int> A(N-1),B(N-1);
    vector<vector<int>> G(N);
    for(int i=0;i<N-1;i++){
        cin>>A[i]>>B[i];
        A[i]--;B[i]--;
        G[A[i]].push_back(B[i]);
        G[B[i]].push_back(A[i]);
    }
    ll sumC=0;
    vector<ll> C(N);
    for(int i=0;i<N;i++){
        cin>>C[i];
        sumC+=C[i];
    }

    vector<int> dist0(N,inf);

    {
        queue<int> que;
        que.push(0);
        dist0[0]=0;
        while(!que.empty()){
            int now=que.front();
            que.pop();
            for(int to:G[now]){
                if(dist0[to]==inf){
                    dist0[to]=dist0[now]+1;
                    que.push(to);
                }
            }
        }
    }

    vector<ll> val(N,0);
    vector<int> num(N,0);

    auto dfs0=[&](auto f,int now,int pre)->void{
        
        for(int to:G[now]){
            if(to==pre) continue;
            f(f,to,now);
            val[now]+=val[to];
            num[now]+=num[to];
        }
        val[now]+=C[now];
        num[now]+=1; //自分自身
    };

    dfs0(dfs0,0,-1);

    ll score0=0;
    for(int i=0;i<N;i++){
        score0+=dist0[i]*C[i];
    }

    vector<ll> ans(N);

    auto dfs=[&](auto f,int now,int pre,ll score)->void{

        ans[now]=score;

        for(int to:G[now]){
            if(to==pre) continue;
            ll to_score=0;
            to_score+=score;
            to_score-=val[to];
            to_score+=(sumC-val[to]);
            f(f,to,now,to_score);
        }
    };

    dfs(dfs,0,-1,score0);
    ll final_ans=INF;
    for(int i=0;i<N;i++){
        //cout<<"ans["<<i<<"]="<<ans[i]<<endl;
        final_ans=min(final_ans,ans[i]);
    }
    cout<<final_ans<<endl;

}

Submission Info

Submission Time
Task E - Minimize Sum of Distances
User HIcoder
Language C++ 20 (gcc 12.2)
Score 475
Code Size 3848 Byte
Status AC
Exec Time 137 ms
Memory 18868 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 475 / 475
Status
AC × 3
AC × 31
Set Name Test Cases
Sample example0.txt, example1.txt, example2.txt
All example0.txt, example1.txt, example2.txt, test_00.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt, test_22.txt, test_23.txt, test_24.txt, test_25.txt, test_26.txt, test_27.txt
Case Name Status Exec Time Memory
example0.txt AC 1 ms 3584 KB
example1.txt AC 1 ms 3480 KB
example2.txt AC 1 ms 3468 KB
test_00.txt AC 52 ms 9188 KB
test_01.txt AC 34 ms 7020 KB
test_02.txt AC 61 ms 10208 KB
test_03.txt AC 14 ms 4992 KB
test_04.txt AC 53 ms 10536 KB
test_05.txt AC 31 ms 7316 KB
test_06.txt AC 40 ms 9280 KB
test_07.txt AC 37 ms 9712 KB
test_08.txt AC 29 ms 7664 KB
test_09.txt AC 86 ms 12624 KB
test_10.txt AC 69 ms 12796 KB
test_11.txt AC 84 ms 12704 KB
test_12.txt AC 69 ms 12660 KB
test_13.txt AC 85 ms 12664 KB
test_14.txt AC 71 ms 12560 KB
test_15.txt AC 71 ms 12924 KB
test_16.txt AC 54 ms 12856 KB
test_17.txt AC 75 ms 12864 KB
test_18.txt AC 59 ms 12840 KB
test_19.txt AC 76 ms 12920 KB
test_20.txt AC 58 ms 13008 KB
test_21.txt AC 134 ms 18388 KB
test_22.txt AC 124 ms 18868 KB
test_23.txt AC 118 ms 17120 KB
test_24.txt AC 110 ms 16256 KB
test_25.txt AC 137 ms 17548 KB
test_26.txt AC 118 ms 17812 KB
test_27.txt AC 1 ms 3472 KB


2025-04-08 (Tue)
13:41:10 +00:00