QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#138754 | #6127. Kawa Exam | SanguineChameleon | WA | 0ms | 10468kb | C++20 | 3.0kb | 2023-08-12 09:59:03 | 2023-08-12 09:59:05 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
void just_do_it();
int main() {
#ifdef KAMIRULEZ
freopen("kamirulez.inp", "r", stdin);
freopen("kamirulez.out", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
just_do_it();
return 0;
}
const int maxn = 1e5 + 20;
int a[maxn];
int tree[maxn];
vector<pair<int, int>> adj[maxn];
vector<int> ch[maxn];
int res[maxn];
int par_id[maxn];
bool flag[maxn];
int high[maxn];
int depth[maxn];
int root[maxn];
int sz[maxn];
int best[maxn][2];
int k;
void dfs_tree(int u) {
flag[u] = true;
high[u] = depth[u];
sz[u] = 1;
for (auto e: adj[u]) {
int v = e.first;
int id = e.second;
if (id == par_id[u]) {
continue;
}
if (!flag[v]) {
ch[u].push_back(v);
root[v] = root[u];
depth[v] = depth[u] + 1;
par_id[v] = id;
dfs_tree(v);
high[u] = min(high[u], high[v]);
sz[u] += sz[v];
}
else {
high[u] = min(high[u], depth[v]);
}
}
}
void update_tree(int id, int lt, int rt, int pos, int delta) {
if (lt == rt) {
tree[id] += delta;
return;
}
int mt = (lt + rt) / 2;
if (pos <= mt) {
update_tree(id * 2, lt, mt, pos, delta);
}
else {
update_tree(id * 2 + 1, mt + 1, rt, pos, delta);
}
tree[id] = max(tree[id * 2], tree[id * 2 + 1]);
}
void add_cnt(int u, int delta) {
update_tree(1, 1, k, a[u], delta);
}
void dfs_add(int u, int delta) {
add_cnt(u, delta);
for (auto v: ch[u]) {
dfs_add(v, delta);
}
}
void dfs_cnt(int u, int type, bool keep) {
int delta = (type ? -1 : 1);
int heavy = 0;
for (auto v: ch[u]) {
if (sz[v] > sz[heavy]) {
heavy = v;
}
}
for (auto v: ch[u]) {
if (v != heavy) {
dfs_cnt(v, type, false);
}
}
if (heavy) {
dfs_cnt(heavy, type, true);
}
for (auto v: ch[u]) {
if (v != heavy) {
dfs_add(v, delta);
}
}
add_cnt(u, delta);
best[u][type] = tree[1];
if (!keep) {
dfs_add(u, -delta);
}
}
void test() {
int n, m;
cin >> n >> m;
map<int, int> comp;
k = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
auto it = comp.find(a[i]);
if (it != comp.end()) {
a[i] = it->second;
}
else {
a[i] = (comp[a[i]] = ++k);
}
}
for (int i = 1; i <= n; i++) {
adj[i].clear();
ch[i].clear();
flag[i] = false;
par_id[i] = 0;
}
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
adj[u].emplace_back(v, i);
adj[v].emplace_back(u, i);
}
int sum = 0;
for (int i = 1; i <= n; i++) {
if (!flag[i]) {
root[i] = i;
depth[i] = 0;
dfs_tree(i);
dfs_cnt(i, 0, true);
dfs_cnt(i, 1, true);
sum += best[i][0];
}
}
for (int i = 1; i <= m; i++) {
res[i] = sum;
}
for (int i = 1; i <= n; i++) {
if (par_id[i] && high[i] == depth[i]) {
res[par_id[i]] += best[i][0] + best[i][1] - best[root[i]][0];
}
}
for (int i = 1; i <= m; i++) {
cout << res[i] << " ";
}
cout << '\n';
}
void just_do_it() {
int t;
cin >> t;
for (int i = 0; i < t; i++) {
test();
}
}
详细
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 10468kb
input:
3 7 5 1 2 1 2 1 2 1 1 2 1 3 2 4 5 6 5 7 3 3 1 2 3 1 2 1 3 2 3 2 3 12345 54321 1 2 1 2 1 1
output:
6 5 5 5 4 1 1 1 1 1 1
result:
wrong answer 1st lines differ - expected: '6 5 5 5 4', found: '6 5 5 5 4 '