pat甲級 1004 Counting Leaves(輸出每層葉子結點)(dfs)
阿新 • • 發佈:2018-12-24
#include<iostream> #include<vector> #include<algorithm> using namespace std; vector<int> v[100]; int maxdepth = -1,book[105]; void dfs(int index, int depth) { if (v[index].size() == 0) { book[depth]++; maxdepth = max(maxdepth, depth); return; } for (int i = 0; i < v[index].size(); i++) dfs(v[index][i], depth + 1); } int main() { int n, m, kid, node, c; cin >> n >> m; for (int i = 0; i < m; i++) { cin >> node >> kid; for (int j = 0; j < kid; j++) { cin >> c; v[node].push_back(c); } } dfs(1, 0); cout << book[0]; for (int i = 1; i <= maxdepth; i++) { cout << " " << book[i]; } cout << endl; return 0; }
vector好方便啊!!!!!!!!!