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.
54 lines
1.2 KiB
54 lines
1.2 KiB
#include <bits/stdc++.h>
|
|
using namespace std;
|
|
#define int long long
|
|
#define endl "\n"
|
|
const int N = 100010;
|
|
|
|
int n, m;
|
|
|
|
vector<int> s[N];
|
|
int dp[N], pd[N];
|
|
|
|
void dfs1(int x, int fa) {
|
|
for (int i : s[x]) {
|
|
if (i != fa) {
|
|
dfs1(i, x);
|
|
dp[x] = dp[x] * (dp[i] + 1) % m;
|
|
}
|
|
}
|
|
}
|
|
|
|
void dfs2(int x, int fa) {
|
|
int t = pd[x];
|
|
for (int i : s[x])
|
|
if (i != fa)
|
|
pd[i] = pd[i] * t % m;
|
|
t = 1;
|
|
for (int i : s[x])
|
|
if (i != fa)
|
|
pd[i] = pd[i] * t % m, t = t * (dp[i] + 1) % m;
|
|
t = 1;
|
|
reverse(s[x].begin(), s[x].end());
|
|
for (int i : s[x])
|
|
if (i != fa)
|
|
pd[i] = pd[i] * t % m, t = t * (dp[i] + 1) % m;
|
|
for (int i : s[x])
|
|
if (i != fa) {
|
|
++pd[i];
|
|
dfs2(i, x);
|
|
}
|
|
}
|
|
|
|
signed main() {
|
|
cin >> n >> m;
|
|
for (int i = 1; i <= n; i++) dp[i] = pd[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 << dp[i] * pd[i] % m << endl;
|
|
} |