You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
|
|
#define int long long
|
|
|
|
#define endl "\n"
|
|
|
|
const int N = 100010;
|
|
|
|
|
|
|
|
int n, mod;
|
|
|
|
|
|
|
|
vector<int> s[N];
|
|
|
|
int f[N], g[N];
|
|
|
|
|
|
|
|
void dfs1(int x, int fa) {
|
|
|
|
for (int v : s[x]) {
|
|
|
|
if (v == fa) continue;
|
|
|
|
dfs1(v, x);
|
|
|
|
f[x] = f[x] * (f[v] + 1) % mod;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void dfs2(int x, int fa) {
|
|
|
|
int t = g[x];
|
|
|
|
for (int v : s[x]) {
|
|
|
|
if (v == fa) continue;
|
|
|
|
g[v] = g[v] * t % mod;
|
|
|
|
}
|
|
|
|
|
|
|
|
t = 1;
|
|
|
|
for (int v : s[x]) {
|
|
|
|
if (v == fa) continue;
|
|
|
|
g[v] = g[v] * t % mod, t = t * (f[v] + 1) % mod;
|
|
|
|
}
|
|
|
|
|
|
|
|
t = 1;
|
|
|
|
reverse(s[x].begin(), s[x].end());
|
|
|
|
for (int v : s[x]) {
|
|
|
|
if (v == fa) continue;
|
|
|
|
g[v] = g[v] * t % mod, t = t * (f[v] + 1) % mod;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int v : s[x]) {
|
|
|
|
if (v == fa) continue;
|
|
|
|
g[v]++;
|
|
|
|
dfs2(v, x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
signed main() {
|
|
|
|
cin >> n >> mod;
|
|
|
|
for (int i = 1; i <= n; i++) f[i] = g[i] = 1;
|
|
|
|
for (int i = 1; i < n; i++) {
|
|
|
|
int a, b;
|
|
|
|
cin >> a >> b;
|
|
|
|
s[a].push_back(b), s[b].push_back(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
dfs1(1, 0);
|
|
|
|
dfs2(1, 0);
|
|
|
|
for (int i = 1; i <= n; ++i) cout << f[i] * g[i] % mod << endl;
|
|
|
|
}
|