Free Contest Testing Round 2.2 - TREE
Xem dạng PDF
Gửi bài giải
C, C++, Go, Java, Kotlin, Pascal, PyPy, Python, Rust, Scratch
Điểm:
1,21 (OI)
Giới hạn thời gian:
1.0s
Giới hạn bộ nhớ:
256M
Dạng bài
Ngôn ngữ cho phép
Trong trường hợp đề bài hiển thị không chính xác, bạn có thể tải đề bài tại đây: Đề bài
Lưu ý: các bạn không nhập, xuất dữ liệu bằng file kể cả khi đề bài có yêu cầu. Đọc, ghi dữ liệu được thực hiện ở stdin và stdout.
Bình luận
Link bài gốc : link
include <bits/stdc++.h>
using namespace std;
const long long MOD = 1'000'000'007LL; const int LOG = 19; // 2^18 = 262144 > 2·10⁵ const long long INV2 = (MOD + 1) / 2; // modular inverse of 2
/----------------------------------------------------------------- Global data for the original tree -----------------------------------------------------------------/ int N, Q; vector<int> g[200005]; int tin[200005], tout[200005], timerDFS = 0; int depthNode[200005]; int up[200005][LOG]; // binary lifting table for LCA
/----------------------------------------------------------------- DFS – Euler tour, depths and binary lifting -----------------------------------------------------------------/ void dfs(int v, int p) { tin[v] = ++timerDFS; up[v][0] = p; for (int i = 1; i < LOG; ++i) up[v][i] = up[ up[v][i-1] ][i-1];
}
/----------------------------------------------------------------- LCA utilities -----------------------------------------------------------------/ inline bool isancestor(int u, int v) // u is ancestor of v ? { return tin[u] <= tin[v] && tout[v] <= tout[u]; } int lca(int u, int v) { if (isancestor(u, v)) return u; if (isancestor(v, u)) return v; for (int i = LOG - 1; i >= 0; --i) if (!isancestor(up[u][i], v)) u = up[u][i]; return up[u][0]; }
/----------------------------------------------------------------- Virtual tree (built only from the vertices of one query) -----------------------------------------------------------------/ struct VirtualTree { vector<int> vert; // vertices of the virtual tree (sorted by tin) vector child; // children indices inside vert
vector<char> isOrig; // 1 if the vertex belongs to the original query set
};
/----------------------------------------------------------------- Main -----------------------------------------------------------------/ int main() { ios::syncwithstdio(false); cin.tie(nullptr);
}