Paths on the tree
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 365 Accepted Submission(s): 111
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)
按路径的LCA深度排序,深的排在前面,先取深的,并标记。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <functional>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
//#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
#define INF 1e9
const int maxn = 100005;
//#define mod 1000000007
#define eps 1e-7
#define pi 3.1415926535897932384626433
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define scan(n) scanf("%d",&n)
#define scanll(n) scanf("%I64d",&n)
#define scan2(n,m) scanf("%d%d",&n,&m)
#define scans(s) scanf("%s",s);
#define ini(a) memset(a,0,sizeof(a))
#define out(n) printf("%d\n",n)
//ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b);}
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define MAXL 19
#define MAXN 100005
int dep[MAXN];
int dad[MAXN]; //dad[i]表示i号点的父亲编号
vector<int> T[MAXN]; //邻接表
int anc[MAXN][MAXL]; //anc[i][j]表示i号点向上2^j步所到达的点
int N,m;
void dfs(int n, int d = 0) { //得到父子关系
dep[n] = d;
for (int i = 0; i < T[n].size(); ++i) {
if (T[n][i] == dad[n]) continue;
dad[T[n][i]] = n;
dfs(T[n][i], d + 1);
}
}
void make_root(int r) {//计算anc数组
dad[r] = -1;
dfs(r);
for (int i = 0; i < N; ++i)
anc[i][0] = dad[i];
for (int j = 1; j < MAXL; ++j)
for (int i = 0; i < N; ++i)
anc[i][j] = anc[anc[i][j - 1]][j - 1];
}
int get(int n, int jump) { //快速得到n向上跳跃jump步所到达的点
for (int i = MAXL - 1; i >= 0; --i)
if (((jump >> i) & 1) == 1)
n = anc[n][i];
return n;
}
int lca(int a, int b) { //求a与b的LCA
if (dep[a] < dep[b]) swap(a, b);
a = get(a, dep[a] - dep[b]);
if (a == b) return a;
for (int i = MAXL - 1; i >= 0; --i) {
if (anc[a][i] != anc[b][i]) {
a = anc[a][i];
b = anc[b][i];
}
}
return dad[a];
}
struct Path
{
int u,v,lca;
bool operator<(const Path& b) const
{
return dep[lca] > dep[b.lca];
}
}q[maxn];
bool vis[maxn];
bool check(int u,int v)
{
int la = lca(u,v);
if(vis[la]) return false;
for(int i = u;i != la && i != -1; i = anc[i][0]) if(vis[i]) return false;
for(int i = v;i != la && i != -1; i = anc[i][0]) if(vis[i]) return false;
return true;
}
void Flag(int u,int v)
{
int la = lca(u,v);
vis[la] = 1;
for(int i = u;i != la && i != -1; i = anc[i][0]) vis[i] = 1;
for(int i = v;i != la && i != -1; i = anc[i][0]) vis[i] = 1;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif
while(~scanf("%d%d",&N,&m))
{
ini(vis);
rep(i,N) T[i].clear();
int u,v;
rep(i,N-1)
{
scan2(u,v);
u--;
v--;
T[u].push_back(v);
T[v].push_back(u);
}
make_root(0);
rep(i,m)
{
scan2(u,v);
u--,v--;
q[i].u = u;
q[i].v = v;
q[i].lca = lca(u,v);
}
sort(q,q+m);
int ans = 0;
rep(i,m)
{
u = q[i].u;
v = q[i].v;
if(check(u,v))
{
ans ++;
Flag(u,v);
}
}
cout<<ans<<endl;
}
return 0;
}