hdu 4409 Family Name List
2012-09-24 12:48
519 查看
/* Three operations L : the same as input, correspond to Preorder traversal. b x : correspond to ask how many sons does x's father have. c x y : LCA(x, y), there is a trick, if LCA(x, y) is x or y, you should output LCA(x, y)'s father, because in our real life, one can't be one's ancestor. About sorting the brothers We can use map to store their names. Then their names will be in alphabetical order. At last, Scan the map and construct the family tree. */ #include <cstdio> #include <cstdlib> #include <iostream> #include <cstring> #include <string> #include <vector> #include <map> using namespace std; //STL #define MS(c, a) memset(c, a, sizeof c) //Syntax #define Rep(c, a, b) for (int c = (a); c < (b); c++) //Constant const int MAXN = 30005 << 1; //Array typedef int AI[MAXN]; //FSG struct Edge{ int v, next; }; vector<Edge> E; AI L; void G_ini(){ E.clear(); MS(L, -1); } void AE(int u, int v){ Edge te = {v, L[u]}; L[u] = E.size(); E.push_back(te); } #define Ere(c, a, L) for (int c = L[a]; c != -1; c = E[c].next) //RMQ int ind; AI dep, ta, id, st[20]; void TTI(int u, int fa = -1) { if (fa != -1) dep[u] = dep[fa] + 1; id[u] = ind; ta[ind++] = u; for (int i = L[u]; i != -1; i = E[i].next) if (E[i].v != fa) { TTI(E[i].v, u); ta[ind++] = u; } } int low(int x) { int o = 0; while ((1 << o) <= x) o++; return o; } void st_ini() { Rep(i, 0, ind) st[0][i] = ta[i]; int l = low(ind); Rep(i, 1, l) Rep(j, 0, ind) if (j + (1 << i) < ind) { int x = st[i - 1][j], y = st[i - 1][j + (1 << (i - 1))]; st[i][j] = (dep[x] < dep[y])? x: y; } } int rmq(int x, int y) { x = id[x]; y = id[y]; if (x > y) swap(x, y); int l = low(y - x + 1) - 1; x = st[l][x]; y = st[l][y - (1 << l) + 1]; return (dep[x] < dep[y])? x: y; } int n, q; AI fat, now, son; char buf[MAXN]; string nam[MAXN]; map<string, int> H; map<string, int>::reverse_iterator Hi; void LT(int u, int d = 0) { Rep(i, 0, d) putchar('.'); printf("%s\n", nam[u].c_str()); Ere(i, u, L) LT(E[i].v, d + 1); } int main() { while (scanf("%d", &n), n) { MS(fat, -1); MS(now, -1); MS(son, 0); H.clear(); scanf("%s", buf); string ne = buf; H[ne] = 1; nam[now[0] = 0] = ne; Rep(i, 2, n + 1) { scanf("%s", buf); int k = 0; while (buf[k] == '.') k++; son[fat[i - 1] = now[k - 1]]++; H[ne = buf + k] = i; nam[now[k] = i - 1] = ne; } G_ini(); for (Hi = H.rbegin(); Hi != H.rend(); Hi++) { int v = Hi->second - 1, u = fat[v]; if (Hi->second > 1) AE(u, v); } MS(dep, 0); TTI(ind = 0); st_ini(); scanf("%d", &q); while (q--) { char op[3]; scanf("%s", op); if (op[0] == 'L') LT(0); if (op[0] == 'b') { scanf("%s", buf); ne = buf; int u = H[ne] - 1; if (!u) puts("1"); else printf("%d\n", son[fat[u]]); } if (op[0] == 'c') { scanf("%s", buf); ne = buf; int u = H[ne] - 1; scanf("%s", buf); ne = buf; int v = H[ne] - 1, p = rmq(u, v); if (p == u || p == v) p = fat[p]; printf("%s\n", nam[p].c_str()); } } } return 0; }
相关文章推荐
- hdu 4409 Family Name List LCA +stl
- hdu 4409 Family Name List
- Family Name List (HDU 4409)
- Hdu 4409 Family Name List (LCA 家谱 STL 2012金华网赛)
- HDU-4409 Family Name List LCA求解,TC+DFS || tarjan
- HDU 4409 - Family Name List(模拟树+lca)
- HDU 4409 Family Name List --乱搞、LCA
- HDU 4409 Family Name List 简单树操作
- hdu 4409 Family Name List 金华赛区1010 (解题报告)
- HDU 4409 Family Name List --乱搞、LCA
- HDU 4409 Family Name List ( 模拟 + LCA)
- HDU 4409 Family Name List(LCA)
- ACM网络赛金华赛区的一道关于树的题:Family Name List
- 【模拟】 hdu4409 Family Name List
- python django ImportError: cannot import name 'parse_http_list'
- HDU Phone List
- poj 3630 || zoj 2876 ||hdu 1671 Phone List (Trie树的应用)
- csharp read excel file get sheetName list
- HTTP Status 500 - tag 'select', field 'list', name 'vehicle.vehicleType.id': The requested list key
- HDU 1671 Phone List(字典树)