QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#422214 | #7513. Palindromic Beads | pandapythoner | WA | 1ms | 3852kb | C++17 | 3.0kb | 2024-05-27 04:40:24 | 2024-05-27 04:40:24 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define flt double
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
const ll inf = 1e18;
mt19937 rnd(234);
int n;
vector<int> c;
vector<vector<int>> g;
vector<vector<int>> f;
vector<int> tin, tout;
vector<int> e;
void dfs(int v, int p) {
tin[v] = (int)e.size();
e.push_back(v);
for (auto to : g[v]) {
if (to == p) {
continue;
}
dfs(to, v);
}
tout[v] = (int)e.size();
}
bool is_son(int v, int p) {
return tin[p] <= tin[v] and tin[v] < tout[p];
}
int solve() {
tin.resize(n);
tout.resize(n);
e.clear();
e.reserve(n);
dfs(0, -1);
f.assign(n, vector<int>());
for (auto v : e) {
f[c[v]].push_back(v);
}
int rs = 1;
sort(all(f), [&](const vector<int>& a, const vector<int>& b) {
if ((int)a.size() != (int)b.size()) {
return (int)a.size() < (int)b.size();
}
if ((int)a.size() != 2) {
return false;
}
int u = a[0], v = a[1];
int s = b[0], t = b[1];
bool para = is_son(u, v) || is_son(v, u);
bool parb = is_son(s, t) || is_son(t, s);
if (para != parb) {
return para < parb;
}
if (para) {
return abs(tin[u] - tin[v]) > abs(tin[s] - tin[t]);
}
return tin[u] < tin[s];
});
vector<array<int, 3>> aboba;
auto add_point = [&](int x, int y, int val) {
aboba.push_back({ x, y, val });
};
auto get = [&](int lx, int rx, int ly, int ry) {
int rs = 0;
for (auto [x, y, val] : aboba) {
if (lx <= x and x <= rx and ly <= y and y <= ry) {
rs = max(rs, val);
}
}
return rs;
};
for (auto x : f) {
if ((int)x.size() != 2) {
continue;
}
int u = x[0], v = x[1];
int mx = 2;
if (is_son(v, u)) {
mx = max(mx, get(0, tin[u] - 1, tin[v], tout[v] - 1) + 2);
mx = max(mx, get(tin[v], tout[v] - 1, tout[u], n - 1) + 2);
} else {
mx = max(mx, get(tin[u], tout[u] - 1, tin[v], tout[v] - 1));
}
if (is_son(v, u)) {
rs = max(rs, mx);
} else {
rs = max(rs, mx + 1);
}
add_point(tin[u], tin[v], mx);
}
return rs;
}
int32_t main() {
if (1) {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
cin >> n;
c.resize(n);
for (int i = 0; i < n; i += 1) {
cin >> c[i];
--c[i];
}
g.assign(n, vector<int>());
for (int i = 0; i < n - 1; i += 1) {
int u, v;
cin >> u >> v;
--u;
--v;
g[u].push_back(v);
g[v].push_back(u);
}
int rs = solve();
cout << rs << "\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3788kb
input:
4 1 1 2 2 1 2 2 3 2 4
output:
3
result:
ok single line: '3'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3504kb
input:
5 1 3 2 2 1 1 2 2 3 3 4 4 5
output:
4
result:
ok single line: '4'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3636kb
input:
6 1 1 2 2 3 3 1 2 2 3 3 4 4 5 5 6
output:
2
result:
ok single line: '2'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3560kb
input:
6 1 2 3 4 5 6 1 2 2 3 3 4 4 5 5 6
output:
1
result:
ok single line: '1'
Test #5:
score: -100
Wrong Answer
time: 1ms
memory: 3852kb
input:
2000 845 1171 345 282 1181 625 754 289 681 493 423 840 1494 318 266 1267 967 379 135 14 39 191 60 972 116 1216 1205 19 194 185 1360 861 379 430 1262 1151 756 65 389 488 277 53 1283 1438 101 1465 195 714 737 980 80 298 961 1326 163 1163 1317 1152 992 35 334 802 1502 486 710 234 555 88 1278 146 46 696...
output:
4
result:
wrong answer 1st lines differ - expected: '5', found: '4'