HDU-4912-Paths on the tree
来源:互联网 发布:windows美化大师 编辑:程序博客网 时间:2024/05/17 04:22
贪心+LCA离线算法
代码:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio>#include<cstring>#include<iostream>#include<vector>#include<algorithm>using namespace std;const int maxn=1e5+100;const int maxm=maxn*2;struct Node{ int u; int v; int id;}s[maxn];int n,m,dep[maxn],p[maxn],lca[maxn];int e,pnt[maxm],nxt[maxm],head[maxn];bool vis[maxn];vector<pair<int,int> > querys[maxn];bool cmp(Node a,Node b){ return dep[lca[a.id]]>dep[lca[b.id]];}void AddEdge(int u,int v){ pnt[e]=v;nxt[e]=head[u];head[u]=e++;}void Init(){ e=0; memset(head,-1,sizeof(head)); memset(dep,-1,sizeof(dep)); memset(vis,0,sizeof(vis)); for(int i=0;i<=n;i++) querys[i].clear();}int find(int x){ if(p[x]==x) return x; return p[x]=find(p[x]);}void DFS(int u,int f){ p[u]=u; for(int i=0;i<querys[u].size();i++) { if(dep[querys[u][i].first]==-1) continue; lca[querys[u][i].second]=find(querys[u][i].first); } for(int i=head[u];i!=-1;i=nxt[i]) if(pnt[i]!=f) { dep[pnt[i]]=dep[u]+1; DFS(pnt[i],u); p[pnt[i]]=u; }}void dfs(int u){ vis[u]=1; for(int i=head[u];i!=-1;i=nxt[i]) if(!vis[pnt[i]]&&dep[pnt[i]]>dep[u]) dfs(pnt[i]);}int main(){ while(scanf("%d%d",&n,&m)!=EOF) { Init(); for(int i=1;i<n;i++) { int u,v; scanf("%d%d",&u,&v); AddEdge(u,v); AddEdge(v,u); } for(int i=0;i<m;i++) { scanf("%d%d",&s[i].u,&s[i].v); s[i].id=i; querys[s[i].u].push_back(make_pair(s[i].v,i)); querys[s[i].v].push_back(make_pair(s[i].u,i)); } dep[1]=0; DFS(1,-1); sort(s,s+m,cmp); int ans=0; for(int i=0;i<m;i++) { if(!vis[s[i].u]&&!vis[s[i].v]) { ans++; dfs(lca[s[i].id]); } } printf("%d\n",ans); } return 0;}
0 0
- HDU 4912 Paths on the tree
- hdu 4912 Paths on the tree
- hdu 4912Paths on the tree (LCA)
- HDU-4912-Paths on the tree
- HDU 4912 Paths on the tree LCA 排序贪心
- hdu 4912 Paths on the tree(lca+贪心)
- hdu 4912 Paths on the tree lca+贪心
- hdu 4912 Paths on the tree LCA + 贪心
- HDU 4912 - Paths on the tree (LCA 贪心)
- hdu 4912 Paths on the tree (LCA+贪心)
- HDU 4912 Paths on the tree (LCA+贪心)
- HDU 4912 Paths on the tree 贪心+lca
- hdu 4912 Paths on the tree(树链剖分+贪心)
- 【HDU】4912 Paths on the tree 离线LCA+贪心
- HDU 4912(Paths on the tree-树上取链,贪心)
- HDU 4912 Paths on the tree(LCA+贪心)
- hdu4912 Paths on the tree --- LCA贪心
- ZOJ 3863Paths on the Tree
- codeforce 20140808 C. Boredom
- poj 2774 最长公共子串--字符串hash或者后缀数组或者后缀自动机
- 缓存算法
- 常用的正则查找表达式
- 防止基本的web安全问题的方法与介绍
- HDU-4912-Paths on the tree
- 从setContentView()谈起
- 6.jQuery UI 自动补全UI
- ufldl学习笔记与编程作业:Multi-Layer Neural Network(多层神经网络+识别手写体编程)
- Mac远程登录Windows
- 事情还没开始前总会难一点
- Subsets
- leetcode 虐我篇之(十三)Maximum Subarray
- 字符串的压缩算法