HDU 4409 - Family Name List(模拟树+lca)
来源:互联网 发布:网络种地实例 编辑:程序博客网 时间:2024/06/05 18:57
题目:
http://acm.hdu.edu.cn/showproblem.php?pid=4409
思路:
模拟输出树,暴力求解LCA
AC.
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>#include <set>#include <map>using namespace std;const int maxn = 30005;char s[70],ch[70];int pre[maxn],son[maxn],dep[maxn];char now[maxn][70];char name[maxn][70];map<string,int> idx;set<string> st;struct Edge{ int to,next;}edge[maxn];int head[maxn],tot,cnt;void init(){ memset(head,-1,sizeof(head)); tot = 0;}void add(int u,int v){ edge[tot].to = v; edge[tot].next = head[u]; head[u] = tot++;}set<string>::iterator ite;void dfs(int u){ st.clear(); int dfsci[maxn]; int num = 0; for(int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; st.insert(name[v]); } for(ite = st.begin(); ite != st.end(); ite++) dfsci[num++] = idx[*ite]; for(int i = 0; i < num; i++) { int v = dfsci[i]; for(int j = 0; j < dep[v]; j++) now[cnt][j] = '.'; now[cnt][dep[v]] = 0; strcat(now[cnt],name[v]); cnt++; dfs(v); }}int main(){ //freopen("in","r",stdin); int n; while(scanf("%d",&n) && n) { memset(pre,-1,sizeof(pre)); memset(son,0,sizeof(son)); idx.clear(); init(); for(int i = 1; i <= n; i++) { scanf("%s",s); int len = strlen(s); int step = 0, j; for(j = 0; j < len; j++) if(s[j] == '.') step++; else break; strncpy(ch,&s[j],len-step); ch[len - step] = 0; dep[i] = step; if(step == 0) pre[i] = 0,son[0] = 1; else { int tmp = idx[now[step-1]]; pre[i] = tmp; son[tmp]++; add(tmp,i); } strcpy(now[step],ch); idx[ch] = i; strcpy(name[i],ch); } strcpy(now[1],name[1]); cnt = 2; dfs(1); int q; scanf("%d",&q); while(q--) { scanf("%s",s); if(s[0] == 'L') { for(int i = 1; i < cnt; i++) printf("%s\n",now[i]); } else if(s[0] == 'b') { scanf("%s",s); int id = idx[s]; printf("%d\n",son[pre[id]]); } else if(s[0] == 'c') { scanf("%s%s",s,ch); int u = idx[s], v = idx[ch]; int g1 = u, g2 = v; while(u != v) { if(dep[u] < dep[v]) v = pre[v]; else if(dep[u] > dep[v]) u = pre[u]; else { u = pre[u]; v = pre[v]; } } if(u == g1 || u == g2) u = pre[u]; printf("%s\n", name[u]); } } } return 0;}
0 0
- HDU 4409 - Family Name List(模拟树+lca)
- hdu 4409 Family Name List(LCA&有坑点)
- HDU 4409 Family Name List --乱搞、LCA
- HDU-4409 Family Name List LCA求解,TC+DFS || tarjan
- hdu 4409 Family Name List
- Family Name List (HDU 4409)
- Hdu 4409 Family Name List (LCA 家谱 STL 2012金华网赛)
- HDU 4409 Family Name List 简单树操作
- 【模拟】 hdu4409 Family Name List
- hdu 4409 Family Name List 金华赛区1010 (解题报告)
- HDU 4409 大模拟 + LCA
- hdu 5266(线段树+LCA)
- HDU 2103 Family Plan
- hdu-2103-Family planning
- hdu 2103 Family planning
- hdu 2103 Family planning
- HDU 4169 Wealthy Family
- HDU 4169 Wealthy Family
- Linux-CentOS7 KDE 中文语言支持
- 10 个非常重要的 HotSpot JVM 参数
- C++Primer阅读留存问题
- java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor
- 自己理解的KMP
- HDU 4409 - Family Name List(模拟树+lca)
- jsp JSTL
- python twisted 浅析
- Android基础
- iOS开发UI篇-Quartz2D使用(绘图路径)
- Linux进程间通信方式--信号,管道,消息队列,信号量,共享内存
- Viewpager的setOnPageChangeListener方法详解
- 记录文件,要做的事情
- Monitor Object Monitors with Eclipse Object Monitor Monitors