QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#747894 | #9732. Gathering Mushrooms | laurxh | WA | 24ms | 24956kb | C++14 | 4.0kb | 2024-11-14 18:38:46 | 2024-11-14 18:38:46 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
typedef long long ll;
const ll inf = 1e18;
int T, n, k;
int t[N], a[N], d[N], dep[N], tree[N], id[N], top[N];
ll cnt[N];
pair<ll, int> ans[N];
vector<int> G[N], val[N], stk[N], cir;
void clear() {
for (int i = 1; i <= n; i++) {
G[i].clear();
d[i] = tree[i] = 0;
ans[i] = {0, 0};
}
}
ll get(int u) {
int kth = k, type = t[u];
ll ans = 0;
vector<int> &v = val[type];
if (tree[u]) {
if (kth <= stk[type].size()) {
int pos = stk[type].size() - kth;
return dep[u] - dep[stk[type][pos]] + 1;
}
ans += dep[u];
kth -= stk[type].size();
u = top[u];
}
if (val[type].empty())return inf;
int pos = lower_bound(v.begin(), v.end(), id[u]) - v.begin();
if (v.size() - pos >= kth) {
ans += v[pos + kth - 1] - id[u];
return ans;
}
kth -= v.size() - pos;
ans += cir.size() - id[u];
int round = (kth - 1) / v.size();
kth -= round * v.size();
ans += 1ll * round * cir.size();
ans += v[kth - 1] + 1;
return ans;
}
void dfs(int u, int fa) {
top[u] = top[fa];
dep[u] = dep[fa] + 1;
stk[t[u]].push_back(u);
ans[u] = {ans[fa].first + 1, ans[fa].second};
ll dis = get(u);
if (dis < ans[u].first)ans[u] = {dis, t[u]};
for (auto v: G[u]) {
if (v == fa)continue;
dfs(v, u);
}
stk[t[u]].pop_back();
}
void solve(int x) {
cir.clear();
id[x] = cir.size();
cir.push_back(x);
int u = a[x];
while (u != x) {
id[u] = cir.size();
cir.push_back(u);
u = a[u];
}
vector<int> num;
for (auto v: cir) {
cnt[t[v]]++;
num.push_back(t[v]);
val[t[v]].push_back(id[v]);
}
sort(num.begin(), num.end());
num.erase(unique(num.begin(), num.end()), num.end());
int pos = 0, siz = cir.size();
for (int i = 1; i < siz; i++) {
if (cnt[t[cir[i]]] > cnt[t[cir[pos]]])pos = i;
}
int mx = cnt[t[cir[pos]]];
int round = (k - 1) / mx;
ll res = 1ll * round * siz;
int cur = pos;
for (auto v: num)cnt[v] = cnt[v] * round;
while (1) {
cnt[t[cir[cur]]]++;
res++;
if (cnt[t[cir[cur]]] >= k)break;
cur = (cur + 1) % siz;
}
ans[cir[pos]] = {res, t[cir[cur]]};
cur = (pos - 1 + siz) % siz;
for (int i = 0; i < siz - 1; i++, cur = (cur - 1 + siz) % siz) {
int nxt = (cur + 1) % siz;
ans[cir[cur]] = {ans[cir[nxt]].first + 1, ans[cir[nxt]].second};
ll dis = get(cir[cur]);
if (dis < ans[cir[cur]].first)ans[cir[cur]] = {dis, t[cir[cur]]};
}
for (auto v: cir) {
top[v] = v, dep[v] = 0;
for (auto e: G[v]) {
if (tree[e])dfs(e, v);
}
}
for (auto v: num) {
cnt[v] = 0;
val[v].clear();
}
}
void solve() {
cin >> n >> k;
for (int i = 1; i <= n; i++)cin >> t[i];
for (int i = 1; i <= n; i++) {
cin >> a[i];
d[a[i]]++;
G[i].push_back(a[i]);
G[a[i]].push_back(i);
}
queue<int> que;
for (int i = 1; i <= n; i++)if (!d[i])que.push(i);
while (!que.empty()) {
int u = que.front();
que.pop();
tree[u] = 1;
for (auto v: G[u]) {
if (!d[v])continue;
d[v]--;
if (!d[v])que.push(v);
}
}
for (int i = 1; i <= n; i++) {
if (!tree[i] && !ans[i].first)solve(i);
}
ll res = 0;
for (int i = 1; i <= n; i++) {
res += 1ll * i * ans[i].second;
// cout << i << " " << ans[i].second << "\n";
}
cout << res << "\n";
clear();
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> T;
while (T--)solve();
}
/*
3
5 3
2 2 1 3 3
2 5 1 2 4
5 4
2 2 1 3 3
2 5 1 2 4
3 10
1 2 3
1 3 2
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 24956kb
input:
3 5 3 2 2 1 3 3 2 5 1 2 4 5 4 2 2 1 3 3 2 5 1 2 4 3 10 1 2 3 1 3 2
output:
41 45 14
result:
ok 3 lines
Test #2:
score: -100
Wrong Answer
time: 24ms
memory: 24120kb
input:
6000 19 48 18 19 18 19 11 9 15 19 12 18 11 18 9 18 9 18 19 11 15 12 14 18 8 1 3 19 5 13 14 15 2 14 5 19 2 19 12 9 15 23 3 1 1 3 6 1 4 1 1 6 6 4 12 4 6 14 1 8 8 6 6 12 14 6 8 5 7 14 2 5 9 140979583 4 5 8 9 2 7 6 8 2 8 9 4 6 9 2 4 7 8 4 976357580 2 3 1 3 2 1 1 4 6 508962809 4 3 4 3 4 4 4 5 4 5 5 6 13 ...
output:
3420 260 254 26 84 759 126 30 1092 1 2493 2422 168 360 298 324 2424 2520 220 228 1107 9 3486 1 796 81 340 272 600 3196 32 495 40 128 140 665 1635 702 68 96 90 288 29 588 16 234 445 2928 140 40 477 1197 19 1994 1082 32 522 672 20 390 32 2204 1938 42 21 885 4 1539 196 420 11 1709 801 720 1 556 40 17 2...
result:
wrong answer 255th lines differ - expected: '734', found: '744'