poj 1470 Closest Common Ancestors(LCA)
来源:互联网 发布:一维数组的使用 编辑:程序博客网 时间:2024/05/09 07:07
poj 1470 Closest Common Ancestors
统计每个点成为LCA的次数
基础的LCA,但是输入好恶心啊、、、
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>using namespace std;#define MAXN 905vector<int> t[MAXN];vector<int> q[MAXN];int n,m;int fa[MAXN],cnt[MAXN];bool vis[MAXN];void ini(){ for(int i=0;i<=n;i++) { t[i].clear(); q[i].clear(); fa[i]=i; cnt[i]=0; vis[i]=false; }}int find(int x) {return x==fa[x]?x:fa[x]=find(fa[x]);}void lca(int u){ vis[u]=true; for(int i=0;i<q[u].size();i++) { int v=q[u][i]; if(vis[v]) { //printf("%d--%d--%d\n",u,v,find(v)); cnt[find(v)]++; } } for(int i=0;i<t[u].size();i++) { int v=t[u][i]; if(!vis[v]) { lca(v); fa[v]=find(u); } }}int main(){ while(scanf("%d",&n)!=EOF) { ini(); int u,v,nn; for(int i=0;i<n;i++) { scanf("%d:(%d)",&u,&nn); while(nn--) { scanf("%d",&v); vis[v]=true; t[u].push_back(v); t[v].push_back(u); } } scanf("%d",&m); while(m--) { while(getchar()!='('); scanf("%d %d",&u,&v); while(getchar()!=')'); q[u].push_back(v); q[v].push_back(u); } //找根 for(int i=1;i<=n;i++) if(!vis[i]) { for(int j=0;j<=n;j++) vis[j]=false; lca(i); break; } for(int i=1;i<=n;i++) if(cnt[i]) printf("%d:%d\n",i,cnt[i]); } return 0;}
0 0
- POJ-1470 Closest Common Ancestors【LCA】
- POJ-1470 Closest Common Ancestors【LCA】
- POJ 1470 Closest Common Ancestors [LCA+RMQ]
- poj 1470 Closest Common Ancestors(LCA)
- poj--1470--Closest Common Ancestors--LCA
- POJ 1470 Closest Common Ancestors LCA题解
- Closest Common Ancestors - POJ 1470 LCA
- poj 1470 Closest Common Ancestors(LCA)
- 【LCA】 POJ 1470 Closest Common Ancestors
- poj 1470 Closest Common Ancestors(LCA)
- POJ - 1470 Closest Common Ancestors(LCA离线)
- POJ 1470Closest Common Ancestors(LCA)
- POJ 1470 Closest Common Ancestors【LCA Tarjan】
- [POJ 1470]Closest Common Ancestors[离线LCA]
- poj 1470 Closest Common Ancestors LCA离线
- POJ 1470 Closest Common Ancestors (LCA)
- 【LCA基础】poj 1470 Closest Common Ancestors
- POJ 1470 Closest Common Ancestors 离线LCA
- Xcode + SourceTree + Bitbucket - 轻松云端作业
- 今年没有尿红墙(组图)
- 南方都市报副总编辑王钧
- 黑马程序员——关于类的学习
- 巨型杀人蟹蜘蛛蟹形似蜘蛛 堪称令人惶恐的节奏
- poj 1470 Closest Common Ancestors(LCA)
- poj1008-Maya Calendar(水)
- HDU1068 Girls and Boys 【最大独立集】
- 肖央称对“筷子兄弟”解散谣言不知情 歌迷: 一根筷子咋吃饭?
- poj1012-Joseph
- 黑马程序员——什么是API
- 使用pdiusbd12制作包含HID接口的多接口USB设备
- SDUTOJ 2772 KMP简单应用
- Cocos2d-x 3.0final 终结者系列教程23CocosStudio UI组件使用大全Cocos2d-x3.2使用