QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#655281 | #2550. Lion and Zebra | valeriu | WA | 1ms | 3584kb | C++20 | 4.2kb | 2024-10-19 02:24:23 | 2024-10-19 02:24:24 |
Judging History
answer
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;
using ld = long double;
//#define int ll
#define sz(x) ((int)(x).size())
using pii = pair<int,int>;
using tii = tuple<int,int,int>;
struct Diam {
int x, y, D, xdist;
Diam operator + (const Diam& a) const {
if(a.D > D) return a;
return *this;
}
};
const int nmax = 1e5 + 5;
vector<int> g[nmax];
Diam diam(int node, int f) {
Diam curr = Diam{node, node, 0, 0};
for(auto &x : g[node]) {
if(x == f) continue;
auto R = diam(x, node);
R.xdist++;
Diam U = Diam{R.x, curr.x, curr.xdist + R.xdist, R.xdist};
Diam V = Diam{curr.x, R.x, curr.xdist + R.xdist, curr.xdist};
if(R.xdist < curr.xdist) U = V;
curr = R + curr + U;
}
return curr;
}
int occ[nmax];
void mkdist(int node, int f, int D, vector<int> &height, vector<int>& dist) {
dist[node] = D;
height[node] = 0;
for(auto x : g[node]) {
if(x == f || occ[x]) continue;
mkdist(x, node, D + 1, height, dist);
height[node] = max(height[node], height[x] + 1);
}
return;
}
int jump[nmax], p[nmax];
void initlca(int node, int f, const vector<int>& d) {
jump[node] = p[node] = f;
if(d[jump[jump[f]]] + d[f] == 2 * d[jump[f]]) jump[node] = jump[jump[f]];
for(auto x : g[node]) {
if(x == f) continue;
initlca(x, node, d);
}
return;
}
int twoable[nmax];
signed main() {
cin.tie(0) -> sync_with_stdio(0);
int n, q;
cin >> n >> q;
for(int i = 1, x, y; i < n; i++) {
cin >> x >> y;
g[x].emplace_back(y);
g[y].emplace_back(x);
}
for(int i = 1; i <= n; i++) {
if(sz(g[i]) == 1) {
twoable[g[i][0]] = 1;
}
}
auto [L, R, a, b] = diam(1, 1);
cerr << L << ' ' << R << '\n';
vector<int> Ldist(n + 2), Rdist(n + 2), height(n + 2), tmp(n + 2);
mkdist(L, L, 0, tmp, Ldist);
mkdist(R, R, 0, tmp, Rdist);
initlca(L, L, Ldist);
auto kth = [&](int node, int k) {
int targ = Ldist[node] - k;
while(Ldist[node] > targ) {
if(Ldist[jump[node]] > targ) node = jump[node];
else node = p[node];
}
return node;
};
for(int i = 1; i <= n; i++) {
if(Ldist[i] + Rdist[i] == Ldist[R]) {
occ[i] = 1;
}
}
for(int i = 1; i <= n; i++) {
if(Ldist[i] + Rdist[i] == Ldist[R]) {
for(auto x : g[i]) {
if(Ldist[x] + Rdist[x] != Ldist[x])
mkdist(x, i, 0, height, tmp);
}
}
}
for(int i = 0, node, d; i < q; i++) {
cin >> node >> d;
if(d == 1) {
cout << "1\n";
}
else if(d == 2) {
if(twoable[node])
cout << 3 << '\n';
else
cout << 2 << '\n';
}
else {
if(Ldist[node] > Rdist[node]) swap(Ldist, Rdist), swap(L, R);
int tchain = (Rdist[node] + Ldist[node] - Ldist[R]) / 2;
//cerr << "pula\n";
if(Ldist[node] < d)
cout << d + Ldist[node] - 2 * tchain;
else {
//cerr << "pula2\n";
if(d - tchain * 2 > 2)
cout << d + Ldist[node] - 2 * tchain;
else {
//cerr << "pula3\n";
if(d % 2 == 1) {
auto T = kth(node, (d - 3) / 2);
cout << 3 + (d - 3) / 2 + height[T] << '\n';
}
else {
//cerr << "pula4\n";
auto T = kth(node, (d - 3) / 2), P = kth(node, (d - 2) / 2);
if(height[T] + 1 >= height[P]) {
cout << 4 + (d - 4) / 2 + height[T] << '\n';
}
else if (height[P] + (d - 2) / 2 < d){
cout << 2 + (d - 2) / 2 + height[P] << '\n';
}
else {
cout << 4 + (d - 4) / 2 + height[T] << '\n';
}
}
}
}
}
}
}
/**
Ovaj podrum ima hladan sjaj\Iako je to bolje od gomile drugih
--
*/
详细
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3584kb
input:
5 2 1 2 2 3 3 4 4 5 1 4 3 1
output:
41
result:
wrong answer 1st numbers differ - expected: '4', found: '41'