D.
题意:给定一棵根为1且n个节点的树,对树节点赋值0或者1.在t秒时,每个节点会变成t-1秒时以该节点所有儿子的权值异或和.造成的贡献为该节点在t秒时的值,求出所有染色方案在t->∞时产生的总贡献.
因为叶子节点会一层一层向上变成0,所以对于x号点,假设以该点为根的子树中最深的叶子节点深度为d,那么该节点一定会在d-d[x]+1秒后变成0.直接求方案数比较困难,我们求出每个节点变成1的概率再乘以(d-d[x]+1)*2^(n)即可.每个节点变成1的概率为0.5.
证明:对于叶子节点来说,显然是对的,因为初始染色的概率为0.5.
对于连接叶子节点的节点来说.假设其有cnt个节点.若该节点初始为1,叶子节点初始有偶数个1的概率为0.5,由二项式定理推得.该点为0的情况相似,所以该节点变成1的概率是0.5.往上递推可得以上结论.
E.
题意:给定n个点m条边的有向图,边权为w,要求对边进行重定向,代价是重定向所有边中最大的边权,使得在满足至少有一个点可以到达图中其他所有点.
二分代价mid,边权小于mid的边改双向边,tarjan缩点后判断是否只有一个点入度为0,是则返回yes,否则no.
#include <bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false), cin.tie(0)
#define ll long long
// #define double long double
#define ull unsigned long long
#define PII pair<int, int>
#define PDI pair<double, int>
#define PDD pair<double, double>
#define debug(a) cout << #a << " = " << a << endl
#define point(n) cout << fixed << setprecision(n)
#define all(x) (x).begin(), (x).end()
#define mem(x, y) memset((x), (y), sizeof(x))
#define lbt(x) (x & (-x))
#define SZ(x) ((x).size())
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
// namespace nqio { const unsigned R = 4e5, W = 4e5; char* a, * b, i[R], o[W], * c = o, * d = o + W, h[40], * p = h, y; bool s; struct q { void r(char& x) { x = a == b && (b = (a = i) + fread(i, 1, R, stdin), a == b) ? -1 : *a++; } void f() { fwrite(o, 1, c - o, stdout); c = o; } ~q() { f(); }void w(char x) { *c = x; if (++c == d) f(); } q& operator >>(char& x) { do r(x); while (x <= 32); return *this; } q& operator >>(char* x) { do r(*x); while (*x <= 32); while (*x > 32) r(*++x); *x = 0; return *this; } template<typename t> q& operator>>(t& x) { for (r(y), s = 0; !isdigit(y); r(y)) s |= y == 45; if (s) for (x = 0; isdigit(y); r(y)) x = x * 10 - (y ^ 48); else for (x = 0; isdigit(y); r(y)) x = x * 10 + (y ^ 48); return *this; } q& operator <<(char x) { w(x); return *this; }q& operator<< (char* x) { while (*x) w(*x++); return *this; }q& operator <<(const char* x) { while (*x) w(*x++); return *this; }template<typename t> q& operator<< (t x) { if (!x) w(48); else if (x < 0) for (w(45); x; x /= 10) *p++ = 48 | -(x % 10); else for (; x; x /= 10) *p++ = 48 | x % 10; while (p != h) w(*--p); return *this; } }qio; }using nqio::qio;
using namespace std;
const int N = 2e5 + 10, MOD = 1e9 + 7;
int n, v[N];
vector<int> g[N];
int qmi(int a, int k) {int res = 1;while (k) {if (k & 1) res = res * a % MOD;k >>= 1;a = a * a % MOD;}return res;
}
void dfs(int x, int fa) {v[x] = 1;for (int y : g[x]) {if (y == fa) {continue;}dfs(y, x);v[x] = max(v[x], v[y] + 1);}
}
void solve() {cin >> n;for (int i = 1; i <= n; ++i) {g[i].clear();}for (int i = 1; i <= n - 1; ++i) {int x, y;cin >> x >> y;g[x].emplace_back(y);g[y].emplace_back(x);}dfs(1, 0);int ans = 0;for (int i = 1; i <= n; ++i) {(ans += v[i]) %= MOD;}cout << ans * qmi(2, n - 1) % MOD << "\n";
}
signed main() {IOS;int T;cin >> T;while (T--) solve();
}
F.
题意:给定长度为n的序列a[1~n],求出最大子数组的值(见上图).
对每个点建最大值笛卡尔树,然后递归处理(l, mid, r),枚举min(mid - l + 1, r - mid + 1)段,然后用可持久化trie查询最大异或和.复杂度O(nlogn*logn)
#include <bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false), cin.tie(0)
#define ll long long
// #define double long double
#define ull unsigned long long
#define PII pair<int, int>
#define PDI pair<double, int>
#define PDD pair<double, double>
#define debug(a) cout << #a << " = " << a << endl
#define point(n) cout << fixed << setprecision(n)
#define all(x) (x).begin(), (x).end()
#define mem(x, y) memset((x), (y), sizeof(x))
#define lbt(x) (x & (-x))
#define SZ(x) ((x).size())
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
// namespace nqio { const unsigned R = 4e5, W = 4e5; char* a, * b, i[R], o[W], * c = o, * d = o + W, h[40], * p = h, y; bool s; struct q { void r(char& x) { x = a == b && (b = (a = i) + fread(i, 1, R, stdin), a == b) ? -1 : *a++; } void f() { fwrite(o, 1, c - o, stdout); c = o; } ~q() { f(); }void w(char x) { *c = x; if (++c == d) f(); } q& operator >>(char& x) { do r(x); while (x <= 32); return *this; } q& operator >>(char* x) { do r(*x); while (*x <= 32); while (*x > 32) r(*++x); *x = 0; return *this; } template<typename t> q& operator>>(t& x) { for (r(y), s = 0; !isdigit(y); r(y)) s |= y == 45; if (s) for (x = 0; isdigit(y); r(y)) x = x * 10 - (y ^ 48); else for (x = 0; isdigit(y); r(y)) x = x * 10 + (y ^ 48); return *this; } q& operator <<(char x) { w(x); return *this; }q& operator<< (char* x) { while (*x) w(*x++); return *this; }q& operator <<(const char* x) { while (*x) w(*x++); return *this; }template<typename t> q& operator<< (t x) { if (!x) w(48); else if (x < 0) for (w(45); x; x /= 10) *p++ = 48 | -(x % 10); else for (; x; x /= 10) *p++ = 48 | x % 10; while (p != h) w(*--p); return *this; } }qio; }using nqio::qio;
using namespace std;
const int N = 2e5 + 10;
struct Node {int ch[2], cnt;
} tr[N * 64];
int root[N], tot;
int stk[N], ls[N], rs[N];
int n, a[N], s[N], ans;
void insert(int x, int k, int pre, int &now) {tr[now = ++tot] = tr[pre];++tr[now].cnt;if (k < 0) return;int t = x >> k & 1;insert(x, k - 1, tr[pre].ch[t], tr[now].ch[t]);
}
int query(int l, int r, int x) {int res = 0;for (int i = 30; i >= 0; --i) {int t = x >> i & 1;if (tr[tr[r].ch[t ^ 1]].cnt - tr[tr[l].ch[t ^ 1]].cnt) {l = tr[l].ch[t ^ 1], r = tr[r].ch[t ^ 1];res += 1ll << i;}else {l = tr[l].ch[t], r = tr[r].ch[t];}}return res;
}
int build(int n) {int top = 0;for (int i = 1; i <= n; ++i) {int now = top;while (now && a[stk[now]] < a[i]) --now;if (now) rs[stk[now]] = i;if (now < top) ls[i] = stk[now + 1];stk[top = ++now] = i;}return stk[1];
}
void dfs(int m, int l, int r) {if (r - m + 1 > m - l + 1) {for (int i = l; i <= m; ++i) {ans = max(ans, query(root[m - 1], root[r], s[i - 1] ^ a[m]));}}else {for (int i = m; i <= r; ++i) {ans = max(ans, query(l == 1 ? 0 : root[l - 2], root[m - 1], s[i] ^ a[m]));}}if (ls[m]) dfs(ls[m], l, m - 1);if (rs[m]) dfs(rs[m], m + 1, r);
}
void solve() {cin >> n;insert(0, 30, 0, root[0]);for (int i = 1; i <= n; ++i) {cin >> a[i];s[i] = s[i - 1] ^ a[i];insert(s[i], 30, root[i - 1], root[i]);}ans = 0;dfs(build(n), 1, n);cout << ans << "\n";for (int i = 0; i <= n; ++i) {root[i] = 0;ls[i] = rs[i] = 0;}for (int i = 0; i <= tot; ++i) {tr[i] = {};}tot = 0;
}
signed main() {IOS;int T;cin >> T;while (T--) solve();
}