Submission #1340239

#TimeUsernameProblemLanguageResultExecution timeMemory
1340239trungcanDrzava (COCI25_drzava)C++20
61 / 110
2095 ms83900 KiB
#include <bits/stdc++.h>
#define ll long long

using namespace std;

static struct FastInput {
  static constexpr int BUF_SIZE = 1 << 20;
  char buf[BUF_SIZE];
  size_t chars_read = 0;
  size_t buf_pos = 0;
  FILE *in = stdin;
  char cur = 0;
 
  inline char get_char() {
    if (buf_pos >= chars_read) {
      chars_read = fread(buf, 1, BUF_SIZE, in);
      buf_pos = 0;
      buf[0] = (chars_read == 0 ? -1 : buf[0]);
    }
    return cur = buf[buf_pos++];
  }
 
  inline void tie(int) {}
 
  inline explicit operator bool() {
    return cur != -1;
  }
 
  inline static bool is_blank(char c) {
    return c <= ' ';
  }
 
  inline bool skip_blanks() {
    while (is_blank(cur) && cur != -1) {
      get_char();
    }
    return cur != -1;
  }
 
  inline FastInput& operator>>(char& c) {
    skip_blanks();
    c = cur;
    return *this;
  }
 
  inline FastInput& operator>>(string& s) {
    if (skip_blanks()) {
      s.clear();
      do {
        s += cur;
      } while (!is_blank(get_char()));
    }
    return *this;
  }
 
  template <typename T>
  inline FastInput& read_integer(T& n) {
    // unsafe, doesn't check that characters are actually digits
    n = 0;
    if (skip_blanks()) {
      int sign = +1;
      if (cur == '-') {
        sign = -1;
        get_char();
      }
      do {
        n += n + (n << 3) + cur - '0';
      } while (!is_blank(get_char()));
      n *= sign;
    }
    return *this;
  }
 
  template <typename T>
  inline typename enable_if<is_integral<T>::value, FastInput&>::type operator>>(T& n) {
    return read_integer(n);
  }
 
  #if !defined(_WIN32) || defined(_WIN64)
  inline FastInput& operator>>(__int128& n) {
    return read_integer(n);
  }
  #endif
 
  template <typename T>
  inline typename enable_if<is_floating_point<T>::value, FastInput&>::type operator>>(T& n) {
    // not sure if really fast, for compatibility only
    n = 0;
    if (skip_blanks()) {
      string s;
      (*this) >> s;
      sscanf(s.c_str(), "%lf", &n);
    }
    return *this;
  }
} fast_input;
 
#define cin fast_input

const int N = 5005;
const int MOD = 1e9 + 7;
const ll LIM = 2e18;

ll dp[N][N], ans[N];
short sz[N], s[N][2];
short id[N], cnt, n;
vector<short> adj[N];

void dfs(short u, short par){
    dp[u][0] = sz[u] = 1;
    for (short v: adj[u])
        if (v != par){
            dfs(v, u);

            for (short i = sz[u]; i >= 0; --i)
                for (short j = 1; j <= sz[v]; ++j)
                    if (dp[u][i] && dp[v][j]){
                        dp[u][i + j] += (dp[u][i] % MOD * 1LL * (dp[v][j] % MOD)) % MOD;
                        if (dp[u][i + j] >= LIM) dp[u][i + j] %= MOD;
                    }
            sz[u] += sz[v];
        }
    dp[u][1] = sz[u];
}

void reroot(short u, short par){
    for (short i = 0; i < n; ++i)
        ans[i + 1] += dp[u][i] % MOD + (i == 1 ? -1 : 0);
    id[u] = ++cnt;
    vector<ll> f[2];
    int sc = sz[u];
    for (short i = 0; i <= sz[u]; ++i) f[0].push_back(dp[u][i]);
//    cout << "\t\t" << u << "\n";
//    for (int i = 1; i <= n; ++i) cout << i << " " << sz[i] << "\n";
//    for (int i = 0; i <= sz[u]; ++i) cout << dp[u][i] << " "; cout << '\n';
    for (short v: adj[u])
        if (v != par){
            int tmp = sz[v];
            f[1].clear();
            for (short i = 1; i <= sz[u]; ++i) dp[u][i] = 0;
            for (short i = 0; i <= sz[v]; ++i) f[1].push_back(dp[v][i]);


            sz[u] = dp[u][0] = 1;
            for (short w: adj[u])
                if (w != v){
                    for (short i = sz[u]; i >= 0; --i)
                        for (short j = 1; j <= sz[w]; ++j)
                            if (dp[u][i] && dp[w][j]){
                                dp[u][i + j] += (dp[u][i] % MOD * 1LL * (dp[w][j] % MOD)) % MOD;
                                if (dp[u][i + j] >= LIM) dp[u][i + j] %= MOD;
                            }
                    sz[u] += sz[w];
                }
            dp[u][1] = sz[u];

            --dp[v][1];
            for (short i = sz[v]; i >= 0; --i)
                for (short j = 1; j <= sz[u]; ++j)
                    if (dp[v][i] && dp[u][j]){
                        dp[v][i + j] += (dp[v][i] % MOD * 1LL * (dp[u][j] % MOD)) % MOD;
                        if (dp[v][i + j] >= LIM) dp[v][i + j] %= MOD;
                    }
            sz[v] += sz[u]; dp[v][1] = sz[v];

            reroot(v, u);

            sz[v] = tmp;
            for (short i = 0; i <= sz[v]; ++i) dp[v][i] = f[1][i];
        }

    sz[u] = sc;
    for (short i = 0; i <= sz[u]; ++i) dp[u][i] = f[0][i];
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> n;
    for (short i = 1, u, v; i < n; ++i){
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    dfs(1, 1);
    reroot(1, 1);

    for (short i = 1; i <= n; ++i) cout << ans[i] % MOD << " ";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...