poj 1470 LCA基础题
来源:互联网 发布:linux mv命令 文件夹 编辑:程序博客网 时间:2024/06/05 04:11
传送门
题意:求最近公共祖先,按编号,如果是输出编号和个数。
思路:一般思路,记录个数。
跑了900+ms。。。。。。。
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int n,m,fst[1000],next[1000],node[1000],x[1000000],y[1000000],num,rot[1000],cnt[1000],father[1000];bool vis[1000];int find(int i){ if(father[i]==i)return i; else return find(father[i]);}void LCA(int u){ father[u]=u; for(int i=fst[u];i!=-1;i=next[i]) { LCA(node[i]); father[node[i]]=u; } vis[u]=1; for(int i=0;i<m;i++) { if(u==x[i]&&vis[y[i]]) { cnt[find(y[i])]++; } else if(u==y[i]&&vis[x[i]]) { cnt[find(x[i])]++; } }}int main(){ int u,v; while(scanf("%d",&n)!=EOF) { num=0; memset(fst,-1,sizeof(fst)); memset(vis,0,sizeof(vis)); memset(rot,0,sizeof(rot)); memset(cnt,0,sizeof(cnt)); for(int i=0;i<n;i++) { scanf("%d:(%d)",&u,&m); for(int j=0;j<m;j++) { scanf("%d",&v); next[++num]=fst[u]; fst[u]=num; node[num]=v; rot[v]=1; } } scanf("%d",&m); for(int i=0;i<m;i++) { while(getchar()!='('); scanf("%d %d)",&x[i],&y[i]); } for(int i=1;i<=n;i++) { if(!rot[i]) { LCA(i); break; } } for(int i=1;i<=n;i++) { if(cnt[i]) { printf("%d:%d\n",i,cnt[i]); } } } return 0;}
- poj 1470 LCA基础题
- 【LCA基础】poj 1470 Closest Common Ancestors
- poj 1470(LCA模板题)
- poj 1470--tarjan--LCA
- POJ 1470 LCA
- poj 1470(LCA)
- POJ 1470 LCA 公共祖先
- poj 1470(LCA RE)
- poj 1470 LCA 离线算法
- poj 1470 LCA倍增 裸
- poj 1330 (LCA模板题)
- 【LCA模板题】POJ 1330
- poj 1330 LCA模版题
- 【poj】lca模板题 poj1330
- POJ 1470 Closest Common Ancestors(LCA入门题)
- POJ-1470 Closest Common Ancestors【LCA】
- POJ-1470 Closest Common Ancestors【LCA】
- POJ 1470 Closest Common Ancestors [LCA+RMQ]
- Upgrading the HMC from Version 7.3.x or Version 7.7.x to Version 7.7.7
- 加班与效率
- unity 协同,调用——MonoBehaviour类Invoke, Coroutine
- 实现美观的TabHost
- WebDriver中close()与quit()的不同
- poj 1470 LCA基础题
- 二叉树递归遍历应用
- ios项目开发笔记
- LAMP(Linux+Apache+Mysql+PHP)优化技巧
- 开通此博,用于记录与分享
- 64位centos安装bugfree的方法
- extjs利用第三方打印插件实现打印功能
- hdu(1128)
- 如何在sping3.1 MVC中应用aspect注解之AOP