Paths on the tree
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1594 Accepted Submission(s): 552
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.
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).
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.
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)
Source
题意:给出一个含有n个点的树,再给出m条路径,问m个路径中任意两条路径都不相交的子集集合最大是多少?
题解:预处理出每条路径的lca,对lca按深度排序,优先选择lca深度更大的路径,每次选择后标记整个lca的子树.
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int MAXN=401000;
const int mod=1e9+7;
struct edge{
int to,next;
}e[MAXN<<1];
int head[MAXN];
int tot=0;
void add(int u,int v){
e[tot].to=v;
e[tot].next=head[u];
head[u]=tot++;
}
int dep[MAXN];
int fir[MAXN];
int que[MAXN<<1];
int p[MAXN];
int st[MAXN][20];
int vis[MAXN];
int cnt=0;
void dfs(int u,int d){
vis[u]=1;
que[++cnt]=u;fir[u]=cnt;dep[cnt]=d;
for(int k=head[u];k!=-1;k=e[k].next){
int v=e[k].to;
if(!vis[v]){
p[v]=u;
dfs(v,d+1);
que[++cnt]=u;dep[cnt]=d;
}
}
}
void ST(int n){
for(int i=1;i<=n;i++)
st[i][0]=i;
for(int j=1;(1<<j)<=n;j++){
for(int i=1;i+(1<<j)-1<=n;i++){
int a=st[i][j-1],b=st[i+(1<<(j-1))][j-1];
st[i][j]=dep[a]<dep[b]?a:b;
}
}
}
int RMQ(int l,int r){
int k=0;
while((1<<(k+1))<=r-l+1){
k++;
}
int a=st[l][k],b=st[r-(1<<k)+1][k];
return dep[a]<dep[b]?a:b;
}
int lca(int u,int v){
int x=fir[u],y=fir[v];
if(x>y)
swap(x,y);
int res=RMQ(x,y);
return que[res];
}
struct path{
int u,v;
int lca;
}pa[MAXN];
int cmp(path x,path y){
return dep[fir[x.lca]]>dep[fir[y.lca]];
}
int used[MAXN];
void init(){
tot=0;
cnt=0;
p[1]=0;
memset(head,-1,sizeof(head));
memset(vis,0,sizeof(vis));
memset(used,0,sizeof(used));
}
bool check(int u,int v,int lca){
if(used[u]||used[v]){
return false;
}
if(u==lca&&v==lca){
return true;
}
if(u!=lca)
u=p[u];
if(v!=lca)
v=p[v];
return check(u,v,lca);
}
void USE(int s){
if(used[s])
return;
used[s]=1;
for(int k=head[s];k!=-1;k=e[k].next){
int v=e[k].to;
if(v!=p[s]&&!used[v]){
USE(v);
}
}
}
int main(){
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
init();
for(int i=0;i<n-1;i++){
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs(1,1);
ST(2*n-1);
for(int i=0;i<m;i++){
scanf("%d%d",&pa[i].u,&pa[i].v);
pa[i].lca=lca(pa[i].u,pa[i].v);
}
sort(pa,pa+m,cmp);
int res=0;
for(int i=0;i<m;i++){
if(check(pa[i].u,pa[i].v,pa[i].lca)){
res++;
USE(pa[i].lca);
}
}
printf("%d\n",res);
}
}