QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#270056 | #6127. Kawa Exam | zlxFTH | WA | 257ms | 28576kb | C++17 | 3.1kb | 2023-11-30 14:36:50 | 2023-11-30 14:36:50 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define debug(...) fprintf(stderr, __VA_ARGS__)
const int N = 1e5 + 5;
int tot, head[N];
struct Edge {
int nxt, v;
} e[2 * N];
inline void addEdge(int u, int v) {
e[++tot] = {head[u], v};
head[u] = tot;
}
int n, m, sum, mx, sz;
int a[N], ans[N], cnt[N], num[N], sd[N];
vector<int> b, st, col[N];
vector<pair<int, int>> G[N];
vector<array<int, 3>> E;
int ti, dfn[N], low[N];
void tarjan(int u, int from = 0) {
b.push_back(a[u]);
st.push_back(u);
dfn[u] = low[u] = ++ti;
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].v;
if ((i ^ 1) == from) continue;
if (!dfn[v]) {
tarjan(v, i);
low[u] = min(low[u], low[v]);
if (dfn[u] < low[v]) {
E.push_back({u, v, i >> 1});
++sz;
while (st.back() != u) {
int cur = st.back();
sd[cur] = sz;
st.pop_back();
col[sz].push_back(a[cur]);
}
}
} else low[u] = min(low[u], dfn[v]);
}
}
int siz[N], s[N], son[N], dfm[N], rev[N];
void add(int u, int v) {
if (mx == cnt[u] && (num[mx] == 1 || cnt[u] == 0)) mx += v;
num[cnt[u]]--;
num[cnt[u] += v]++;
}
void dfs1(int u) {
dfm[u] = ++ti, rev[ti] = u;
s[u] = col[u].size();
siz[u] = 1;
for (auto [v, id] : G[u]) {
dfs1(v);
siz[u] += siz[v];
s[u] += s[v];
if (s[v] > s[son[u]]) son[u] = v;
}
}
void dfs2(int u, int id, int kp, int d) {
for (auto [v, id] : G[u]) if (v != son[u]) {
dfs2(v, id, 0, d);
}
for (auto [v, id] : G[u]) if (v == son[u]) {
dfs2(v, id, 1, d);
}
for (auto x : col[u]) add(x, d);
for (auto [v, id] : G[u]) if (v != son[u]) {
for (int i = dfm[v]; i < dfm[v] + siz[v]; ++i) {
int cur = rev[i];
for (auto x : col[cur]) add(x, d);
}
}
if (id) ans[id] += mx;
if (!kp) {
for (int i = dfm[u]; i < dfm[u] + siz[u]; ++i) {
int cur = rev[i];
for (auto x : col[cur]) add(x, -d);
}
}
}
void work(int rt) {
tarjan(rt);
++sz;
while (st.size()) {
int cur = st.back();
sd[cur] = sz;
st.pop_back();
col[sz].push_back(a[cur]);
}
for (auto [u, v, id] : E) G[sd[u]].push_back({sd[v], id});
ti = 0;
dfs1(sz);
dfs2(sz, 0, 0, 1);
for (auto v : b) add(v, 1);
for (auto [u, v, id] : E) ans[id] -= mx;
sum += mx;
dfs2(sz, 0, 0, -1);
for (int i = 1; i <= sz; ++i)
G[i].clear(), col[i].clear(), son[i] = 0;
sz = ti = 0;
E.clear();
for (int i = 0; i <= mx; ++i) num[i] = 0;
for (auto v : b) cnt[v] = 0;
mx = 0, b.clear();
}
void solve() {
cin >> n >> m;
for (int i = 1; i <= n; ++i)
cin >> a[i], dfn[i] = low[i] = head[i] = sd[i] = 0;
tot = 1, sum = 0;
for (int i = 1, u, v; i <= m; ++i) {
ans[i] = 0;
cin >> u >> v;
addEdge(u, v);
addEdge(v, u);
}
for (int i = 1; i <= n; ++i) if (!dfn[i]) work(i);
// for (int i = 1; i <= n; ++i) debug("%d%c", sd[i], " \n"[i == n]);
for (int i = 1; i <= m; ++i)
cout << ans[i] + sum << " \n"[i == m];
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int t;
cin >> t;
while (t--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 13680kb
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:
ok 3 lines
Test #2:
score: -100
Wrong Answer
time: 257ms
memory: 28576kb
input:
5557 2 7 79960 79960 2 2 1 1 1 1 2 2 1 1 2 1 1 2 9 8 21881 70740 70740 21881 22458 22458 639 21881 70740 3 3 1 6 5 8 7 5 5 7 2 3 5 1 7 6 6 7 13064 20716 6746 13064 6746 69225 5 5 4 1 4 1 1 6 4 5 3 2 3 2 8 4 45146 14400 45146 45146 14400 72969 14400 45146 8 6 1 3 4 6 8 3 18 13 48132 37949 92338 92338...
output:
2 2 2 2 2 2 2 5 5 6 5 5 5 5 5 2 2 2 3 3 2 2 7 5 7 6 8 8 8 7 8 7 8 7 8 8 9 8 8 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 7 8 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 9 10 9 14 15 15 14 15 15 15 15 9 7 8 7 9 8 7 7 7 7 7 7 7 9 8 7 7 7 7 8 6 5 5 5 6 6 5 5 5 5 6 6 6 5 6 5 5 6 1 1 1 1 1 1 1 1 1...
result:
wrong answer 2nd lines differ - expected: '6 6 7 6 6 6 6 6', found: '5 5 6 5 5 5 5 5'