hdu 2874 Connections between cities(LCA)


hdu 2874 Connections between cities


普通的LCA求最短距离,只不过此题存在不连通情况,并查集判断下


#include
#include
#include
using namespace std;
#define MAXN 10005
struct tnode
{
    int v,d;
    tnode(int v=0,int d=0):v(v),d(d){}
};
struct qnode
{
    int v,id;
    qnode(int v=0,int id=0):v(v),id(id){}
};
vector t[MAXN];
vector q[MAXN];
int dis[MAXN],fa[MAXN],con[MAXN],ans[1000005];
int n,m;
bool vis[MAXN];
void ini()
{
    for(int i=0;i<=n;i++)
    {
        t[i].clear();
        q[i].clear();
        fa[i]=con[i]=i;
        vis[i]=false;
    }
}
int find(int x,int *f) {return x==f[x]?x:f[x]=find(f[x],f);}
void setunion(int x,int y)
{
    int fx=find(x,con),fy=find(y,con);
    if(fx!=fy) con[fx]=fy;
}
void lca(int u)
{
    vis[u]=true;
    for(int i=0;i


你可能感兴趣的:(LCA,LCA)