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>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;
#define fi first
#define se second
#define REP(i, n) for (auto i = 0; i < (n); ++i)
#define FOR(i, a, b) for (auto i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (auto i = (a); i >= (b); --i)
#define FORE(i, a, b) for (auto i = (a); i < (b); ++i)
#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define left ___left
#define right ___right
#define __builtin_popcount __builtin_popcountll
int solve(int N, int M, std::vector<int> F, std::vector<std::vector<int>> S) {
int ans = 0, res = 0;
vector <vector <int>> adj(N);
for (int i = 1; i < N; ++i) {
adj[F[i]].emplace_back(i);
}
vector <int> d(N);
int num_node = 0;
function <void(int)> dfs = [&] (int u) -> void {
if(d[u]) return;
d[u] = true;
num_node++;
for (auto v : adj[u]) dfs(v);
};
for (int i = 0; i < N - 1; ++i) {
num_node = 0;
for (int j = 0; j < M; ++j) {
dfs(S[j][i]);
}
ans += num_node == 1;
}
return ans;
}
#ifdef LOCAL
#include <cassert>
#include <cstdio>
#include <vector>
void taskcase() {
int N, M;
assert(2 == scanf("%d%d", &N, &M));
std::vector<int> F(N);
F[0] = -1;
for (int i = 1; i < N; ++i)
assert(1 == scanf("%d", &F[i]));
std::vector<std::vector<int>> S(M, std::vector<int>(N - 1));
for (int i = 0; i < M; ++i)
for (int j = 0; j < N - 1; ++j)
assert(1 == scanf("%d", &S[i][j]));
printf("%d\n", solve(N, M, F, S));
}
int main() {
freopen("TASK.inp", "r", stdin);
freopen("TASK.out", "w", stdout);
int T;
assert(1 == scanf("%d", &T));
while(T--) taskcase();
return 0;
}
#endif
// Dream it. Wish it. Do it.
Compilation message (stderr)
september.cpp: In function 'int solve(int, int, std::vector<int>, std::vector<std::vector<int> >)':
september.cpp:22:18: warning: unused variable 'res' [-Wunused-variable]
22 | int ans = 0, res = 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |