POJ 1330 解题报告
2014-12-15 03:34
369 查看
这道题似乎是一道很经典的问题,有经典的解法:Tarjan或是RMQ。但是似乎都不是一下能懂。。。
我用的是naive的方法。
我用的是naive的方法。
1330 | Accepted | 524K | 16MS | C++ | 1375B |
/* ID: thestor1 LANG: C++ TASK: poj1330 */ #include <iostream> #include <fstream> #include <cmath> #include <cstdio> #include <cstring> #include <limits> #include <string> #include <vector> #include <list> #include <set> #include <map> #include <queue> #include <stack> #include <algorithm> #include <cassert> using namespace std; const int MAXN = 100000; int getheight(int c, int parent[]) { int h = 0; while (parent[c] >= 0) { c = parent[c]; h++; } return h; } void swap(int &a, int &b) { int tmp = a; a = b; b = tmp; } int main() { int T; scanf("%d", &T); int parent[MAXN]; for (int t = 0; t < T; ++t) { int N; scanf("%d", &N); for (int i = 0; i < N; ++i) { parent[i] = -1; } for (int i = 0; i < N - 1; ++i) { int p, c; scanf("%d%d", &p, &c); p--, c--; parent[c] = p; } int c1, c2; scanf("%d%d", &c1, &c2); c1--, c2--; // cout << "c1: " << c1 << ", c2: " << c2 << endl; int h1, h2; h1 = getheight(c1, parent), h2 = getheight(c2, parent); // cout << "h1: " << h1 << ", h2: " << h2 << endl; if (h1 < h2) { swap(h1, h2); swap(c1, c2); } while (h1 > h2) { c1 = parent[c1]; h1--; } while (c1 != c2) { c1 = parent[c1]; c2 = parent[c2]; } printf("%d\n", c1 + 1); } return 0; }
相关文章推荐
- POJ1330 Nearest Common Ancestors 非lca pascal 解题报告
- POJ-1330-Nearest Common Ancestors 解题报告
- 【POJ】【1033】【Defragment】【模拟】解题报告
- poj3461解题报告
- POJ-2262 Goldbach's Conjecture 解题报告(数论) 哥德巴赫的猜想
- POJ2251 Dungeon Master 解题报告
- POJ-2488 A Knight's Journey 解题报告(搜索) 马跳遍棋格的字典序问题
- 水题poj1423 解题报告
- poj1173 解题报告
- 【原】 POJ 1056 IMMEDIATE DECODABILITY Trie树查找前缀 解题报告
- POJ - 1129 Channel Allocation解题报告(涂色问题+四色定理)
- poj 1163 The Triangle之解题报告
- 【原】 POJ 2159 Tree Recovery 解题报告
- POJ-3083 Children of the Candy Corn 解题报告(搜索) 广搜深搜走迷宫
- 【原】 POJ 3080 Blue Jeans KMP 解题报告
- POJ 2418 解题报告
- poj 1734 Sightseeing trip hdu 1599 find the mincost route 最小环 解题报告
- poj1611解题报告
- POJ 1469 COURSES 解题报告
- poj 2109Power of Cryptography 解题报告