alexjercan/bugnet huggingface dataset:
Problem p02726 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) { return b != 0 ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

int main() {
  ll n, x, y;
  cin >> n >> x >> y;
  x--;
  y--;
  vector<ll> ans(n);
  queue<pair<ll, ll>> q;
  rep(i, n) {
    vector<ll> dist(n, 1e9);
    q.push(make_pair(i, 0));
    dist[i] = 0;
    while (!q.empty()) {
      pair<ll, ll> v = q.front();
      q.pop();
      ll f = v.first, s = v.second;
      if (f - 1 >= 0) {
        if (dist[f - 1] == 1e9) {
          q.push(make_pair(f - 1, s + 1));
          dist[f - 1] = s + 1;
        }
      }
      if (f + 1 < n) {
        if (dist[f + 1] == 1e9) {
          q.push(make_pair(f + 1, s + 1));
          dist[f + 1] = s + 1;
        }
      }
      if (f == x) {
        if (dist[y] == 1e9) {
          q.push(make_pair(y, s + 1));
          dist[y] = s + 1;
        }
      }
      if (f == y) {
        if (dist[x] == 1e9) {
          q.push(make_pair(x, s + 1));
          dist[x] = s + 1;
        }
      }
    }
    rep(j, n) ans[dist[j]]++;
  }
  for (ll i = 1; i < n; i++)
    cout << ans[i] / 2 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) { return b != 0 ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

int main() {
  ll n, x, y, a = 0;
  cin >> n >> x >> y;
  for (ll k = 1; k <= n - 1; k++) {
    for (ll i = 1; i <= n - k; i++) {
      for (ll j = i + k; j <= n; j++) {
        if (i <= x) {
          if (j < y) {
            a += (min(j - i, (x - i) + 1 + (y - j)) == k);
          } else {
            a += (min(j - i, (j - i) - (y - x) + 1) == k);
          }
        } else if (i > x && i < y) {
          if (j < y) {
            a += (min(j - i, (i - x) + 1 + (y - j)) == k);
          } else {
            a += (min(j - i, (i - x) + 1 + (j - y)) == k);
          }
        } else {
          a += (j - i == k);
        }
      }
    }
    cout << a << endl;
    a = 0;
  }
  return 0;
}
Problem p02726 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int INF = 100100100;

vector<int> G[2050];

int main() {
  int n, x, y;
  cin >> n >> x >> y;
  --x;
  --y;
  vector<int> ans(n);

  rep(i, n) {
    G[i].push_back(i + 1);
    G[i + 1].push_back(i);
  }
  G[x].push_back(y);
  G[y].push_back(x);

  rep(sv, n) {
    vector<int> dist(n, INF);
    queue<int> que;
    que.push(sv);
    dist[sv] = 0;

    int u;
    while (!que.empty()) {
      u = que.front();
      que.pop();
      for (int v : G[u]) {
        if (dist[v] == INF) {
          dist[v] = dist[u] + 1;
          que.push(v);
        }
      }
    }
    rep(i, n)++ ans[dist[i]];
  }

  rep(i, n) ans[i] /= 2;
  for (int i = 1; i <= n - 1; ++i) {
    cout << ans[i] << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int INF = 100100100;

int main() {
  int n, x, y;
  cin >> n >> x >> y;
  --x;
  --y;
  vector<int> G[n];
  vector<int> ans(n);

  rep(i, n) {
    G[i].push_back(i + 1);
    G[i + 1].push_back(i);
  }
  G[x].push_back(y);
  G[y].push_back(x);

  rep(sv, n) {
    vector<int> dist(n, INF);
    queue<int> que;
    que.push(sv);
    dist[sv] = 0;

    int u;
    while (!que.empty()) {
      u = que.front();
      que.pop();
      for (int v : G[u]) {
        if (dist[v] == INF) {
          dist[v] = dist[u] + 1;
          que.push(v);
        }
      }
    }
    rep(i, n)++ ans[dist[i]];
  }

  rep(i, n) ans[i] /= 2;
  for (int i = 1; i <= n - 1; ++i) {
    cout << ans[i] << endl;
  }

  return 0;
}
Problem p02726 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n, x, y;
  cin >> n >> x >> y;
  x--;
  y--;

  vector<int> cnt(n, 0);
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      cnt[min(abs(j - i),
              min(abs(x - i) + 1 + abs(j - y), abs(y - i) + 1 + abs(j - x)))]++;
    }
  }
  for (int i = 1; i < n; i++)
    cout << cnt[i] << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;

bool g[2005][2005];
bool ok[2005];
int d[1005][1005];

int main() {
  int n, x, y;
  cin >> n >> x >> y;
  for (int i = 0; i < n - 1; i++) {
    g[i][i + 1] = g[i + 1][i] = true;
  }
  x--;
  y--;
  g[x][y] = g[y][x] = true;

  for (int i = 0; i < n; i++)
    ok[i] = false;

  vector<int> ans(n, 0);
  for (int i = 0; i < n; i++) {
    queue<int> q;
    q.push(i);
    d[i][i] = 0;
    ok[i] = true;
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      for (int j = 0; j < n; j++) {
        if (!g[v][j])
          continue;
        if (ok[j])
          continue;
        ok[j] = true;
        q.push(j);
        d[i][j] = d[i][v] + 1;
      }
    }
    for (int j = 0; j < n; j++)
      ans[d[i][j]]++;
    for (int j = 0; j < n; j++)
      ok[j] = false;
  }
  for (int i = 1; i <= n - 1; i++)
    cout << ans[i] / 2 << endl;
}
Problem p02726 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define pq priority_queue
using namespace std;
const ll INF = (ll)1e9;
const ll MOD = (ll)1e9 + 7;
const ll MAX = 100100;
vector<int> dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ll N, X, Y;
  cin >> N >> X >> Y;
  vector<vector<ll>> v(N, vector<ll>(N));
  vector<ll> ans(N, 0);
  for (ll i = 0; i < N; i++) {
    for (ll j = 0; j < N; j++)
      v[i][j] = abs(i - j);
  }

  v[X - 1][Y - 1] = 1;
  v[Y - 1][X - 1] = 1;

  for (ll i = 0; i < N; i++) {
    for (ll j = 0; j < N; j++) {
      v[i][j] = min(v[i][j], abs(X - i - 1) + abs(Y - j - 1) + 1);
    }
  }

  for (ll i = 0; i < N; i++) {
    for (ll j = i + 1; j < N; j++) {
      ans[v[i][j]]++;
    }
  }
  for (ll i = 1; i < N; i++)
    cout << ans[i] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pq priority_queue
using namespace std;
const ll INF = (ll)1e9;
const ll MOD = (ll)1e9 + 7;
const ll MAX = 100100;
vector<int> dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ll N, X, Y;
  cin >> N >> X >> Y;
  vector<vector<ll>> v(N, vector<ll>(N));
  vector<ll> ans(N, 0);
  for (ll i = 0; i < N; i++) {
    for (ll j = 0; j < N; j++)
      v[i][j] = abs(i - j);
  }

  v[X - 1][Y - 1] = 1;
  v[Y - 1][X - 1] = 1;

  for (ll k = 0; k < N; k++) {
    for (ll i = 0; i < N; i++) {
      for (ll j = i + 1; j < N; j++) {
        v[i][j] = min(v[i][k] + v[k][j], v[i][j]);
      }
    }
  }

  for (ll i = 0; i < N; i++) {
    for (ll j = i + 1; j < N; j++) {
      ans[v[i][j]]++;
    }
  }
  for (ll i = 1; i < N; i++)
    cout << ans[i] << endl;
}
Problem p02726 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define F first
#define S second
using namespace std;
using ll = long long;
using P = pair<int, int>;

