HDU 4912 LCA+策略

点击打开链接

题意:给了一个树,然后m条路径,问最多可以选多少条路径而没有一个点是重复使用的,如第二组样例,3条路径是4—2—5和6—3—7和2—1—3,那么只能选前两个使得所选路径最多

思路:没啥思路,看了正解竟然是LCA+贪心,好嘛可以这样考虑,对于所有的可选路径,我们先选择最下面的对上面是没有影响的,那么我们可以对每条路经的最上面的那个点进行排序,就按深度由大到小排序,然后这个最上面的点不就是LCA嘛,然后因为是由下到上的,那么对于每次之后就要将以LCA为根的子树节点全部标记,因为它作为深度最深的点以后它的下面在出现肯定是不符合的了       PS:主要是不敢去想用贪心,自己就是想当然的写根本不知道这么贪心对不对(弱哭)

#include <vector>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=100010;
bool vis[maxn];
int L[maxn*2],E[maxn*2],H[maxn],dis[maxn],dp[2*maxn][20],num[maxn],head[maxn],kkk,pre[maxn];
struct node{
    int to,next;
}EE[maxn*10];
void add_edge(int u,int v){
    EE[kkk].to=v;EE[kkk].next=head[u];head[u]=kkk++;
}
int k,n;
void dfs(int t,int deep){
    k++;E[k]=t;L[k]=deep;H[t]=k;
    for(int i=head[t];i!=-1;i=EE[i].next){
        int tt=EE[i].to;
        if(!vis[tt]){
            vis[tt]=1;pre[tt]=t;
            dfs(tt,deep+1);
            k++;E[k]=t;L[k]=deep;
        }
    }
}
void RMQ_init(){
    for(int i=1;i<=2*n-1;i++) dp[i][0]=i;
    for(int i=1;(1<<i)<=2*n-1;i++){
        for(int j=1;j+(1<<i)-1<=2*n-1;j++){
            if(L[dp[j][i-1]]<L[dp[j+(1<<(i-1))][i-1]]) dp[j][i]=dp[j][i-1];
            else dp[j][i]=dp[j+(1<<(i-1))][i-1];
        }
    }
}
int RMQ(int le,int ri){
    le=H[le];ri=H[ri];
    if(le>ri) swap(le,ri);
    int kk=0;
    while((1<<(kk+1))<=ri-le+1) kk++;
    if(L[dp[le][kk]]<L[dp[ri-(1<<kk)+1][kk]]) return E[dp[le][kk]];
    else return E[dp[ri-(1<<kk)+1][kk]];
}
struct edge{
    int f,x,y;
}tmp[maxn];
bool cmp(const edge &a,const edge &b){
    return L[H[a.f]]>L[H[b.f]];
}
void visdfs(int x){
    vis[x]=1;
    for(int i=head[x];i!=-1;i=EE[i].next){
        int t=EE[i].to;
        if(t==pre[x]||vis[t]) continue;
        visdfs(t);
    }
}
int main(){
    int q,u,v;
    while(scanf("%d%d",&n,&q)!=-1){
        for(int i=0;i<=n;i++) vis[i]=0;
        memset(head,-1,sizeof(head));
        for(int i=0;i<n-1;i++){
            scanf("%d%d",&u,&v);
            add_edge(u,v);add_edge(v,u);
        }
        kkk=0;k=0;vis[1]=1;dfs(1,1);RMQ_init();
        for(int i=1;i<=q;i++){
            scanf("%d%d",&u,&v);
            int en=RMQ(u,v);
            tmp[i].f=en;tmp[i].x=u;tmp[i].y=v;
        }
        int ans=0;
        for(int i=1;i<=n;i++) vis[i]=0;
        sort(tmp+1,tmp+1+q,cmp);
        for(int i=1;i<=q;i++){
            if(vis[tmp[i].x]==0&&vis[tmp[i].y]==0){
                ans++;
                int uu=tmp[i].x,vv=tmp[i].y,ff=tmp[i].f;
                visdfs(ff);
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-05 00:58:18

HDU 4912 LCA+策略的相关文章

HDU 4912 LCA+贪心

Paths on the tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 531    Accepted Submission(s): 182 Problem Description bobo has a tree, whose vertices are conveniently labeled by 1,2,…,n. Th

HDU 4912 lca贪心

Paths on the tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 297    Accepted Submission(s): 93 Problem Description bobo has a tree, whose vertices are conveniently labeled by 1,2,-,n. Th

HDU 4912 Paths on the tree LCA 排序贪心

lca... 排个序然后暴力保平安 _(:зゝ∠)_ #pragma comment(linker, "/STACK:102400000,102400000") #include"cstdio" #include"iostream" #include"set" #include"queue" #include"string.h" using namespace std; #define

hdu 4912 Paths on the tree(树链剖分+贪心)

题目链接:hdu 4912 Paths on the tree 题目大意:给定一棵树,和若干个通道,要求尽量选出多的通道,并且两两通道不想交. 解题思路:用树链剖分求LCA,然后根据通道两端节点的LCA深度排序,从深度最大优先选,判断两个节点均没被标 记即为可选通道.每次选完通道,将该通道LCA以下点全部标记. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include

HDU 2586 + HDU 4912 最近公共祖先

先给个LCA模板 HDU 1330(LCA模板) #include <cstdio> #include <cstring> #define N 40005 struct Edge{ int x,y,d,ne; }; Edge e[N*2],e2[N*2]; int be[N],be2[N],all,all2,n,m; bool vis[N]; int fa[N]; int ancestor[N][3]; int dis[N]; void add(int x, int y, int

HDU 4912 Paths on the tree

http://acm.hdu.edu.cn/showproblem.php?pid=4912 题意:给一棵树,再给一些路径,求最多有多少条路径不相交. 题解:主要是贪心的想法.用LCA处理出路径的层数,然后从最深处的节点往上找.因为节点越深,对其他路径影响度越小,相交的可能性越低.需要手动扩栈. 1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include <iostream> 3 #include

hdu 4912 Paths on the tree(树链拆分+贪婪)

题目链接:hdu 4912 Paths on the tree 题目大意:给定一棵树,和若干个通道.要求尽量选出多的通道,而且两两通道不想交. 解题思路:用树链剖分求LCA,然后依据通道两端节点的LCA深度排序,从深度最大优先选.推断两个节点均没被标 记即为可选通道. 每次选完通道.将该通道LCA下面点所有标记. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #includ

HDU 2874 LCA离线算法

Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4477    Accepted Submission(s): 1284 Problem Description After World War X, a lot of cities have been seriously damage

hdu 2586 LCA模板题(离线算法)

http://acm.hdu.edu.cn/showproblem.php?pid=2586 Problem Description There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B&quo