并查集
模板:
int Find(int x)
{
return (fa[x]==x)?x:(fa[x]=find(fa[x]));
}
void Union(int a,int b)
{
p1=Find(a); p2=Find(b);
if(p1!=p2) fa[p1]=p2;
}
类别偏移:小胖的奇偶(poj1733)、食物链(poj1182)、黑帮团伙(poj1703)
**带权并查集:**poj1962、银河英雄传
**其他:**poj1417(并查集+dp)、poj1456(贪心+并查集)
拓扑
一般用于判断有向环,及AOV,有Kahn算法和dfs算法
模板
l=1; r=0;
for (i=1; i<=n; i++)
if (d[i]==0) {r++; q[r]=i;}
while (l<=r)
{
x=q[l]; j=head[x];
while (j>0)
{
v=e[j].v;
/*
主体
*/
d[v]--;
if (d[v]==0)
{
r++; q[r]=v;
}
j=e[j].nx;
}
l++;
}
题目:重叠的方块(poj1128)(搜索+拓扑)、比赛名次(hdu1285)、长方体(hdu3231)(三维拓扑)、rank of tetris(hdu1811)(拓扑+并查集)