vector<int> f(const vector<vector<P>> &m, int src) {
  vector<int> d(m.size(), 1 << 30);
  queue<P> q;
  q.push(P(src, 0));

  while (!q.empty()) {
    P t = q.front();
    q.pop();

    int dst = t.F;
    int c = t.S;
    if (d[dst] > c) {
      d[dst] = c;
      rep(i, m[dst].size()) {
        P n = m[dst][i];
        q.push(P(n.F, c + n.S));
      }
    }
  }

  return d;
}

int main() {
  int n, x, y;
  cin >> n >> x >> y;

  vector<vector<P>> m(n);
  rep(i, n - 1) {
    m[i].push_back(P(i + 1, 1));
    m[i + 1].push_back(P(i, 1));
  }
  m[x - 1].push_back(P(y - 1, 1));
  m[y - 1].push_back(P(x - 1, 1));

  vector<int> v(n, 0);
  rep(i, n - 1) {
    vector<int> d = f(m, i);
    for (int j = i + 1; j < d.size(); j++) {
      v[d[j]]++;
    }
  }

  for (int i = 1; i < n; i++) {
    cout << v[i] << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define F first
#define S second
using namespace std;
using ll = long long;
using P = pair<int, int>;

typedef int Weight;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;

// void shortestPath(const Matrix &g, Matrix &dist) {
void shortestPath(Matrix &dist) {
  int n = dist.size();

  REP(k, n) REP(i, n) REP(j, n) {
    if (dist[i][j] > dist[i][k] + dist[k][j]) {
      dist[i][j] = dist[i][k] + dist[k][j];
    }
  }
}

int main() {
  int n, x, y;
  cin >> n >> x >> y;

  const int inf = (1 << 29);

  Matrix m(n, Array(n, inf));
  REP(i, n - 1) {
    m[i][i + 1] = m[i + 1][i] = 1;
    m[i][i] = 0;
  };
  m[n - 1][n - 1] = 0;
  m[x - 1][y - 1] = m[y - 1][x - 1] = 1;

  // Matrix d(n, Array(n));
  shortestPath(m);

  vector<int> v(n, 0);
  REP(i, n - 1) {
    for (int j = i + 1; j < n; j++) {
      int t = m[i][j];
      v[t]++;
    }
  }
  for (int i = 1; i < n; i++) {
    printf("%d\\n", v[i]);
  }

  return 0;
}
Problem p02726 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int main() {

  int N, X, Y;
  cin >> N >> X >> Y;
  X--;
  Y--;
  vector<int> klist(N - 1);
  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      const int inf = 100000;
      int ans = inf;
      if (j <= X || Y <= i)
        ans = j - i;
      else if (i <= X && X <= j && j <= Y)
        ans = min(X - i + 1 + Y - j, j - i);
      else if (X <= i && i <= Y && X <= j && j <= Y)
        ans = min(j - i, i - X + Y - j + 1);
      else if (X <= i && i <= Y && Y <= j)
        ans = min(j - i, i - X + 1 + j - Y);
      else if (i < X && Y <= j)
        ans = X - i + j - Y + 1;

      if (ans >= N - 1)
        continue;
      else
        klist.at(ans - 1)++;
    }
  }

  for (int i = 0; i < N - 1; i++) {
    cout << klist.at(i) << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, X, Y;
  cin >> N >> X >> Y;
  X--;
  Y--;

  for (int k = 1; k <= N - 1; k++) {
    int ans = 0;
    for (int i = 0; i < N - 1; i++) {
      for (int j = i + 1; j < N; j++) {
        if ((j <= X || Y <= i) && j - i == k)
          ans++;
        else if (i <= X && X <= j && j <= Y &&
                 min(X - i + 1 + Y - j, j - i) == k)
          ans++;
        else if (X <= i && i <= Y && X <= j && j <= Y &&
                 min(j - i, i - X + Y - j + 1) == k)
          ans++;
        else if (X <= i && i <= Y && Y <= j &&
                 min(j - i, i - X + 1 + j - Y) == k)
          ans++;
        else if (i < X && Y <= j && X - i + j - Y + 1 == k)
          ans++;
      }
    }
    cout << ans << endl;
  }
}
Problem p02726 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define Hello                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl '\\n'
#define ll long long
const int N = 2e3 + 5, M = 2e5;
ll costs[M], ans[M];
vector<vector<pair<int, int>>> v;
int n, x, y;
void dijkestra(int src) {
  priority_queue<pair<ll, ll>> pq;
  for (int i = 0; i < N; i++)
    costs[i] = 1e12;
  pq.push({0, src});
  costs[src] = 0;
  while (!pq.empty()) {
    ll cost = -pq.top().first;
    ll node = pq.top().second;
    pq.pop();
    if (cost > costs[node])
      continue;
    for (ll i = 0; i < v[node].size(); i++) {
      if (cost + v[node][i].second < costs[v[node][i].first]) {
        costs[v[node][i].first] = cost + v[node][i].second;
        pq.push({-(costs[v[node][i].first]), v[node][i].first});
      }
    }
  }
  for (int i = 1; i <= n; i++)
    ans[costs[i]]++;
}
int main() {
  Hello cin >> n >> x >> y;
  v.resize(n + 5);
  v[x].push_back({y, 1});
  v[y].push_back({x, 1});
  for (int i = 1; i <= n; i++) {
    v[i].push_back({i + 1, 1});
    v[i + 1].push_back({i, 1});
  }
  for (int i = 1; i <= n; i++)
    dijkestra(i);
  for (int i = 1; i <= n - 1; i++)
    cout << ans[i] / 2 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define Hello                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl '\\n'
#define ll long long
const int N = 2e3 + 5, M = 2e5;
ll costs[M], ans[M];
vector<vector<pair<int, int>>> v;
map<pair<int, int>, int> mp;
int n, x, y;
void dijkestra(int src) {
  priority_queue<pair<ll, ll>> pq;
  for (int i = 0; i < N; i++)
    costs[i] = 1e12;
  pq.push({0, src});
  costs[src] = 0;
  while (!pq.empty()) {
    ll cost = -pq.top().first;
    ll node = pq.top().second;
    pq.pop();
    if (cost > costs[node])
      continue;
    for (ll i = 0; i < v[node].size(); i++) {
      if (cost + v[node][i].second < costs[v[node][i].first]) {
        costs[v[node][i].first] = cost + v[node][i].second;
        pq.push({-(costs[v[node][i].first]), v[node][i].first});
      }
    }
  }
  for (int i = 1; i <= n - 1; i++) {
    if (mp.find({src, i}) == mp.end()) {
      mp[{src, i}] = costs[i];
      mp[{i, src}] = 0;
    }
  }
}
int main() {
  Hello cin >> n >> x >> y;
  v.resize(n + 5);
  v[x].push_back({y, 1});
  v[y].push_back({x, 1});
  for (int i = 1; i <= n; i++) {
    v[i].push_back({i + 1, 1});
    v[i + 1].push_back({i, 1});
  }
  for (int i = 1; i <= n; i++)
    dijkestra(i);
  for (auto i : mp)
    ans[i.second]++;
  for (int i = 1; i <= n - 1; i++)
    cout << ans[i] << endl;
  return 0;
}
Problem p02726 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define inf 99999
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, x, y, i, j, temp;
  cin >> n >> x >> y;
  vector<int> key(n);
  for (i = 0; i < n; ++i)
    key[i] = 0;
  x--;
  y--;
  for (i = 0; i < n; ++i) {
    for (j = i + 1; j < n; ++j) {
      temp = min(j - i, abs(x - i) + 1 + abs(j - y));
      key[temp]++;
    }
  }

  for (i = 1; i <= n - 1; ++i)
    cout << key[i] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define inf 99999
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, x, y, i, j, k;
  cin >> n >> x >> y;
  vector<int> key(n);
  int G[n][n];
  for (i = 0; i < n; ++i) {
    for (j = 0; j < n; ++j) {
      if (j - i == 1) {
        G[i][j] = 1;
      } else if (i - j == 1)
        G[i][j] = 1;
      else if (i == j)
        G[i][j] = 0;
      else
        G[i][j] = inf;
    }
    key[i] = 0;
  }

  G[x - 1][y - 1] = 1;
  G[y - 1][x - 1] = 1;

  for (k = 0; k < n; k++) {
    for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) {
        if (G[i][k] + G[k][j] < G[i][j])
          G[i][j] = G[i][k] + G[k][j];
      }
    }
  }

  for (i = 0; i < n; ++i) {
    for (j = 0; j < n; ++j)
      key[G[i][j]]++;
  }
  for (i = 1; i <= n - 1; ++i)
    cout << key[i] / 2 << endl;
}
Problem p02726 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const string ln = "\\n";
constexpr int INF = 1001001001;
constexpr int MOD = 1000000007;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  ll n, x, y;
  cin >> n >> x >> y;
  x--;
  y--;

  vector<vector<int>> g(n, vector<int>(n, INF));

  // ワーシャルフロイド
  // 全頂点間の最短距離を求める
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      // iからjへのコストより、iからkを経由してjにいくコストの方が低いなら最短コストを更新
      if (i < j) {
        g[i][j] = min((int)abs(j - i), (int)(abs(x - i) + abs(y - j) + 1));
      }
    }
  }

  vector<int> ans(n + 1, 0);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i >= j)
        continue;
      int cost = g[i][j];

      ans[cost]++;
    }
  }

  for (int i = 1; i < n; i++) {
    cout << ans[i] << ln;
  }

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const string ln = "\\n";
constexpr int INF = 1001001001;
constexpr int MOD = 1000000007;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  ll n, x, y;
  cin >> n >> x >> y;
  x--;
  y--;

  vector<vector<int>> g(n, vector<int>(n, INF));
  for (int i = 1; i < n; i++) {
    g[i][i - 1] = 1;
    g[i - 1][i] = 1;
  }
  g[x][y] = 1;
  g[y][x] = 1;

  for (int i = 0; i < n; i++) {
    if (g[i][x] > abs(i - x)) {
      g[i][x] = abs(i - x);
      g[x][i] = g[i][x];
    }
  }

  for (int i = 0; i < n; i++) {
    if (g[i][y] > abs(i - y)) {
      g[i][y] = abs(i - y);
      g[y][i] = g[i][y];
    }
  }

  // ワーシャルフロイド
  // 全頂点間の最短距離を求める
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      // iからjへのコストより、iからkを経由してjにいくコストの方が低いなら最短コストを更新
      if (g[i][j] > g[i][x] + g[y][j] + 1) {
        g[i][j] = g[i][x] + g[y][j] + 1;
      }
    }
  }

  vector<int> ans(n + 1, 0);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i >= j)
        continue;
      int cost = g[i][j];

      ans[cost]++;
    }
  }

  for (int i = 1; i < n; i++) {
    cout << ans[i] << ln;
  }

  return 0;
}
Problem p02728 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
#include <vector>
#define N 200005
using namespace std;
typedef long long ll;

