This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e3 + 5;
int n, m, root[2];
vector<int> g[2][N];
int dp[N][N];
void dfs1(int u) {
for(int v : g[0][u]) {
for(int i = 1; i <= n; ++i) {
dp[v][i] += dp[u][i];
}
dfs1(v);
}
}
void dfs2(int u) {
for(int v : g[1][u]) {
for(int i = 1; i <= n; ++i) {
dp[i][v] += dp[i][u];
}
dfs2(v);
}
}
void solve() {
cin >> n >> m;
for(int i = 1, p0, p1; i <= n; ++i) {
cin >> p0 >> p1;
if(p0 == 0) root[0] = i;
else g[0][p0].push_back(i);
if(p1 == 0) root[1] = i;
else g[1][p1].push_back(i);
}
for(int i = 0, p0, p1; i < m; ++i) {
cin >> p0 >> p1;
dp[p0][p1]++;
}
dfs1(root[0]); dfs2(root[1]);
for(int i = 1; i <= n; ++i) {
cout << dp[i][i] << '\n';
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int test = 1;
// cin >> test;
while(test--) solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |