POJ1470
来源:互联网 发布:html5 php 交互 编辑:程序博客网 时间:2024/06/11 23:14
LCA问题的离线tarjan算法
#include<cstdio>#include<cstring>#include<vector>#define MAXN 1000#define MAXM 500000using namespace std;int n;int v[MAXN],root[MAXN],question[MAXN][MAXN],vi[MAXN],cnt[MAXN],f[MAXN];vector<int>vt[MAXN];int find(int x){return (x==root[x])?x:(root[x]=find(root[x]));}void un(int x,int y){x=find(x);y=find(y);root[x]=y;}void dfs(int x,int fa){root[x]=x;for(int i=0;i<vt[x].size();i++){int y=vt[x][i];dfs(y,x);un(y,x);//访问完一个节点再把它和父亲连起来}vi[x]=1;for(int i=1;i<=n;i++){if(vi[i]&&question[x][i]){int tp=find(i);cnt[tp]+=question[x][i];question[x][i]=question[i][x]=0;}}}int main(){while(1==scanf("%d",&n)){memset(v,0,sizeof(v));memset(vi,0,sizeof(vi));memset(cnt,0,sizeof(cnt));for(int i=1;i<=n;i++)vt[i].clear();for(int i=0;i<n;i++){int a,k,b;scanf("%d:(%d)",&a,&k);for(int j=0;j<k;j++){scanf("%d",&b);v[b]=1;vt[a].push_back(b);}}//for(int i=1;i<=n;i++)//printf("%d\n",vt[i].size());int qu;scanf("%d",&qu);getchar();for(int i=0;i<qu;i++){int a,b;char c;while(scanf("%c",&c)&&c!='(');scanf("%d%d",&a,&b);while(scanf("%c",&c)&&c!=')');question[a][b]++;question[b][a]++;}for(int i=1;i<=n;i++){if(!v[i]){dfs(i,0);break;}}for(int i=1;i<=n;i++)if(cnt[i])printf("%d:%d\n",i,cnt[i]);}return 0;}
- POJ1470
- poj1470
- poj1470
- LCA+poj1470
- POJ1470 RMQLCA
- LCA问题 poj1330 / poj1470
- poj1470解题报告
- POJ1470--Closest Common Ancestors
- poj1470 Closest Common Ancestors
- poj1470 Closest Common Ancestors
- 【树结构】poj1470/ZOJ1141
- poj1470的输入
- POJ1470 Closest Common Ancestors LCA
- POJ1470翻译 最近公共祖先
- poj1470-Closest Common Ancestors(LCA)
- poj1470——Closest Common Ancestors//LCA
- POJ1470 倍增法在线求LCA
- POJ1470 Closest Common Ancestors 【Tarjan的LCA】
- hdu 2604 Queuing(矩阵乘法+递推)
- 风的世界里有海的七滴泪水
- php和lucene
- 无线网络基础知识
- HDU 3047 Zjnu Stadium (带权并查集)
- POJ1470
- 斐波那契查找
- hdu 3585 maximum shortest distance 二分小技巧
- C++ 简单代理实现
- js验证日期函数
- Fusion Charts Free 使用方法
- 若爱 就如桃花般地去爱
- 开源项目之Oracle驱动 OCILIB
- 修改vc6的目标文件输出路径