ll n, nfac = 1, sz[N], num[N] = {0, 1}, M = 1e9 + 7;
vector<ll> gr[N];

ll pow(ll p, ll q) {
  ll re = 1;
  while (q) {
    if (q & 1)
      re = re * p % M;
    p = p * p % M;
    q >>= 1;
  }
  return re;
}

void dfs(int u, int pr) {
  sz[u] = 1;
  for (auto v : gr[u]) {
    if (v == pr)
      continue;
    dfs(v, u);
    sz[u] += sz[v];
  }
  (num[1] *= sz[u]) %= M;
}

void dfs2(int u, int pr) {
  for (auto v : gr[u]) {
    if (v == pr)
      continue;
    num[v] = num[u] * pow(sz[v], M - 2) % M * (n - sz[v]) % M;
    dfs2(v, u);
  }
}

int main() {
  ll i, t1, t2;
  cin >> n;
  for (i = 0; i < n - 1; i++) {
    scanf("%lld%lld", &t1, &t2);
    gr[t1].push_back(t2);
    gr[t2].push_back(t1);
  }
  for (i = 2; i <= n; i++)
    (nfac *= i) %= M;
  dfs(1, 0);
  dfs2(1, 0);
  for (i = 1; i <= n; i++) {
    printf("%lld\\n", nfac * pow(num[i], M - 2) % M);
  }
  return 0;
}

