hdu 4912 Paths on the tree LCA + 贪心

题意:给一棵树,有m 条paths, 问最多可以选取多少条 paths && 任意俩条path 没有公共的点;

思路:记 f = lca(u,v) ,按照 dep[f] 的大小去排序,优先处理 深度大的(离根节点远的,, 如果 u,v都是没有用过的,那么就把 以 f 为根的子数全部标记为用过;

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<string>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<list>
#include<map>
#include<set>
using namespace std;
const int N = 100010;
const int Max_step = 17;

int n, m;
vector <int> e[N];
int dep[N];
bool used[N];
int parent[Max_step][N];
struct Que{
    int i,f;
    Que(int i =0, int f = 0): i(i),f(f) {}

};
queue <Que> que;
struct In{
    int st,ed,f,dep;
    In(int st=0,int ed=0,int f=0,int dep=0):st(st),ed(ed),f(f),dep(dep) {}
};
vector <In> res;
void deal_init(){
    for(int i = 1; i <= n ; i++ ) e[i].clear();
    
    int a,b;
    for(int i = 1; i < n; i++){
        scanf("%d%d",&a,&b);
        e[a].push_back(b);
        e[b].push_back(a);
    }
}

void bfs(){
    que.push(Que(1,-1));
    dep[1]=0;
    while(!que.empty()){
        Que cur=que.front();que.pop();
        int i = cur.i,f = cur.f;
        parent[0][i] = f;
        vector<int> :: iterator it;
        for(it = e[i].begin(); it != e[i].end(); it++){
            if(*it != f){
                que.push(Que(*it, i));
                dep[*it] = dep[i] + 1;
            }
        }
    }
}
void deal_prelca(){
    bfs();
    for(int step = 0; step < Max_step-1; step++){
        for(int i = 1; i <= n; i++){
            int f = parent[step][i];
            if(f < 0) parent[step + 1][i] = f;
            else parent[step + 1][i] = parent[step][f];
        }
    }
}
int LCA(int u,int v){
    if(dep[u] > dep[v]) swap(u,v);
    for(int step = 0; step < Max_step; step++){
        int dis = dep[v] - dep[u];
        if(dis >> step & 1) v = parent[step][v];
    }
    if(u == v) return u;

    for(int step = Max_step - 1; step >= 0; step--){
        if(parent[step][u] != parent[step][v]){
            u = parent[step][u];
            v = parent[step][v];
        }
    }
    return parent[0][u];
}
bool cmp(In a,In b){
    return a.dep > b.dep;
}
void deal_res(){
    res.clear();
    int a, b,f;
    for(int i = 0; i < m; i++){
        scanf("%d%d",&a, &b);
        f = LCA(a,b);
        res.push_back(In(a,b,f,dep[f]));
    }
    sort(res.begin(), res.end(),cmp);

}
queue<int>mark;
void deal_mark(int st){
    if(used[st]) return ;
    mark.push(st);
    while(!mark.empty()){
        int i = mark.front(); mark.pop();
        if(used[i]) continue;
        used[i] = 1;
        vector<int> :: iterator it;
        for(it = e[i].begin(); it != e[i].end(); it++){
            if(dep[*it] < dep[i] || used[*it]) continue;
            else mark.push(*it);
        }
    }
}
void deal_ans(){
    int ans = 0;
    memset(used, 0, sizeof(used));

    vector<In> :: iterator it;
    for(it = res.begin(); it != res.end(); it++){
        int f = it -> f;
        int a = it -> st, b = it -> ed;
        if(used[a] == 0 && used[b] == 0) {
            ans++;
            deal_mark(f);
        }
    }
    cout << ans << endl;
}
int main()
{

//	freopen("in.in","r",stdin);
    while(~scanf("%d%d",&n, &m)){
        deal_init();
        deal_prelca();
        deal_res();
        deal_ans();
    }
	return 0;
}







Paths on the tree

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


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
 

Author
Xiaoxu Guo (ftiasch)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值