2012年6月19日

hdu 3072 Intelligence System

摘要: http://acm.hdu.edu.cn/showproblem.php?pid=3072题意:在一个有向图中,有多个连通分量,每个连通 分量都会有一个根节点,求当满足每个根节点都可以到其它根节点时的最小权值。环中的间的权值都为0.思路:因为有环,而已环中的权值为0,所以要进行缩点,缩点后重建图,所得到的图将没有环。然后枚举每个根节点,进行记忆化搜索就好了。View Code #include<stdio.h>#include<string.h>#include<iostream>#include<stack>#include<utili 阅读全文

posted @ 2012-06-19 21:30 aigoruan 阅读(171) 评论(0) 推荐(0)

hdu 1827 Summer Holiday

摘要: http://acm.hdu.edu.cn/showproblem.php?pid=1827题目意思:在一棵有向图中,每个点都有一个权值,求有多少个连通分量;在某个连通分量中,求出可以遍历这个连通分量的点,使得这个点的权值最小。思路:如果没有环,则起点就是所要求的点。如果有环且环上的点都能遍历这个边通分量,则选择环上权值最小的点。定义:每次选择的初始点为根。选择没有遍历过的点作为根,进行tarjan遍历,如果一开始就进入了环,则要把整个环都标记到根上,取环上的最小值作为根的权值。如果发现可以到达其它根,则把该根去掉。View Code #include<string.h>#incl 阅读全文

posted @ 2012-06-19 19:44 aigoruan 阅读(380) 评论(0) 推荐(0)

hdu 3594 Cactus

摘要: http://acm.hdu.edu.cn/showproblem.php?pid=3594这是一个很好的题目,很巧的利用的low[u]和dfn[u]。如果low[u]!=dfn[u],则说明已经存在环,如果我们访问u点时发现low[u]!=dfn[u],则说明有一条边在两个环上了。View Code #include<stdio.h>#include<string.h>#include<iostream>#include<stack>#include<algorithm>#include<utility>using na 阅读全文

posted @ 2012-06-19 17:35 aigoruan 阅读(226) 评论(2) 推荐(0)

hdu 2874 Connections between cities

摘要: http://acm.hdu.edu.cn/showproblem.php?pid=2874裸LCA,处理森林的无向图。思路:给每棵树一个唯一的编号,查LCA的时候,判断一下是不是在同一棵树下就行了。View Code #include<stdio.h>#include<string.h>#include<iostream>#include<vector>#include<iostream>#include<algorithm>#pragma comment(linker, "/STACK:36777216&qu 阅读全文

posted @ 2012-06-19 15:35 aigoruan 阅读(182) 评论(0) 推荐(0)

hdu 2586 How far away ?

摘要: http://acm.hdu.edu.cn/showproblem.php?pid=2586也是一道很裸的LCA,只不过卡了一下栈,要自己开大点栈才能过(c++)。思路:选1为根节点,先得到从根节点出发到每个点的距离,这样任意两个点的距离为:dis(u)+dis(v)-2*dis(lca(u,v)).View Code #include<stdio.h>#include<string.h>#include<iostream>#include<stdlib.h>#include<vector>#include<algorithm& 阅读全文

posted @ 2012-06-19 11:58 aigoruan 阅读(205) 评论(0) 推荐(0)

poj 1470 Closest Common Ancestors

摘要: http://poj.org/problem?id=1470裸LCA:读入数据很诡异。View Code #include<stdio.h>#include<string.h>#include<iostream>#include<vector>#include<algorithm>#include<stdlib.h>using namespace std;const int maxn = 1000;struct nd{ int to,next;}edge[maxn*2];vector<int>qes[maxn] 阅读全文

posted @ 2012-06-19 10:45 aigoruan 阅读(176) 评论(0) 推荐(0)

poj 1330 tarjan lca

摘要: http://poj.org/problem?id=1330tarjan 求解lca主要利用并差集的想法:首先遍历树,从叶子节点开始向上合并成一棵棵的子树,然后子树并子树,就成了一棵树了。查找是在合并的时候进行的,exp:u是s,t的lca,先从u节点进入s,把s并到u下面,然后发现t没有被访问,退到u,再进入t,同样把t并到u下面,发现s被访问过了,那么s的lca也就是s,t的lca了,也就是并差集里的f[s]。当然,f[s]会变的:假设当前f[s] = u; f[u] = u; 当u并到v的时候,也就是f[u]=v; 相应的f[s] = v。这也就是tarjan求解lca的关键方法。这个题 阅读全文

posted @ 2012-06-19 09:58 aigoruan 阅读(244) 评论(0) 推荐(0)

导航