Error: -11

Fixed submission:

#include <cstdio>
#include <iostream>
#include <vector>
#define N 200005
using namespace std;
typedef long long ll;

ll n, nfac = 1, sz[N], num[N] = {0, 1}, M = 1e9 + 7;
vector<int> gr[N];

ll pow(ll p, ll q) {
  ll re = 1;
  while (q) {
    if (q & 1)
      re = re * p % M;
    p = p * p % M;
    q >>= 1;
  }
  return re;
}

void dfs(int u, int pr) {
  sz[u] = 1;
  for (auto v : gr[u]) {
    if (v == pr)
      continue;
    dfs(v, u);
    sz[u] += sz[v];
  }
  (num[1] *= sz[u]) %= M;
}

void dfs2(int u, int pr) {
  for (auto v : gr[u]) {
    if (v == pr)
      continue;
    num[v] = num[u] * pow(sz[v], M - 2) % M * (n - sz[v]) % M;
    dfs2(v, u);
  }
}

int main() {
  ll i, t1, t2;
  cin >> n;
  for (i = 0; i < n - 1; i++) {
    scanf("%d%d", &t1, &t2);
    gr[t1].push_back(t2);
    gr[t2].push_back(t1);
  }
  for (i = 2; i <= n; i++)
    (nfac *= i) %= M;
  dfs(1, 0);
  dfs2(1, 0);
  for (i = 1; i <= n; i++) {
    printf("%lld\\n", nfac * pow(num[i], M - 2) % M);
  }
  return 0;
}
Problem p02728 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

