#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
vector<vector<int>> graph;
vector<bool> ending;
long long answer = 0, ending_nodes = 0;
void dfs(int x) {
if(graph[x].empty()) {
++answer;
if(!ending[x]) {
ending[x] = true;
++ending_nodes;
}
} else
for(const auto& i : graph[x])
dfs(i);
}
int main() {
ios_base::sync_with_stdio(false);
cout.tie(nullptr);
cin.tie(nullptr);
int n;
cin >> n;
graph.resize(n+1);
ending.resize(n+1);
for(int i = 1; i <= n; ++i) {
int r;
cin >> r;
graph[i].resize(r);
for(auto& j : graph[i])
cin >> j;
}
dfs(1);
cout << max<long long>(answer - ending_nodes, 1);
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include <iostream> #include <algorithm> #include <vector> using namespace std; vector<vector<int>> graph; vector<bool> ending; long long answer = 0, ending_nodes = 0; void dfs(int x) { if(graph[x].empty()) { ++answer; if(!ending[x]) { ending[x] = true; ++ending_nodes; } } else for(const auto& i : graph[x]) dfs(i); } int main() { ios_base::sync_with_stdio(false); cout.tie(nullptr); cin.tie(nullptr); int n; cin >> n; graph.resize(n+1); ending.resize(n+1); for(int i = 1; i <= n; ++i) { int r; cin >> r; graph[i].resize(r); for(auto& j : graph[i]) cin >> j; } dfs(1); cout << max<long long>(answer - ending_nodes, 1); } |
English