2014多校5(1002)hdu4912(贪心+LCA)

Paths on the tree

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 424    Accepted Submission(s): 127


Problem Description
bobo has a tree, whose vertices are conveniently labeled by 1,2,…,n.

There are m paths on the tree. bobo would like to pick some paths while any two paths do not share common vertices.

Find the maximum number of paths bobo can pick.
 

Input
The input consists of several tests. For each tests:

The first line contains n,m (1≤n,m≤10 5). Each of the following (n - 1) lines contain 2 integers a i,b i denoting an edge between vertices a i and b i (1≤a i,b i≤n). Each of the following m lines contain 2 integers u i,v i denoting a path between vertices u i and v i (1≤u i,v i≤n).
 

Output
For each tests:

A single integer, the maximum number of paths.
 

Sample Input
   
   
3 2 1 2 1 3 1 2 1 3 7 3 1 2 1 3 2 4 2 5 3 6 3 7 2 3 4 5 6 7
 

Sample Output
   
   
1 2

题意:给出了m条路径,需要你选择尽量多的路径使得两两之间没有公共点

思路:以任意一点为根dfs树,然后优先选择路径中最小深度的节点(可以用LCA求)最深的路径,每次选完一条路径以后将该路径中以最小深度节点为根的子树标记(之后不能再选),然后就没了。

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         #pragma comment(linker, "/STACK:16777216") using namespace std; #define maxn 100010 int m,n; int head[maxn]; int edge[maxn<<1]; int next[maxn<<1]; int d; int cent[maxn]; int dp[maxn][20]; int fa[maxn]; int vis[maxn]; struct node{ int i; int p; int u; int v; }; node vex[maxn]; int maxi(int x,int y) { return x>y?x:y; } void add(int u,int v) { edge[d]=v; next[d]=head[u]; head[u]=d++; } void dfs(int u,int pre,int ce) { int i; cent[u]=ce; fa[u]=pre; for(i=head[u];i!=-1;i=next[i])if(edge[i]!=pre)dfs(edge[i],u,ce+1); } void dfs2(int u,int pre) { int i; vis[u]=1; for(i=head[u];i!=-1;i=next[i])if(!vis[edge[i]]&&edge[i]!=pre)dfs2(edge[i],u); } void LCA() { int i,j; memset(dp,0,sizeof(dp)); for(i=1;i<=n;i++)dp[i][0]=fa[i]; for(j=1;j<=19;j++) for(i=1;i<=n;i++)dp[i][j]=dp[dp[i][j-1]][j-1]; } int query(int u,int v) { int temp,i; if(u==v)return u; if(cent[u] 
        
          =0;i--)if(dp[u][i]!=dp[v][i]){ u=dp[u][i]; v=dp[v][i]; } return fa[u]; } bool cmp(node a,node b) { return a.i>b.i; } int main() { while(scanf("%d%d",&n,&m)!=EOF) { int i,j,u,v; d=0; memset(head,-1,sizeof(head)); memset(vis,0,sizeof(vis)); for(i=1;i 
          
         
       
      
      
     
     
    
    
   
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值