ZOJ-1141
来源:互联网 发布:视频变速播放软件 编辑:程序博客网 时间:2024/05/12 22:48
求树中两点最近公共祖先,本题我是用的最直接的求两点路径,然后从根开始遍历求公共点,这种算法是最搓的。。在ZOJ上可以过,POJ上就TLE。。有空看看LCA相关算法
#include<cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<deque>#include<map>using namespace std;int main(){ int n, m; deque<int> P, Q; map<int, int> M; while (scanf("%d", &n) != EOF) { int *p = (int *) malloc((n + 1) * sizeof(int)); for (int i = 1; i <= n; i++) p[i] = 0; for (int i = 0; i < n; i++) { int v, c, t; scanf("%d:(%d)", &v, &c); while (c--) { scanf("%d", &t); p[t] = v; } } scanf("%d", &m); int a, b; M.clear(); while (m--) { cin >> ws; scanf("(%d,%d)", &a, &b); P.clear(); Q.clear(); while (a) { P.push_front(a); a = p[a]; } while (b) { Q.push_front(b); b = p[b]; } int res; while (!P.empty() && !Q.empty() && P.front() == Q.front()) { res = P.front(); P.pop_front(); Q.pop_front(); } if (M.find(res) != M.end()) M[res]++; else M[res] = 1; } for (map<int, int>::iterator it = M.begin(); it != M.end(); it++) printf("%d:%d\n", (*it).first, (*it).second); free(p); } return 0;}
0 0
- ZOJ-1141
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- STL_map——map::erase
- CDialog::DoModal()的返回值 和 自定义返回值
- Linux join命令
- 抽象和模型
- 一些程序员常去的网站
- ZOJ-1141
- spring 加载配置文件
- Android中Handler的介绍
- orcale监听错误解决
- CDialog::DoModal()的返回值 和 自定义返回值
- jQuery选择器大全(48个代码片段+21幅图演示)
- STL_map——map::find
- MFC C++ 获取外网IP地址
- CDialog::DoModal()的返回值 和 自定义返回值