QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#655284 | #2550. Lion and Zebra | valeriu | TL | 1ms | 3812kb | C++20 | 4.2kb | 2024-10-19 02:25:36 | 2024-10-19 02:25:36 |
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 << '\n';
else {
//cerr << "pula2\n";
if(d - tchain * 2 > 2)
cout << d + Ldist[node] - 2 * tchain << '\n';
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: 100
Accepted
time: 0ms
memory: 3668kb
input:
5 2 1 2 2 3 3 4 4 5 1 4 3 1
output:
4 1
result:
ok 2 number(s): "4 1"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3488kb
input:
11 2 1 2 2 3 1 4 4 5 1 6 6 7 7 8 1 9 9 10 10 11 3 2 10 4
output:
2 5
result:
ok 2 number(s): "2 5"
Test #3:
score: 0
Accepted
time: 1ms
memory: 3812kb
input:
7 2 1 2 2 3 3 4 4 5 4 6 6 7 5 4 5 3
output:
5 3
result:
ok 2 number(s): "5 3"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3548kb
input:
11 9 1 2 2 3 1 4 4 5 1 6 6 7 7 8 1 9 9 10 10 11 7 2 3 2 3 5 3 4 4 4 1 1 8 1 4 3 5 3
output:
3 2 5 4 5 1 1 4 3
result:
ok 9 numbers
Test #5:
score: -100
Time Limit Exceeded
input:
175 7862 70 167 102 128 3 76 46 42 104 112 53 46 52 99 172 116 48 158 40 138 11 103 26 8 59 147 163 88 71 133 130 134 98 73 115 104 28 166 5 173 148 61 38 45 173 73 96 10 36 58 124 59 94 73 137 136 79 164 21 11 27 161 9 152 37 101 123 131 145 68 111 156 153 51 61 73 4 93 54 157 33 69 47 12 144 115 1...