版本比较
bababoy
正文
增加 25 行删除 25 行
41 行未改动42434445464647484949505152535455565556575858596061626364656667666768697071727373747576774 行未改动82838485868687888989909192937 行未改动1011021031041051051061071071081091101111121131131141151161171181181191201211221231231241251261272 行未改动13013113213313413413513613613713813813914014114214314414514414514614714814915 行未改动1651661671681691691701711721731731741751761771781791801811821811821831841851861871881891901911921921931941951963 行未改动41 行未改动#include "bits/stdc++.h"#define f(i ,m ,n ,x) for (int i = (m) ,i##END = (n) ; i <= i##END ; i += (x))
const int N = 1e6 + 25 ,mod = 1e9 + 7 ;int n ,head[N] ,tot ,dep[N] ,dfn[N] ,siz[N] ,lft[N] ,rt[N] ,res[N] ,sum[N] ,p[N] ; int n ,head[N] ,tot ,dep[N] ,dfn[N] ,siz[N] ,lft[N] ,rt[N] ,res[N] ,sum[N] ,p[N] ;bool isleaf[N] ,vis[N] ,vis2[N] ;struct Edge { int to ,nxt ;} e[N << 1] ;std :: basic_string < int > buc[N] ,buc2[N] ; std :: basic_string < int > buc[N] ,buc2[N] ;
class SegmentTree {private : %:define ls ((cur) << 1) %:define rs ((cur) << 1 | 1) %:define mid ((l) + (r) >> 1) %:define mid ((l) + (r) >> 1)
int ans[N << 2] ,cov[N << 2] ;
inline void Pushdown (int cur) { if (!cov[cur]) return ; cov[ls] = cov[rs] = ans[ls] = ans[rs] = cov[cur] ; return cov[cur] = 0 ,void () ; }public : inline void Modify (int cur ,int l ,int r ,int nowl ,int nowr ,int x) { if (nowl > nowr) return ; if (nowl <= l && nowr >= r) if (nowl > nowr) return ; if (nowl <= l && nowr >= r) return ans[cur] = cov[cur] = x ,void () ; Pushdown (cur) ; if (nowl <= mid) Modify (ls ,l ,mid ,nowl ,nowr ,x) ; if (nowr > mid) Modify (rs ,mid + 1 ,r ,nowl ,nowr ,x) ; }
inline int Query (int cur ,int l ,int r ,int pos) { if (l == r) return ans[cur] ; Pushdown (cur) ; if (pos <= mid) return Query (ls ,l ,mid ,pos) ;4 行未改动class Bit {private : %:define lowbit(x) ((x) & (-(x))) int ans[N] ; inline void Update (int x ,int v) inline void Update (int x ,int v) { f (i ,x ,n ,lowbit (i)) ans[i] += v ;}public : inline void Modify (int l ,int v) inline void Modify (int l ,int v) { return Update (l ,v) ;}
inline int Que (int x) { int res = 0 ;7 行未改动
signed main (void) { std :: ios :: sync_with_stdio (false) , std :: cin.tie (nullptr) ,std :: cout.tie (nullptr) ;
auto Qpow = [] (int bs ,int p = mod - 2) -> int { int ans = 1 ; int ans = 1 ; for (; p ; p >>= 1 ,bs = 1ll* bs * bs % mod) (p & 1) && (ans = 1ll* ans * bs % mod) ; return ans ; } ; //std :: cerr << Qpow (6) * 5 % mod << '\n' ;
auto Add = [] (int u ,int v) -> void { e[++tot] = (Edge){v ,head[u]} ; return head[u] = tot ,void () ; } ;
std :: cin >> n ; f (i ,1 ,n - 1 ,1) { int u ,v ; std :: cin >> u >> v ; Add (u ,v) ,Add (v ,u) ; }
std :: fill (isleaf + 1 ,isleaf + n + 1 ,true) ; auto Dfs = [] (auto &&self ,int cur ,int fa) -> void { static int cnt = 0 ; dfn[cur] = ++cnt ; dep[cur] = dep[fa] + 1 ; buc2[dep[cur]] += cur ;2 行未改动 if (nex == fa) continue ; isleaf[cur] = false ; self (self ,nex ,cur) ; siz[cur] += siz[nex] ; if (isleaf[nex] && !vis[cur]) if (isleaf[nex] && !vis[cur]) vis[cur] = true ,buc[dep[cur]] += cur ; } } } ; Dfs (Dfs ,1 ,0) ;
__builtin_memset (rt ,0x3f ,sizeof (int) * (n + 1)) ; f (i ,1 ,*std :: max_element (dep + 1 ,dep + n + 1) ,1) for (int pos = 1 ; pos < buc[i].size () ; pos++) rt[buc[i][pos - 1]] = buc[i][pos] ,lft[buc[i][pos]] = buc[i][pos - 1] ; //std :: cerr << lft[8] << ' ' << rt[8] << '\n' ; auto Judge = [] (int x ,int l ,int r) -> bool
auto Judge = [] (int x ,int l ,int r) -> bool { return x >= l && x <= r ;} ;
[&] (void) -> void { std :: queue < int > q ;15 行未改动 //for (auto j : buc2[las]) if (!isleaf[j]) (sum[las] += p[j]) %= mod ; } int anc = sgt.Query (1 ,1 ,n ,dfn[cur]) ; //std :: cerr << "lzk " << cur << ' ' << anc << ' ' ; if (!anc && (rt[cur] <= n || lft[cur])) if (!anc && (rt[cur] <= n || lft[cur])) vis2[cur] = true ;//,(sum[dep[cur]] += p[cur]) %= mod , //std :: cerr << "lzk " << cur << '\n' ; if (anc && ((rt[cur] <= n && Judge (dfn[rt[cur]] ,dfn[anc] ,dfn[anc] + siz[anc] - 1)) || (lft[cur] && Judge (dfn[lft[cur]] ,dfn[anc] ,dfn[anc] + siz[anc] - 1)))) || (lft[cur] && Judge (dfn[lft[cur]] ,dfn[anc] ,dfn[anc] + siz[anc] - 1)))) vis2[cur] = true ;//,(sum[dep[cur]] += p[cur]) %= mod ,std :: cerr << "lzk " << cur << '\n' ; if (vis2[cur]) bit.Modify (dfn[cur] ,1) ; //std :: cerr << dep[cur] << ' ' << vis2[dep[cur]] << '\n' ; int cnt = 0 ; for (int i = head[cur] ; i ; i = e[i].nxt) { int nex = e[i].to ; if (dep[nex] < dep[cur]) continue ; cnt++ ; } }
int inv = Qpow (cnt) ; for (int i = head[cur] ; i ; i = e[i].nxt) { int nex = e[i].to ; if (dep[nex] < dep[cur] || isleaf[nex]) continue ; //std :: cerr << cur << ' ' << nex << ' ' << p[cur] << ' ' << cnt << '\n' ; p[nex] = 1ll * p[cur] * inv % mod ; q.emplace (nex) ; } } } () ;
//f (i ,1 ,n ,1) std :: cout << vis2[i] << '\n' ; int ans = 0 ; f (i ,1 ,n ,1) (ans += vis2[i] * p[i]) %= mod ; std :: cout << ans << '\n' ;3 行未改动