Language: C++

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

struct edge
{
    int to;
    ll cost;
};

vector<int> G[MAX_N];
vector<edge> graph[MAX_N];
int par[MAX_N];
int st[MAX_N];
ll sma;

void dfs(int u,int p)
{
    par[u] = p;
    st[u] = 1;
    rep(i,len(G[u])){
        if(G[u][i] != p){
            dfs(G[u][i],u);
            st[u] += st[G[u][i]];
        }
    }
}

void redfs(int u,int p,ll dist)
{
    sma += dist;
    rep(i,len(graph[u])){
        if(graph[u][i].to != p){
            redfs(graph[u][i].to,u,dist+graph[u][i].cost);
        }
    }
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vp vec(n-1);
    rep(i,n-1){
        int a,b;
        cin >> a >> b;
        --a,--b;
        vec[i] = P(a,b);
        G[a].pb(b),G[b].pb(a);
    }
    vl sm(n);
    rep(i,n){
        cin >> sm[i];
    }
    dfs(0,-1);
    vl ans(n-1);
    int flag = -1;
    bool hoge = false;
    rep(i,n-1){
        int a = vec[i].fi,b = vec[i].se;
        bool cri = false;
        if(par[a] != b){
            cri = true;
            swap(a,b);
        }
        if(n == 2*st[a]){
            hoge = true;
            flag = i;
            continue;
        }
        ans[i] = (sm[a]-sm[b])/(n-2*st[a]);
    }
    if(flag >= 0){
        rep(i,n-1){
            int a = vec[i].fi,b = vec[i].se;
            if(flag == i){
                graph[a].pb((edge){b,0}),graph[b].pb((edge){a,0});
            }else{
                graph[a].pb((edge){b,ans[i]}),graph[b].pb((edge){a,ans[i]});
            }
        }
        sma = 0;
        if(hoge){
            redfs(vec[flag].fi,-1,0);
            ans[flag] = (sm[vec[flag].fi]-sma) / st[vec[flag].se];
        }else{
            redfs(vec[flag].se,-1,0);
            ans[flag] = (sm[vec[flag].se]-sma) / st[vec[flag].fi];
        }
    }
    rep(i,n-1){
        cout << ans[i] << "\n";
    }
    return 0;
}
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

struct edge
{
    int to;
    ll cost;
};

vector<int> G[MAX_N];
vector<edge> graph[MAX_N];
int par[MAX_N];
int st[MAX_N];
ll sma;

void dfs(int u,int p)
{
    par[u] = p;
    st[u] = 1;
    rep(i,len(G[u])){
        if(G[u][i] != p){
            dfs(G[u][i],u);
            st[u] += st[G[u][i]];
        }
    }
}

void redfs(int u,int p,ll dist)
{
    sma += dist;
    rep(i,len(graph[u])){
        if(graph[u][i].to != p){
            redfs(graph[u][i].to,u,dist+graph[u][i].cost);
        }
    }
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vp vec(n-1);
    rep(i,n-1){
        int a,b;
        cin >> a >> b;
        --a,--b;
        vec[i] = P(a,b);
        G[a].pb(b),G[b].pb(a);
    }
    vl sm(n);
    rep(i,n){
        cin >> sm[i];
    }
    dfs(0,-1);
    vl ans(n-1);
    int flag = -1;
    bool hoge = false;
    rep(i,n-1){
        int a = vec[i].fi,b = vec[i].se;
        bool cri = false;
        if(par[a] != b){
            cri = true;
            swap(a,b);
        }
        if(n == 2*st[a]){
            hoge = true;
            flag = i;
            continue;
        }
        ans[i] = (sm[a]-sm[b])/(n-2*st[a]);
    }
    if(flag >= 0){
        rep(i,n-1){
            int a = vec[i].fi,b = vec[i].se;
            if(flag == i){
                graph[a].pb((edge){b,0}),graph[b].pb((edge){a,0});
            }else{
                graph[a].pb((edge){b,ans[i]}),graph[b].pb((edge){a,ans[i]});
            }
        }
        sma = 0;
        if(hoge){
            redfs(vec[flag].fi,-1,0);
            ans[flag] = (sm[vec[flag].fi]-sma) / st[vec[flag].se];
        }else{
            redfs(vec[flag].se,-1,0);
            ans[flag] = (sm[vec[flag].se]-sma) / st[vec[flag].fi];
        }
    }
    rep(i,n-1){
        cout << ans[i] << "\n";
    }
    return 0;
}
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

