Problem p02728 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (n); (i)++)
#define INF 5000000000000000
using namespace std;

struct Edge {
  int to;
  long weight;
  Edge(int t, long w) : to(t), weight(w) {}
};

long modpow(long a, long n, long mod) {
  long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

// a^{-1} mod を計算する
long modinv(long a, long mod) { return modpow(a, mod - 2, mod); }

struct compare1 {
  bool operator()(const pair<long, long> &value, const long &key) {
    return (value.first < key);
  }
  bool operator()(const long &key, const pair<long, long> &value) {
    return (key < value.first);
  }
};

struct UnionFind {
  vector<int> par;
  vector<int> rank;

  UnionFind(int n = 1) { init(n); }

  void init(int n = 1) {
    par.resize(n);
    rank.resize(n);
    REP(i, n) par[i] = i, rank[i] = 0;
  }

  int root(int x) {
    if (par[x] == x)
      return x;
    else
      return par[x] = root(par[x]);
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (rank[x] < rank[y])
      swap(x, y);
    if (rank[x] == rank[y])
      rank[x]++;
    par[y] = x;
    return true;
  }
};

template <class Abel> struct weightedUnionFind {
  vector<int> par;
  vector<int> rank;
  vector<Abel> diff_weight;

  weightedUnionFind(int n = 1, Abel SUM_UNITY = 0) { init(n, SUM_UNITY); }

  void init(int n = 1, Abel SUM_UNITY = 0) {
    par.resize(n);
    rank.resize(n);
    diff_weight.resize(n);
    REP(i, n) par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY;
  }

  int root(int x) {
    if (par[x] == x)
      return x;
    else {
      int r = root(par[x]);
      diff_weight[x] += diff_weight[par[x]];
      return par[x] = r;
    }
  }

  Abel weight(int x) {
    root(x);
    return diff_weight[x];
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y, Abel w) {
    w += weight(x);
    w -= weight(y);
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (rank[x] < rank[y])
      swap(x, y), w = -w;
    if (rank[x] == rank[y])
      rank[x]++;
    par[y] = x;
    diff_weight[y] = w;
    return true;
  }

  Abel diff(int x, int y) { return weight(y) - weight(x); }
};

using Graph = vector<vector<int>>;
using P = pair<int, int>;

/*
void dijkstra(int s, int V, Graph &G, long* d){
    priority_queue<P, vector<P>, greater<P>> pque;
    fill(d, d + V, INF);
    d[s] = 0;
    pque.push(P(0, s));

    while(!pque.empty()){
        P p = pque.top(); pque.pop();
        int now = p.second;
        if(d[now] < p.first) continue;
        REP(i, G[now].size()){
            Edge e = G[now][i];
            if(d[e.to] > d[now] + e.weight){
                d[e.to] = d[now] + e.weight;
                pque.push(P(d[e.to], e.to));
            }
        }
    }
}
*/
long kaijyo[200005];
long kaijyo2[200005];
long dp[200005];
int kosu[200005];
long dp2[200005];
bool check[200005];
Graph G(200005);
long mod = 1000000007;
int N;

void dfs(int par, int me) {
  dp[me] = 1l;
  kosu[me] = 1;
  int temp = 0;
  for (auto next : G[me]) {
    if (next == par)
      continue;
    dfs(me, next);
    dp[me] *= dp[next];
    dp[me] %= mod;
    dp[me] *= kaijyo2[kosu[next]];
    dp[me] %= mod;
    temp += kosu[next];
  }
  dp[me] *= kaijyo[temp];
  dp[me] %= mod;
  kosu[me] += temp;
}

void dfs2(int par, int me) {
  if (check[me])
    return;
  dp2[me] = 1l;
  for (auto next : G[me]) {
    if (next == par) {
      dp2[me] *= dp2[par];
      dp2[me] %= mod;
      dp2[me] *= kaijyo2[N - 1];
      dp2[me] %= mod;
      dp2[me] *= modinv(dp[me], mod);
      dp2[me] %= mod;
      dp2[me] *= kaijyo[N - kosu[me] - 1];
      dp2[me] %= mod;
      dp2[me] *= kaijyo[kosu[me]];
      dp2[me] %= mod;
      dp2[me] *= kaijyo2[N - kosu[me]];
      dp2[me] %= mod;
    } else {
      dp2[me] *= dp[next];
      dp2[me] %= mod;
      dp2[me] *= kaijyo2[kosu[next]];
      dp2[me] %= mod;
    }
  }
  dp2[me] *= kaijyo[N - 1];
  dp2[me] %= mod;
  check[me] = true;
  for (auto next : G[me]) {
    dfs2(me, next);
  }
}

int main() {
  cin >> N;
  REP(i, N - 1) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  kaijyo[0] = 1l;
  kaijyo2[0] = 1l;
  REP(i, 200000) {
    kaijyo[i + 1] = (kaijyo[i] * (long)(i + 1)) % mod;
    kaijyo2[i + 1] = (kaijyo2[i] * modinv((long)(i + 1), mod)) % mod;
  }
  dfs(-1, 0);
  // REP(i, N) cout << dp[i] << " " << kosu[i] << endl;
  REP(i, N) check[i] = false;
  dfs2(-1, 0);
  REP(i, N) cout << dp2[i] << endl;
  // cout << mod * mod << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (n); (i)++)
#define INF 5000000000000000
using namespace std;

struct Edge {
  int to;
  long weight;
  Edge(int t, long w) : to(t), weight(w) {}
};

long modpow(long a, long n, long mod) {
  long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

// a^{-1} mod を計算する
long modinv(long a, long mod) { return modpow(a, mod - 2, mod); }

struct compare1 {
  bool operator()(const pair<long, long> &value, const long &key) {
    return (value.first < key);
  }
  bool operator()(const long &key, const pair<long, long> &value) {
    return (key < value.first);
  }
};

struct UnionFind {
  vector<int> par;
  vector<int> rank;

  UnionFind(int n = 1) { init(n); }

  void init(int n = 1) {
    par.resize(n);
    rank.resize(n);
    REP(i, n) par[i] = i, rank[i] = 0;
  }

  int root(int x) {
    if (par[x] == x)
      return x;
    else
      return par[x] = root(par[x]);
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (rank[x] < rank[y])
      swap(x, y);
    if (rank[x] == rank[y])
      rank[x]++;
    par[y] = x;
    return true;
  }
};

template <class Abel> struct weightedUnionFind {
  vector<int> par;
  vector<int> rank;
  vector<Abel> diff_weight;

  weightedUnionFind(int n = 1, Abel SUM_UNITY = 0) { init(n, SUM_UNITY); }

  void init(int n = 1, Abel SUM_UNITY = 0) {
    par.resize(n);
    rank.resize(n);
    diff_weight.resize(n);
    REP(i, n) par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY;
  }

  int root(int x) {
    if (par[x] == x)
      return x;
    else {
      int r = root(par[x]);
      diff_weight[x] += diff_weight[par[x]];
      return par[x] = r;
    }
  }

  Abel weight(int x) {
    root(x);
    return diff_weight[x];
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y, Abel w) {
    w += weight(x);
    w -= weight(y);
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (rank[x] < rank[y])
      swap(x, y), w = -w;
    if (rank[x] == rank[y])
      rank[x]++;
    par[y] = x;
    diff_weight[y] = w;
    return true;
  }

  Abel diff(int x, int y) { return weight(y) - weight(x); }
};

using Graph = vector<vector<long>>;
using P = pair<int, int>;

/*
void dijkstra(int s, int V, Graph &G, long* d){
    priority_queue<P, vector<P>, greater<P>> pque;
    fill(d, d + V, INF);
    d[s] = 0;
    pque.push(P(0, s));

    while(!pque.empty()){
        P p = pque.top(); pque.pop();
        int now = p.second;
        if(d[now] < p.first) continue;
        REP(i, G[now].size()){
            Edge e = G[now][i];
            if(d[e.to] > d[now] + e.weight){
                d[e.to] = d[now] + e.weight;
                pque.push(P(d[e.to], e.to));
            }
        }
    }
}
*/
long kaijyo[200005];
long kaijyo2[200005];
Graph G(200005);
long ans[800000][2];
long inde[800000];
long mod = 1000000007;
int N;

void dfs(long par, long me) {
  // cout << par << " " << me << " ";
  long temp = 200005 * par + me;
  int ind = (int)(lower_bound(inde, inde + 3 * N - 2, temp) - inde);
  long temp3 = 200005 * me + par;
  int ind3 = (int)(lower_bound(inde, inde + 3 * N - 2, temp3) - inde);
  long temp4 = 200005 * me + me;
  int ind4 = (int)(lower_bound(inde, inde + 3 * N - 2, temp4) - inde);
  // cout << ind << endl;
  if (ans[ind][0] != -1)
    return;
  if (ans[ind3][0] != -1 && ans[ind4][0] != -1) {
    long si = N - 1 - ans[ind3][1];
    ans[ind][0] = (ans[ind4][0] * kaijyo2[N - 1]) % mod;
    ans[ind][0] = (ans[ind][0] * kaijyo[ans[ind3][1]]) % mod;
    ans[ind][0] = (ans[ind][0] * kaijyo[si]) % mod;
    ans[ind][0] = (ans[ind][0] * modinv(ans[ind3][0], mod)) % mod;
    ans[ind][1] = ans[ind4][1] - ans[ind3][1];
    return;
  }
  if (G[me].size() == 1 && par != me) {
    ans[ind][0] = 1;
    ans[ind][1] = 1;
    return;
  }
  long si = 0;
  ans[ind][0] = 1;
  for (auto next : G[me]) {
    if (next == par)
      continue;
    long temp2 = 200005 * me + next;
    int ind2 = (int)(lower_bound(inde, inde + 3 * N - 2, temp2) - inde);
    if (ans[ind2][0] == -1)
      dfs(me, next);
    ans[ind][0] = (ans[ind][0] * ans[ind2][0]) % mod;
    ans[ind][0] = (ans[ind][0] * kaijyo2[ans[ind2][1]]) % mod;
    si += ans[ind2][1];
  }
  ans[ind][0] = (ans[ind][0] * kaijyo[si]) % mod;
  ans[ind][1] = si + 1;
}

int main() {
  cin >> N;
  int te = 0;
  REP(i, N) {
    inde[te] = 200005 * (long)i + (long)i;
    te++;
  }
  REP(i, N - 1) {
    long a, b;
    cin >> a >> b;
    a--, b--;
    G[a].push_back(b);
    G[b].push_back(a);
    inde[te] = 200005 * a + b;
    te++;
    inde[te] = 200005 * b + a;
    te++;
  }
  sort(inde, inde + 3 * N - 2);
  // REP(i, 3*N-2) cout << inde[i] << endl;
  REP(i, te) ans[i][0] = -1;
  kaijyo[0] = 1;
  kaijyo2[0] = 1;
  for (long i = 1; i <= N; i++) {
    kaijyo[i] = (kaijyo[i - 1] * i) % mod;
    kaijyo2[i] = (kaijyo2[i - 1] * modinv(i, mod)) % mod;
  }

  REP(i, N) dfs((long)i, (long)i);
  REP(i, N) {
    long temp = 200005 * (long)i + i;
    int ind = (int)(lower_bound(inde, inde + 3 * N - 2, temp) - inde);
    cout << ans[ind][0] << endl;
  }
  return 0;
}