Problem p03520 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <complex>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define E18 1000000000000000000LL   // 2e18
#define SIZE 100010

vector<pair<int, int>> way[SIZE];
ll ans[SIZE];
ll s[SIZE];
int a[SIZE], b[SIZE];
int n;
int key = -1, keycc = 0;
ll sum = 0;

int dfs(int now, int back) {
  int res = 0;

  for (int i = 0; i < way[now].size(); i++) {
    if (way[now][i].first == back)
      continue;

    int p = dfs(way[now][i].first, now);

    if (p == n - p) {
      key = way[now][i].second;
      keycc = p;
    } else {
      int q = way[now][i].second;
      ans[q] = abs(s[a[q]] - s[b[q]]) / abs(p - (n - p));
      sum += ans[q] * p;
    }

    res += p;
  }

  return res + 1;
}

int main() {

  scanf("%d", &n);

  for (int i = 0; i < n - 1; i++) {
    scanf("%d%d", a + i, b + i);
    a[i]--;
    b[i]--;
    way[a[i]].push_back({b[i], i});
    way[b[i]].push_back({a[i], i});
  }

  for (int i = 0; i < n; i++) {
    scanf("%lld", s + i);
  }

  dfs(0, -1);

  if (n % 2 == 0) {
    ans[key] = (s[0] - sum) / keycc;
  }

  for (int i = 0; i < n - 1; i++)
    printf("%lld\n", ans[i]);

  return 0;
}


Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <complex>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define E18 1000000000000000000LL   // 2e18
#define SIZE 100010

vector<pair<int, int>> way[SIZE];
ll ans[SIZE];
ll s[SIZE];
int a[SIZE], b[SIZE];
int n;
int key = -1, keycc = 0;
ll sum = 0;

int dfs(int now, int back) {
  int res = 0;

  for (int i = 0; i < way[now].size(); i++) {
    if (way[now][i].first == back)
      continue;

    int p = dfs(way[now][i].first, now);

    if (p == n - p) {
      key = way[now][i].second;
      keycc = p;
    } else {
      int q = way[now][i].second;
      ans[q] = abs(s[a[q]] - s[b[q]]) / abs(p - (n - p));
      sum += ans[q] * p;
    }

    res += p;
  }

  return res + 1;
}

int main() {

  scanf("%d", &n);

  for (int i = 0; i < n - 1; i++) {
    scanf("%d%d", a + i, b + i);
    a[i]--;
    b[i]--;
    way[a[i]].push_back({b[i], i});
    way[b[i]].push_back({a[i], i});
  }

  for (int i = 0; i < n; i++) {
    scanf("%lld", s + i);
  }

  dfs(0, -1);

  if (key != -1) {
    ans[key] = (s[0] - sum) / keycc;
  }

  for (int i = 0; i < n - 1; i++)
    printf("%lld\n", ans[i]);

  return 0;
}