problemscpp
A collection of my answers to algorithm problems in c++.
| 函数
pat::a::a1004 命名空间参考

1004 Counting Leaves 更多...

struct  node
 

函数

int main (istream &cin, ostream &cout)
 
 TEST (a1004, case1)
 

详细描述

1004 Counting Leaves

函数说明

◆ main()

int pat::a::a1004::main ( istream &  cin,
ostream &  cout 
)

在文件 pat.cpp4016 行定义.

4016 {
4017 int n;
4018 int m;
4019 cin >> n >> m;
4020 if(n == 0) {
4021 return 0;
4022 }
4023 unordered_map<string, node *> um;
4024 for(int i = 0; i < m; i++) {
4025 string id;
4026 int k;
4027 cin >> id >> k;
4028 if(!um.contains(id)) {
4029 um[id] = new node(id);
4030 }
4031 auto *const nd = um[id];
4032 for(int j = 0; j < k; j++) {
4033 string cid;
4034 cin >> cid;
4035 if(!um.contains(cid)) {
4036 um[cid] = new node(cid);
4037 }
4038 nd->children.insert(um[cid]);
4039 }
4040 }
4041 queue<pair<node *, int>> q;
4042 q.push(make_pair(um["01"], 0));
4043 if(um["01"] == nullptr) {
4044 cout << 1;
4045 return 0;
4046 }
4047 int current_level = 0;
4048 int leaf_cnt = 0;
4049 while(!q.empty()) {
4050 auto [nd, level] = q.front();
4051 q.pop();
4052 if(level != current_level) {
4053 if(current_level != 0) {
4054 cout << ' ';
4055 }
4056 current_level = level;
4057 cout << leaf_cnt;
4058 leaf_cnt = 0;
4059 }
4060 if(nd->children.empty()) {
4061 leaf_cnt++;
4062 }
4063 for(auto *c: nd->children) {
4064 q.push(make_pair(c, level + 1));
4065 }
4066 }
4067 cout << ' ' << leaf_cnt;
4068 return 0;
4069 }

被这些函数引用 TEST().

◆ TEST()

pat::a::a1004::TEST ( a1004  ,
case1   
)

在文件 pat_test.cpp1984 行定义.

1984 {
1985 istringstream in("2 1\n"
1986 "01 1 02");
1987 auto out = ostringstream();
1988 main(in, out);
1989 ASSERT_EQ("0 1", out.str());
1990 }
int main(int argc, char **argv)
Definition: main.cpp:5

引用了 main().