Input |
---|
2 101 1 02 |
Output |
0 1 |
Notes 作者 CHEN, Yue
树的节点不超过100个,用数组简单建树即可。注意题目已定义ID为”01”的节点为根节点。
#include <iostream>#include <algorithm>#include <map>#include <vector>#include <functional>#include <string>#include <cstring>#include <queue>#include <set>#include <stack>#include <cmath>#include <cstdio>#include <sstream>#include <iomanip>using namespace std;#define IOS ios_base::sync_with_stdio(false)#define TIE std::cin.tie(0)#define MIN2(a,b) (a<b?a:b)#define MIN3(a,b) (a<b?(a<c?a:c):(b<c?b:c))#define MAX2(a,b) (a>b?a:b)#define MAX3(a,b,c) (a>b?(a>c?a:c):(b>c?b:c))typedef long long LL;typedef unsigned long long ULL;const int INF = 0x3f3f3f3f;const double PI = 4.0*atan(1.0);const double eps = 1e-6;const int maxn = 105;struct Node{ vector<int> ve;}nodes[maxn];int n, m, x, f, ch, num;int hn[maxn], maxh;string id1, id2;map<string, int> ma;void dfs(int nn,int h){ if (h > maxh) maxh = h; Node & node = nodes[nn]; if (node.ve.empty()) hn[h]++; h++; for (int i = 0; i < node.ve.size(); i++){ dfs(node.ve[i], h); }}int main(){ IOS; cin >> n >> m; maxh = 0; ma["01"] = 0; num = 1; for (int i = 0; i < m; i++){ cin >> id1; if (ma.count(id1)){ f = ma[id1]; }else{ ma[id1] = num; f = num; num++; } cin >> x; for (int j = 0; j < x; j++){ cin >> id2; if (ma.count(id2)){ ch = ma[id2]; } else{ ma[id2] = num; ch = num; num++; } nodes[f].ve.push_back(ch); } } dfs(0, 0); for (int i = 0; i <= maxh-1; i++) printf("%d ", hn[i]); printf("%d/n", hn[maxh]); //system("pause");}新闻热点
疑难解答