3417 - Network

TMD树剖过不掉。

#pragma GCC optimize("O2")
#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
using namespace std;
#define rep(i,j,k) for(i=j;i<=k;++i)
#define per(i,j,k) for(i=j;i>=k;--i)
#define G getchar()
#define LL long long
#define pll pair<int,int>
#define mkp make_pair
#define X first
#define Y second
const int N=100005;
int n,m;LL f[N],ans;
int he[N],ne[N<<1],to[N<<1],tot;
int sz[N],dad[N],son[N],cnt,pre[N],dep[N];
void read(int &x){
	char ch=getchar();
	while(ch<48||ch>57)ch=G;
	for(x=0;ch>=48&&ch<=57;ch=G)x=x*10+ch-48;
}
void add(int x,int y){
	to[++tot]=y;ne[tot]=he[x];he[x]=tot;
}
void DFS1(int x,int e){
	int y,i;sz[x]=1;
	for(i=he[x];i;i=ne[i])if(i!=e){
		pre[y=to[i]]=x;
		DFS1(y,i^1);
		if(sz[son[x]]<sz[y])son[x]=y;
		sz[x]+=sz[y];
	}
}
void DFS2(int x){
	int i,y;dep[x]=dep[pre[x]]+1;
	dad[x]=son[pre[x]]==x?dad[pre[x]]:x;
	if(son[x])DFS2(son[x]);
	for(i=he[x];i;i=ne[i])if((y=to[i])!=pre[x])
		DFS2(y);
}
int lca(int x,int y){
	int fx=dad[x],fy=dad[y];
	while(fx!=fy)
		if(dep[fx]>dep[fy])fx=dad[x=pre[fx]];
		else fy=dad[y=pre[fy]];
	return dep[x]<dep[y]?x:y;
}
void dp(int x){
	int i,y;
	for(i=he[x];i;i=ne[i])if((y=to[i])!=pre[x]){
		dp(y);f[x]+=f[y];
	}
	if(f[x]==1)++ans;
	else if(!f[x]&&x>1)ans+=m;
}
int main(){
	int i,x,y;
	read(n);read(m);tot=1;
	rep(i,2,n){
		read(x);read(y);
		add(x,y);add(y,x);
	}
	DFS1(1,0);DFS2(1);
	rep(i,1,m){
		read(x);read(y);
		++f[x];++f[y];f[lca(x,y)]-=2;
	}
	dp(1);printf("%lld\n",ans);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值