版本比较
APIO 2026游记
正文
增加 181 行删除 0 行
1Fe123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182Fe```cpp//luogu uid:532268#include<bits/stdc++.h>using namespace std;namespace zhr{#define AC 0#define Debug 1#define D(...) print(#__VA_ARGS__, __VA_ARGS__)template<typename... Args>void print(const char* names, Args&&... args){if(!Debug) return;auto print_one = [&](const char*& p, auto&& v, bool is_first) {if (!is_first) std::cout << ", ";while (*p == ' ') p++;while (*p && *p != ',') std::cout << *p++;std::cout << ": " << v;if (*p == ',') p++;};bool first = true;((print_one(names, forward<Args>(args), first), first = false), ...);cout << '\n';}#define tab cout << " "#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)#define RE register#define int long long#define ll long long#define ull unsigned long long#define db double#define fst first#define scd second#define mkp make_pair#define pii pair<int,int>#define inl inline#define TTr int T;T=read();while(T--)#define TTc int T;cin>>T;while(T--)#define up(l,r,i) for(int i=(l);i<=(r);++i)#define dn(r,l,i) for(int i=(r);i>=(l);--i)#define upp(l,r,i,k) for(int i=(l);i<=(r);i+=(k))#define dnn(r,l,i,k) for(int i=(r);i>=(l);i-=(k))#define mems(a, x) memset((a), (x), sizeof(a))#define Open(s) freopen(s".in","r",stdin);freopen(s".out","w",stdout);#define ctz __builtin_ctz#define popcount __builtin_popcount#define ctzll __builtin_ctzll#define popcountll __builtin_popcountll#define ctn continue#define brk breakconst int inf = 0x3fffffff, pyl = 1e4 + 7;const ll infll = 0x3f3f3f3f3f3f3f3f;const double eps = 1e-8;template <class T> constexpr T abs(T a){return a < 0 ? -a : a;}inline int min(int a, int b){return a < b ? a : b;}inline int max(int a, int b){return a > b ? a : b;}template <class T> constexpr T gcd(T a, T b) {int az = ctz(a), bz = ctz(b), z = min(az, bz);for (b >>= bz; a; ) {a >>= az; int d = a - b;az = ctz(a - b), b = min(a, b), a = (d < 0) ? -d : d;} return b << z;}template <class T> constexpr T lcm(T a, T b){return a / gcd(a, b) * b;}inline int ksm(int a, int m){int ans = 1;while(m) {if (m & 1) ans *= a;a *= a;m >>= 1;}return ans;}inline int ksm(int a, int m, int p){int ans = 1;while(m) { if(m & 1) ans = (ans * a) % p;a = (a * a) % p;m >>= 1;}return ans;}inline bool Isprime(int x){if(x <= 1) return false;for(int i = 2; i * i <= x; i = -~i) if(x % i == 0) return false;return true;}inline int lowbit(int x){return x & -x;}mt19937_64 rng(time(0));inline ll Rand(ll l, ll r){return rng() % (r - l + 1) + l;}inline long long power(long long a, long long b, long long p) {long long res = 1; for (a %= p; b; b >>= 1) {if (b & 1) res = (__int128) res * a % p;a = (__int128) a * a % p;} return res;}const long long BASE[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};inline bool miller_rabin(long long n) {if (n < 2 || n % 6 % 4 != 1) return (n | 1) == 3;long long s = __builtin_ctzll(n - 1), d = n >> s;for (long long a : BASE) {long long p = power(a, d, n), i = s;while (p != 1 && p != n - 1 && a % n && i--) p = (__int128) p * p % n;if (p != n - 1 && i != s) return false;} return true;}inline int TIME(){return chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count();}int __mint_global_mod = 1e9 + 7;#define mod __mint_global_modstruct mint {int val;explicit operator int() const {return val;}static int norm(int x) {return x<0?x+mod:x;}static int get_mod() {return mod;}static int set_mod(int x) {return mod = x;}mint():val(0){}mint(signed m):val(norm(m%mod)){}mint(long long m):val(norm(m%mod)){}mint operator- () const {return -val;}bool operator== (const mint& x) const {return val==x.val;}bool operator!= (const mint& x) const {return val!=x.val;}bool operator< (const mint& x) const {return val<x.val;}bool operator<= (const mint& x) const {return val<=x.val;}bool operator> (const mint& x) const {return val>x.val;}bool operator>= (const mint& x) const {return val>=x.val;}mint& operator+= (const mint& x) {return val=(val+x.val>=mod?val+x.val-mod:val+x.val),*this;}mint& operator-= (const mint& x) {return val=(val-x.val<0?val-x.val+mod:val-x.val),*this;}mint& operator*= (const mint& x) {return val=(static_cast<long long>(val)*x.val%mod),*this;}mint pow(long long b) const {mint a(*this),res(1);for(;b;a*=a,b>>=1)if(b&1)res*=a;return res;}// static void exgcd(long long a,long long b,long long& x,long long& y)// {return b==0?(x=1,y=0):(exgcd(b,a%b,y,x),y-=a/b*x),void();}// mint operator~ () const {long long a,b;return exgcd(val,mod,a,b),a;}mint operator~ () const {return pow(mod-2);}mint& operator/= (const mint& x) {return *this*=~x;}mint operator+ (const mint& x) const {return mint(*this)+=x;}mint operator- (const mint& x) const {return mint(*this)-=x;}mint operator* (const mint& x) const {return mint(*this)*=x;}mint operator/ (const mint& x) const {return mint(*this)/=x;}friend std::istream& operator>> (std::istream& in, mint& x) {long long v;return in>>v,x.val=norm(v%mod),in;}friend std::ostream& operator<< (std::ostream& out, const mint& x) {return out<<x.val;}};#undef mod#define Z mint}using namespace zhr;namespace IO{const int MAXSIZE = 1 << 19;#define isdigit(x) (x >= '0' && x <= '9')char buf[MAXSIZE], *P1 = buf, *P2 = buf;char pbuf[MAXSIZE], *PP = pbuf;#define faster 0inline char gc(){#if fasterif (P1 == P2) P2 = (P1 = buf) + fread(buf, 1, MAXSIZE, stdin);return P1 == P2 ? ' ' : *P1++;#endifreturn getchar();}inline void pc(const char &c){#if fasterif (PP - pbuf == MAXSIZE) fwrite(pbuf, 1, MAXSIZE, stdout), PP = pbuf;*PP++ = c;#elseputchar(c);#endif}inline void Flush(){#if fasterfwrite(pbuf, 1, PP - pbuf, stdout);PP = pbuf;#endif}#define enter pc('\n')#define space pc(' ')inline void read(double& rdx) {double t = 0;int x = 0, s = 0, f = 1;char c;do c = gc();while (!isdigit(c) && c != '-' && c != '.');if (c == '-')f = -1, c = gc();while (isdigit(c) && c != '.') x = (c ^ 48) + (x << 1) + (x << 3), c = gc();if (c == '.') c = gc();else {rdx = x * f;return;}while (c >= '0' && c <= '9') t = t * 10 + (c ^ 48), s = -~s, c = gc();while (s--) t /= 10;rdx = (x + t) * f;}inline void read(char& ch) {ch = gc();while (ch == ' ' || ch == '\n') ch = gc();}inline int read() {int x = 0;bool f = 1;char ch = gc();while (!isdigit(ch)) f = ch == '-' ? 0 : 1, ch = gc();while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = gc();return f ? x : -x;}inline void outin2(int x){if(x < 0){pc('-'), x = -x;}static int sta[130];int top = 0;do {sta[top++] = x & 1, x >>= 1;} while (x);while (top) pc(sta[--top] + 48);}inline void outin2(int x, int y){if(x < 0){pc('-'), x = -x;}static int sta[130];int top = 0;do {sta[top++] = x & 1, x >>= 1;} while (x);while(top < y) sta[top++] = 0;while (top) pc(sta[--top] + 48);}inline void write(int x) {if(x < 0){pc('-'), x = -x;}static int sta[50];int top = 0;do {sta[top++] = x % 10, x /= 10;} while (x);while (top) pc(sta[--top] + 48);}inline void writesp(int x) {if(x < 0){pc('-'), x = -x;}static int sta[50];int top = 0;do {sta[top++] = x % 10, x /= 10;} while (x);while (top) pc(sta[--top] + 48);pc(' ');}inline void writeln(int x) {if(x < 0){pc('-'), x = -x;}static int sta[50];int top = 0;do {sta[top++] = x % 10, x /= 10;} while (x);while (top) pc(sta[--top] + 48);pc('\n');}}using namespace IO;const int N = 3e5 + 7, mod = 1e9 + 7;int n, Q;int fa[21][N], dep[N];Z f[N], g[N];vector<int> G[N];inline void cf(int u){dep[u] = dep[fa[0][u]] + 1;up(1, 20, i) fa[i][u] = fa[i - 1][fa[i - 1][u]];if(G[u].empty()){f[u] = 1;return;}f[u] = (int)G[u].size();for(int v : G[u]){cf(v);f[u] += f[v];}f[u] = f[u] / (int)G[u].size();if(u > 1) f[u] = f[u] * dep[u] / (dep[u] - 1);}inline void cg(int u, Z sum){if(u > 1){g[u] = (int)G[fa[0][u]].size();for(int bro : G[fa[0][u]]){if(bro == u) ctn;g[u] += f[bro] + sum / dep[fa[0][u]];}g[u] /= (int)G[fa[0][u]].size();}for(int v : G[u]){cg(v, sum + g[u] * (dep[u] - 1));}}inline int LCA(int u, int v){if(dep[u] < dep[v]) swap(u, v);dn(20, 0, i) if(dep[fa[i][u]] >= v) u = fa[i][u];if(u == v) return u;dn(20, 0, i) if(fa[i][u] != fa[i][v]) u = fa[i][u], v = fa[i][v];return fa[0][u];}signed main() {n = read(), Q = read();up(1, n, i) fa[0][i] = read(), G[fa[0][i]].emplace_back(i);cf(1);cg(1, 0);while(Q--){int u = read(), v = read();int lca = LCA(u, v);}Flush();return AC;}```