struct edge
{
    int to;
    ll cost;
};

vector<int> G[MAX_N];
vector<edge> graph[MAX_N];
int par[MAX_N];
int st[MAX_N];
ll sma;

void dfs(int u,int p)
{
    par[u] = p;
    st[u] = 1;
    rep(i,len(G[u])){
        if(G[u][i] != p){
            dfs(G[u][i],u);
            st[u] += st[G[u][i]];
        }
    }
}

void redfs(int u,int p,ll dist)
{
    sma += dist;
    rep(i,len(graph[u])){
        if(graph[u][i].to != p){
            redfs(graph[u][i].to,u,dist+graph[u][i].cost);
        }
    }
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vp vec(n-1);
    rep(i,n-1){
        int a,b;
        cin >> a >> b;
        --a,--b;
        vec[i] = P(a,b);
        G[a].pb(b),G[b].pb(a);
    }
    vl sm(n);
    rep(i,n){
        cin >> sm[i];
    }
    dfs(0,-1);
    vl ans(n-1);
    int flag = -1;
    bool hoge = false;
    rep(i,n-1){
        int a = vec[i].fi,b = vec[i].se;
        bool cri = false;
        if(par[a] != b){
            cri = true;
            swap(a,b);
        }
        if(n == 2*st[a]){
            hoge = true;
            flag = i;
            continue;
        }
        ans[i] = (sm[a]-sm[b])/(n-2*st[a]);
    }
    if(flag >= 0){
        rep(i,n-1){
            int a = vec[i].fi,b = vec[i].se;
            if(flag == i){
                graph[a].pb((edge){b,0}),graph[b].pb((edge){a,0});
            }else{
                graph[a].pb((edge){b,ans[i]}),graph[b].pb((edge){a,ans[i]});
            }
        }
        sma = 0;
        if(hoge){
            redfs(vec[flag].fi,-1,0);
            ans[flag] = (sm[vec[flag].fi]-sma) / st[vec[flag].se];
        }else{
            redfs(vec[flag].se,-1,0);
            ans[flag] = (sm[vec[flag].se]-sma) / st[vec[flag].fi];
        }
    }
    rep(i,n-1){
        cout << ans[i] << "\n";
    }
    return 0;
}
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

struct edge
{
    int to;
    ll cost;
};

vector<int> G[MAX_N];
vector<edge> graph[MAX_N];
int par[MAX_N];
int st[MAX_N];
ll sma;

void dfs(int u,int p)
{
    par[u] = p;
    st[u] = 1;
    rep(i,len(G[u])){
        if(G[u][i] != p){
            dfs(G[u][i],u);
            st[u] += st[G[u][i]];
        }
    }
}

void redfs(int u,int p,ll dist)
{
    sma += dist;
    rep(i,len(graph[u])){
        if(graph[u][i].to != p){
            redfs(graph[u][i].to,u,dist+graph[u][i].cost);
        }
    }
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vp vec(n-1);
    rep(i,n-1){
        int a,b;
        cin >> a >> b;
        --a,--b;
        vec[i] = P(a,b);
        G[a].pb(b),G[b].pb(a);
    }
    vl sm(n);
    rep(i,n){
        cin >> sm[i];
    }
    dfs(0,-1);
    vl ans(n-1);
    int flag = -1;
    bool hoge = false;
    rep(i,n-1){
        int a = vec[i].fi,b = vec[i].se;
        bool cri = false;
        if(par[a] != b){
            cri = true;
            swap(a,b);
        }
        if(n == 2*st[a]){
            hoge = true;
            flag = i;
            continue;
        }
        ans[i] = (sm[a]-sm[b])/(n-2*st[a]);
    }
    if(flag >= 0){
        rep(i,n-1){
            int a = vec[i].fi,b = vec[i].se;
            if(flag == i){
                graph[a].pb((edge){b,0}),graph[b].pb((edge){a,0});
            }else{
                graph[a].pb((edge){b,ans[i]}),graph[b].pb((edge){a,ans[i]});
            }
        }
        sma = 0;
        if(hoge){
            redfs(vec[flag].fi,-1,0);
            ans[flag] = (sm[vec[flag].fi]-sma) / st[vec[flag].se];
        }else{
            redfs(vec[flag].se,-1,0);
            ans[flag] = (sm[vec[flag].se]-sma) / st[vec[flag].fi];
        }
    }
    rep(i,n-1){
        cout << ans[i] << "\n";
    }
    return 0;
}


