QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#270122 | #6127. Kawa Exam | zlxFTH | WA | 393ms | 34612kb | C++17 | 2.7kb | 2023-11-30 15:37:31 | 2023-11-30 15:37:32 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define debug(...) fprintf(stderr, __VA_ARGS__)
const int N = 1e5 + 5;
int n, m, sz, ti, mx;
int a[N], cnt[N], num[N], siz[N], sum[N], rev[N], son[N];
int dfn[N], low[N], sd[N], ans[N];
vector<int> st, C[N];
vector<pair<int, int>> G[N], E[N];
void tarjan(int u, int from = 0) {
st.push_back(u);
dfn[u] = low[u] = ++ti;
for (auto [v, id] : G[u]) if (id != from) {
if (!dfn[v]) {
tarjan(v, id);
low[u] = min(low[u], low[v]);
} else low[u] = min(low[u], dfn[v]);
}
if (low[u] == dfn[u]) {
++sz;
while (1) {
int v = st.back();
st.pop_back();
sd[v] = sz;
C[sz].push_back(a[v]);
if (v == u) break;
}
}
}
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, int fa) {
dfn[u] = ++ti, rev[ti] = u, siz[u] = 1;
sum[u] = C[u].size();
for (auto [v, id] : E[u]) if (v != fa) {
dfs1(v, u);
siz[u] += siz[v];
sum[u] += sum[v];
if (sum[v] > sum[son[u]]) son[u] = v;
}
}
void dfs2(int u, int pa, int kp, int d, int dd = -1) {
for (auto [v, id] : E[u]) if (v != son[u] && id != pa) dfs2(v, id, 0, d, dd);
for (auto [v, id] : E[u]) if (v == son[u] && id != pa) dfs2(v, id, 1, d, dd);
for (auto x : C[u]) add(x, d);
for (auto [v, id] : E[u]) if (v != son[u] && id != pa) {
for (int i = dfn[v]; i < dfn[v] + siz[v]; ++i) {
int cur = rev[i];
for (auto x : C[cur]) add(x, d);
}
}
if (pa) ans[pa] += mx - (~dd ? dd : 0);
if (!kp) {
for (int i = dfn[u]; i < dfn[u] + siz[u]; ++i) {
int cur = rev[i];
for (auto x : C[cur]) add(x, -d);
}
}
}
void solve() {
cin >> n >> m;
for (int i = 1; i <= n; ++i)
cin >> a[i], dfn[i] = low[i] = sd[i] = 0;
for (int i = 1, u, v; i <= m; ++i) {
ans[i] = 0;
cin >> u >> v;
G[u].push_back({v, i});
G[v].push_back({u, i});
}
for (int i = 1; i <= n; ++i) if (!dfn[i]) tarjan(i);
for (int u = 1; u <= n; ++u)
for (auto [v, id] : G[u]) if (sd[u] != sd[v]) {
E[sd[u]].push_back({sd[v], id});
}
ti = 0;
for (int i = 1; i <= sz; ++i) dfn[i] = 0;
int res = 0;
for (int i = 1; i <= sz; ++i) if (!dfn[i]) {
dfs1(i, 0);
dfs2(i, 0, 1, 1);
res += mx;
dfs2(i, 0, 1, -1, mx);
}
for (int i = 1; i <= m; ++i)
cout << ans[i] + res << " \n"[i == m];
for (int i = 1; i <= n; ++i) {
dfn[i] = low[i] = sd[i] = 0;
G[i].clear(), E[i].clear(), C[i].clear();
son[i] = 0;
}
for (int i = 1; i <= m; ++i) ans[i] = 0;
ti = sz = 0;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int t;
cin >> t;
while (t--) solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 10516kb
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: 393ms
memory: 34612kb
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 5 5 5 5 5 5 2 2 2 3 3 2 2 7 7 7 7 9 9 9 8 9 8 9 8 9 9 10 9 9 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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 9 10 9 14 15 15 14 15 15 15 14 7 6 7 6 7 6 6 6 6 6 6 6 6 6 7 6 6 6 6 7 9 8 8 8 9 9 8 8 8 8 9 9 9 8 9 9 8 9 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 5 5 5 5 5 5'