#define ull unsigned long long
#define endl '\\n'
#define ll long long
const int N = 2e5 + 5;
ll mod = 1e9 + 7;
vector<ll> node[N];
ll sz[N], dp[N], fact[N], ans[N], n;
ll inv(ll u, ll p) {
  if (p == 0)
    return 1;
  if (p == 1)
    return u;
  ll t = inv(u, p / 2) % mod;
  if (p & 1)
    return t * t % mod * u % mod;
  return t * t % mod;
}
void dfs(ll u, ll p) {
  sz[u] = 1;
  for (auto j : node[u]) {
    if (j != p) {
      dfs(j, u);
      sz[u] += sz[j];
    }
  }
}
ll tot = 1;
void reroot(ll u, ll v) {
  tot = tot * inv(sz[u], mod - 2) % mod * inv(sz[v], mod - 2) % mod;
  sz[u] = n - sz[v];
  sz[v] = n;
  tot = tot * sz[u] % mod * sz[v] % mod;
}
void dfs1(ll u, ll p) {
  ans[u] = fact[n] * inv(tot, mod - 2) % mod;
  for (auto j : node[u]) {
    if (j != p) {
      reroot(u, j);
      dfs1(j, u);
      reroot(j, u);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  if (fopen(".INP", "r")) {
    freopen(".INP", "r", stdin);
    freopen(".OUT", "w", stdout);
  }
  cin >> n;
  fact[0] = 1;
  for (int i = 1; i <= n; i++)
    fact[i] = fact[i - 1] * i % mod;
  for (int i = 1; i < n; i++) {
    ll u, v;
    cin >> u >> v;
    node[u].push_back(v);
    node[v].push_back(u);
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++) {
    tot = (tot * sz[i]) % mod;
  }
  dfs1(1, 0);
  for (int i = 1; i <= n; i++) {
    cout << ans[i] << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define ull unsigned long long
#define endl '\\n'
#define ll long long
const int N = 1e5 + 5;
ll mod = 1e9 + 7;
vector<ll> node[N];
ll sz[N], dp[N], fact[N], ans[N], n;
ll inv(ll u, ll p) {
  if (p == 0)
    return 1;
  if (p == 1)
    return u;
  ll t = inv(u, p / 2) % mod;
  if (p & 1)
    return t * t % mod * u % mod;
  return t * t % mod;
}
void dfs(ll u, ll p) {
  sz[u] = 1;
  for (auto j : node[u]) {
    if (j != p) {
      dfs(j, u);
      sz[u] += sz[j];
    }
  }
}
ll tot = 1;
void reroot(ll u, ll v) {
  tot = tot * inv(sz[u], mod - 2) % mod * inv(sz[v], mod - 2) % mod;
  sz[u] = n - sz[v];
  sz[v] = n;
  tot = tot * sz[u] % mod * sz[v] % mod;
}
void dfs1(ll u, ll p) {
  ans[u] = fact[n] * inv(tot, mod - 2) % mod;
  for (auto j : node[u]) {
    if (j != p) {
      reroot(u, j);
      dfs1(j, u);
      reroot(j, u);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  if (fopen(".INP", "r")) {
    freopen(".INP", "r", stdin);
    freopen(".OUT", "w", stdout);
  }
  cin >> n;
  fact[0] = 1;
  for (int i = 1; i <= n; i++)
    fact[i] = fact[i - 1] * i % mod;
  for (int i = 1; i < n; i++) {
    ll u, v;
    cin >> u >> v;
    node[u].push_back(v);
    node[v].push_back(u);
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++) {
    tot = (tot * sz[i]) % mod;
  }
  dfs1(1, 0);
  for (int i = 1; i <= n; i++) {
    cout << ans[i] << endl;
  }
}