poj1470 Closest Common Ancestors
来源:互联网 发布:win7无法启动网络共享 编辑:程序博客网 时间:2024/06/05 02:41
题意给出一颗树, 后给出m 组(u,v)查询;找到每组两个节点的最近公共祖先, 后按祖先数从小到大输出,最近公共祖先 和以其为祖先的数量
#include <iostream>#include <cstdio>#include <cstring>#include <vector>using namespace std;const int M = 1000;vector<int>que[M];int sign[M][M];int Set[M];int num[M];int vist[M];int rd[M];int n, m;void init() { memset(sign, 0, sizeof(sign)); for(int i = 0; i < M; i++){ //Set[i] = i; num[i] = 0; vist[i] = 0; rd[i] = 0; que[i].clear(); }}int Find(int x) { return x == Set[x] ? x : Set[x] = Find(Set[x]);}void LCA(int u) { int v; Set[u] = u; for(int i = 0; i < (int)que[u].size(); i++){ v = que[u][i]; LCA(v); Set[v] = u; } vist[u] = 1; for(int i = 1; i <= n; i++){ if(vist[i] && sign[u][i]){ num[Find(i)] += sign[u][i]; } }}int main(){ int a, b, c; while(scanf("%d", &n) != EOF) { init(); for(int i = 0; i < n; i++){ scanf("%d:(%d)", &a, &b); for(int j = 0; j < b; j++){ scanf("%d", &c); que[a].push_back(c); rd[c]++; } } scanf("%d", &m); for(int i = 0; i < m; i++){ scanf(" (%d%d)", &a, &b); //正确输入; //scanf("(%d%d)", &a, &b); //这种输入将得不到结果;因为双引号后没有空格; sign[a][b]++; sign[b][a]++; } for(int i = 1; i <= n; i++){ if(rd[i] == 0){ LCA(i); break; } } for(int i= 1; i <= n; i++){ if(num[i]) printf("%d:%d\n", i, num[i]); } } return 0;}
0 0
- POJ1470--Closest Common Ancestors
- poj1470 Closest Common Ancestors
- poj1470 Closest Common Ancestors
- POJ1470 Closest Common Ancestors LCA
- poj1470-Closest Common Ancestors(LCA)
- poj1470——Closest Common Ancestors//LCA
- POJ1470 Closest Common Ancestors 【Tarjan的LCA】
- poj1470 Closest Common Ancestors 离线LCA
- Closest Common Ancestors【POJ1470】——LCA
- POJ1470/ZOJ1141 Closest Common Ancestors(LCA离线算法)
- poj1470 Closest Common Ancestors LCA(最近公共祖先)
- 【LCA最近公共祖先】 poj1470 Closest Common Ancestors
- poj1470 Closest Common Ancestors(LCA离线优化)
- poj1470 Closest Common Ancestors Lca离线算法Tarjan
- poj1470(Closest Common Ancestors)
- Closest Common Ancestors
- G - Closest Common Ancestors
- Pku 1470 Closest Common Ancestors
- 在PHP中利用wsdl创建标准webservice的实现代码
- HDU4923 Room and Moor
- Memcache高可用共享session,容纳100万人同时在线
- Python模块简介之threading模块
- C++运算符重载的方法详细解析
- poj1470 Closest Common Ancestors
- ArcGIS for Server的SOE开发思路解析
- java 快速排序 时间复杂度 空间复杂度 稳定性
- UVA - 10192Vacation(LIS)
- 【linux】挂载命令mount
- OnCreate消息函数
- hdu 2795 Billboard(线段树-剩余空间)
- UVA1368- DNA Consensus String
- leetcode Palindrome Partitioning II