模拟赛日寄

T3

#include<bits/stdc++.h>
#define cint const int& 
#define lowbit(x) (x & -x)
using namespace std;
const int maxn = 5e5+10;
int n, a, b;
struct {
    int nxt, v;
}edge[maxn << 1];
int cnt, head[maxn],dfn[maxn],out[maxn], siz[maxn], dfns;
int t[maxn];
inline void add(cint x, cint v) {
    for (int i = x; i <= n+5; i += lowbit(i)) t[i] += v;
}
inline int get(cint x) {
    int ans = 0;
    for (int i = x; i; i ^= lowbit(i)) ans += t[i];
    return ans;
}
inline void addedge(cint u, cint v) {
    edge[++cnt] = { .nxt = head[u],.v = v };
    head[u] = cnt;
}
void dfs1(cint u, cint fa) {
    siz[u] = 1;
    dfn[u] = ++dfns;
    for (int i = head[u]; i; i = edge[i].nxt) {
        int v = edge[i].v;
        if (v == fa) continue;
        dfs1(v,u);
        siz[u] += siz[v];
    }
    out[u] = dfn[u] + siz[u] + 1;
}
int ans;
int dfs2(cint u, cint fa) {
    ans += get(dfn[u - n]);// -get(dfn[u - n]);
    int res = 1;
    add(dfn[u - n], 1);
    add(out[u - n], -1);
    for (int i = head[u]; i; i = edge[i].nxt) {
        int v = edge[i].v;
        if (v == fa) continue;
        res += dfs2(v, u);
    }
    add(dfn[u - n], -1);
    add(out[u - n], 1);
    return res;
}
signed main() { 
    freopen("climb.in", "r", stdin);
    freopen("climb.out", "w", stdout);
    dfns = 1;
    cin >> n;
    for (int i = 1; i < n; ++i) {
        cin >> a >> b;
        addedge(a, b);
        addedge(b, a);
    }
    for (int i = 1; i < n; ++i) {
        cin >> a >> b;
        addedge(a + n, b + n);
        addedge(b + n, a + n);
    }
    dfs1(1, 0);
    dfs2(1 + n, 0);
    cout << ans;
}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