Compiler output:
a.cc:166:11: error: redefinition of 'const int MAX_N'
  166 | const int MAX_N = 100005;
      |           ^~~~~
a.cc:36:11: note: 'const int MAX_N' previously defined here
   36 | const int MAX_N = 100005;
      |           ^~~~~
a.cc:168:8: error: redefinition of 'struct edge'
  168 | struct edge
      |        ^~~~
a.cc:38:8: note: previous definition of 'struct edge'
   38 | struct edge
      |        ^~~~
a.cc:174:13: error: redefinition of 'std::vector<int> G [100005]'
  174 | vector<int> G[MAX_N];
      |             ^
a.cc:44:13: note: 'std::vector<int> G [100005]' previously declared here
   44 | vector<int> G[MAX_N];
      |             ^
a.cc:175:14: error: redefinition of 'std::vector<edge> graph [100005]'
  175 | vector<edge> graph[MAX_N];
      |              ^~~~~
a.cc:45:14: note: 'std::vector<edge> graph [100005]' previously declared here
   45 | vector<edge> graph[MAX_N];
      |              ^~~~~
a.cc:176:5: error: redefinition of 'int par [100005]'
  176 | int par[MAX_N];
      |     ^~~
a.cc:46:5: note: 'int par [100005]' previously declared here
   46 | int par[MAX_N];
      |     ^~~
a.cc:177:5: error: redefinition of 'int st [100005]'
  177 | int st[MAX_N];
      |     ^~
a.cc:47:5: note: 'int st [100005]' previously declared here
   47 | int st[MAX_N];
      |     ^~
a.cc:178:4: error: redefinition of 'long long int sma'
  178 | ll sma;
      |    ^~~
a.cc:48:4: note: 'long long int sma' previously declared here
   48 | ll sma;
      |    ^~~
a.cc:180:6: error: redefinition of 'void dfs(int, int)'
  180 | void dfs(int u,int p)
      |      ^~~
a.cc:50:6: note: 'void dfs(int, int)' previously defined here
   50 | void dfs(int u,int p)
      |      ^~~
a.cc:192:6: error: redefinition of 'void redfs(int, int, long long int)'
  192 | void redfs(int u,int p,ll dist)
      |      ^~~~~
a.cc:62:6: note: 'void redfs(int, int, long long int)' previously defined here
   62 | void redfs(int u,int p,ll dist)
      |      ^~~~~
a.cc:202:5: error: redefinition of 'int main()'
  202 | int main()
      |     ^~~~
a.cc:72:5: note: 'int main()' previously defined here
   72 | int main()
      |     ^~~~
a.cc:296:11: error: redefinition of 'const int MAX_N'
  296 | const int MAX_N = 100005;
      |           ^~~~~
a.cc:36:11: note: 'const int MAX_N' previously defined here
   36 | const int MAX_N = 100005;
      |           ^~~~~
a.cc:298:8: error: redefinition of 'struct edge'
  298 | struct edge
      |        ^~~~
a.cc:38:8: note: previous definition of 'struct edge'
   38 | struct edge
      |        ^~~~
a.cc:304:13: error: redefinition of 'std::vector<int> G [100005]'
  304 | vector<int> G[MAX_N];
      |             ^
a.cc:44:13: note: 'std::vector<int> G [100005]' previously declared here
   44 | vector<int> G[MAX_N];
      |             ^
a.cc:305:14: error: redefinition of 'std::vector<edge> graph [100005]'
  305 | vector<edge> graph[MAX_N];
      |              ^~~~~
