贴一个我的,思路也是用邻接表保存节点的string和他自身是哪一个节点,然后串在每个父节点后面最后dfs。就是前缀的那些符号条件要仔细考虑。 #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; void DFS(vector<vector<pair<string,int>>> &dir, int root, string mask) { string next_mask; for (int i = 0; i < dir[root].size(); ++i) { pair<string, int> node = dir[root][i]; cout << mask; if (i != dir[root].size() - 1) { cout << "|-- "; } else { cout << "`-- "; } cout << node.first << endl; if (i == dir[root].size() - 1) { next_mask = " " + mask; } else { next_mask = mask + "| "; } DFS(dir, node.second, next_mask); } } bool cmp(pair<string, int> a, pair<string, int> b) { return a.first < b.first; } int main() { int n; cin >> n; vector<vector<pair<string,int>>> dir(n + 1); for (int i = 0; i < n; ++i) { string node_s; int fa_idx; pair<string, int> node; cin >> node_s >> fa_idx; node.first = node_s; node.second = i+1; dir[fa_idx + 1].push_back(node); } for (int i = 0; i < dir.size(); ++i) { sort(dir[i].begin(), dir[i].end(),cmp); } cout << dir[0][0].first << endl; DFS(dir, 1, ""); return 0; }
点赞 1

相关推荐

03-26 22:55
门头沟学院 Java
烤冷面在迎接:河南byd,应该就是郑大了。不过24届计算机是特殊情况,那年除了九✌和强2,以及两三个关系够硬的双非,其他的都是炮灰,感觉是十几年来互联网行业最烂的一年,如果想了解最新的就业情况,得找现在的大四。
点赞 评论 收藏
分享
牛客网
牛客企业服务