记录编号 429222 评测结果 AAAAAAAAA
题目名称 暗之链锁 最终得分 100
用户昵称 GravatarHallmeow 是否通过 通过
代码语言 C++ 运行时间 0.265 s
提交时间 2017-07-26 19:27:58 内存使用 12.13 MiB
显示代码纯文本
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
#define pos(i,a,b) for(int i=(a);i<=(b);i++)
#define pos2(i,a,b) for(int i=(a);i>=(b);i--)
#define N 501000
struct haha{
	int next,to;
}edge[N];
int head[N],cnt=1;
inline void add(int u,int v){
	edge[cnt].to=v;
	edge[cnt].next=head[u];
	head[u]=cnt++;
}
int n,m; 
int deep[N],fa[N];
int temp[N],cun[N],son[N];
inline void dfs(int x,int dep){
	deep[x]=dep;
	//cout<<"dep="<<dep<<endl;
	for(int i=head[x];i;i=edge[i].next){
		int to=edge[i].to;
		if(to!=fa[x]){
			fa[to]=x;
			son[x]++;
			dfs(to,dep+1);
		}
	}
}
inline int lca(int x,int y){
	if(deep[x]<deep[y]){
		swap(x,y);
	}
	while(deep[x]!=deep[y]){
		x=fa[x];
	}
	while(x!=y){
		x=fa[x];
		y=fa[y];
	}
	return x;
}
inline void dfs2(int x){
	if(son[x]==0){
		cun[x]+=temp[x];
		return;
	}
	cun[x]+=temp[x];
	for(int i=head[x];i;i=edge[i].next){
		int to=edge[i].to;
		if(to!=fa[x]){
			dfs2(to);
			cun[x]+=cun[to];
		}
		
	}
}
int ans;
inline int read()
{
    int su=0;
    char ch=getchar();
    while(ch<'0'||ch>'9')
       ch=getchar();
    while(ch<='9'&&ch>='0')
    {
        su=su*10+ch-'0';
        ch=getchar();
    }
          
    return su;
}
int haha(){
	freopen("yam.in","r",stdin);
	freopen("yam.out","w",stdout);
	scanf("%d%d",&n,&m);
	pos(i,1,n-1){
		int x,y;
		x=read();y=read();
		add(x,y);
		add(y,x);
	}
	dfs(1,1);
	pos(i,1,m){
		int x,y;
		x=read();y=read();
		temp[x]++;temp[y]++;temp[lca(x,y)]-=2;
	}
	dfs2(1);
	pos(i,2,n){
		//cout<<"i="<<i<<"  cun[i]="<<cun[i]<<endl;
		if(cun[i]==0){
			ans+=m;
		}
		if(cun[i]==1)
		  ans++;
	}
	printf("%d",ans);
	return 0;
}
int sb=haha();
int main(){
	;
}