a.cc:45:14: note: 'std::vector<edge> graph [100005]' previously declared here
   45 | vector<edge> graph[MAX_N];
      |              ^~~~~
a.cc:306:5: error: redefinition of 'int par [100005]'
  306 | int par[MAX_N];
      |     ^~~
a.cc:46:5: note: 'int par [100005]' previously declared here
   46 | int par[MAX_N];
      |     ^~~
a.cc:307:5: error: redefinition of 'int st [100005]'
  307 | int st[MAX_N];
      |     ^~
a.cc:47:5: note: 'int st [100005]' previously declared here
   47 | int st[MAX_N];
      |     ^~
a.cc:308:4: error: redefinition of 'long long int sma'
  308 | ll sma;
      |    ^~~
a.cc:48:4: note: 'long long int sma' previously declared here
   48 | ll sma;
      |    ^~~
a.cc:310:6: error: redefinition of 'void dfs(int, int)'
  310 | void dfs(int u,int p)
      |      ^~~
a.cc:50:6: note: 'void dfs(int, int)' previously defined here
   50 | void dfs(int u,int p)
      |      ^~~
a.cc:322:6: error: redefinition of 'void redfs(int, int, long long int)'
  322 | void redfs(int u,int p,ll dist)
      |      ^~~~~
a.cc:62:6: note: 'void redfs(int, int, long long int)' previously defined here
   62 | void redfs(int u,int p,ll dist)
      |      ^~~~~
a.cc:332:5: error: redefinition of 'int main()'
  332 | int main()
      |     ^~~~
a.cc:72:5: note: 'int main()' previously defined here
   72 | int main()
      |     ^~~~
a.cc:426:11: error: redefinition of 'const int MAX_N'
  426 | const int MAX_N = 100005;
      |           ^~~~~
a.cc:36:11: note: 'const int MAX_N' previously defined here
   36 | const int MAX_N = 100005;
      |           ^~~~~
a.cc:428:8: error: redefinition of 'struct edge'
  428 | struct edge
      |        ^~~~
a.cc:38:8: note: previous definition of 'struct edge'
   38 | struct edge
      |        ^~~~
a.cc:434:13: error: redefinition of 'std::vector<int> G [100005]'
  434 | vector<int> G[MAX_N];
      |             ^
a.cc:44:13: note: 'std::vector<int> G [100005]' previously declared here
   44 | vector<int> G[MAX_N];
      |             ^
a.cc:435:14: error: redefinition of 'std::vector<edge> graph [100005]'
  435 | vector<edge> graph[MAX_N];
      |              ^~~~~
a.cc:45:14: note: 'std::vector<edge> graph [100005]' previously declared here
   45 | vector<edge> graph[MAX_N];
      |              ^~~~~
a.cc:436:5: error: redefinition of 'int par [100005]'
  436 | int par[MAX_N];
      |     ^~~
a.cc:46:5: note: 'int par [100005]' previously declared here
   46 | int par[MAX_N];
      |     ^~~
a.cc:437:5: error: redefinition of 'int st [100005]'
  437 | int st[MAX_N];
      |     ^~
a.cc:47:5: note: 'int st [100005]' previously declared here
   47 | int st[MAX_N];
      |     ^~
a.cc:438:4: error: redefinition of 'long long int sma'
  438 | ll sma;
      |    ^~~
a.cc:48:4: note: 'long long int sma' previously declared here
   48 | ll sma;
      |    ^~~
a.cc:440:6: error: redefinition of 'void dfs(int, int)'
  440 | void dfs(int u,int p)
      |      ^~~
a.cc:50:6: note: 'void dfs(int, int)' previously defined here
   50 | void dfs(int u,int p)
      |      ^~~
a.cc:452:6: error: redefinition of 'void redfs(int, int, long long int)'
  452 | void redfs(int u,int p,ll dist)
      |      ^~~~~
a.cc:62:6: note: 'void redfs(int, int, long long int)' previously defined here
   62 | void redfs(int u,int p,ll dist)
      |      ^~~~~
a.cc:462:5: error: redefinition of 'int main()'
  462 | int main()
      |     ^~~~
a.cc:72:5: note: 'int main()' previously defined here
   72 | int main()
      |     ^~~~