比赛 |
20241128 |
评测结果 |
AAAAAAAAAAAAAAAAAAAA |
题目名称 |
猴猴的比赛 |
最终得分 |
100 |
用户昵称 |
┭┮﹏┭┮ |
运行时间 |
1.153 s |
代码语言 |
C++ |
内存使用 |
22.55 MiB |
提交时间 |
2024-11-28 09:47:55 |
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,ll>
#define fi first
#define in inline
#define se second
#define mp make_pair
#define pb push_back
const int N = 2e5+10;
ll read(){
ll x = 0,f = 1;char c = getchar();
for(;c < '0' || c > '9';c = getchar())if(c == '-')f = -1;
for(;c >= '0' && c <= '9';c = getchar())x = (x<<1) + (x<<3) + c-'0';
return x * f;
}
int n;
vector<int>e1[N],e2[N];
struct BIT{
int c[N];
int lowbit(int x){return x & (-x);}
void add(int x,int val){
for(;x <= n;x += lowbit(x))c[x] += val;
}
int ask(int x){
int ans = 0;
for(;x > 0;x -= lowbit(x))ans += c[x];
return ans;
}
int ask(int l,int r){
if(l > r)return 0;
return ask(r) - ask(l - 1);
}
}t;
int cnt1,cnt2;
int dfn1[N],dfn2[N],siz1[N],siz2[N],rnk1[N];
void dfs1(int x,int fa){
dfn1[x] = ++cnt1,rnk1[cnt1] = x,siz1[x] = 1;
for(int y : e1[x]){
if(y == fa)continue;
dfs1(y,x);
siz1[x] += siz1[y];
}
}
void dfs2(int x,int fa){
dfn2[x] = ++cnt2,siz2[x] = 1;
for(int y : e2[x]){
if(y == fa)continue;
dfs2(y,x);
siz2[x] += siz2[y];
}
}
vector<pair<pii,int> >G[N];
int main(){
freopen("monkeyclim.in","r",stdin);
freopen("monkeyclim.out","w",stdout);
n = read();
for(int i = 1;i < n;i++){
int x = read(),y = read();
e1[x].pb(y),e1[y].pb(x);
}
for(int i = 1;i < n;i++){
int x = read(),y = read();
e2[x].pb(y),e2[y].pb(x);
}
dfs1(1,0),dfs2(1,0);
for(int i = 1;i <= n;i++){
G[dfn1[i] - 1].pb({{dfn2[i],dfn2[i] + siz2[i] - 1},-1});
G[dfn1[i] + siz1[i] - 1].pb({{dfn2[i],dfn2[i] + siz2[i] - 1},1});
}
ll ans = 0;
for(int i = 1;i <= n;i++){
t.add(dfn2[rnk1[i]],1);
for(auto it : G[i])ans += (ll)t.ask(it.fi.fi,it.fi.se) * it.se;
}
printf("%lld\n",ans - n);
return 0;
}