QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#631997#8237. Sugar Sweet IIfoolnineWA 213ms5256kbC++202.9kb2024-10-12 11:21:432024-10-12 11:21:43

Judging History

你现在查看的是最新测评结果

  • [2024-11-04 16:59:03]
  • hack成功,自动添加数据
  • (/hack/1109)
  • [2024-10-12 11:21:43]
  • 评测
  • 测评结果:WA
  • 用时:213ms
  • 内存:5256kb
  • [2024-10-12 11:21:43]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

using i64 = long long;
const int N = 5e5;
const int mod = 1e9 + 7;

vector<int> inv(N, 1);

int power(int x, int n) {
    i64 ret = 1;
    while (n > 0) {
        if (n & 1) {
            ret = ret * x % mod;
        }
        x = 1LL * x * x % mod;
        n >>= 1;
    }
    return int(ret);
}

void solve() {
    int n;
    cin >> n;

    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    vector<int> b(n);
    for (int i = 0; i < n; i++) {
        cin >> b[i];
        b[i]--;
    }

    vector<int> w(n);
    for (int i = 0; i < n; i++) {
        cin >> w[i];
    }

    vector<int> col(n, 0);
    // 1 impossible
    // 2 must
    vector<vector<int>> e(n);

    for (int i = 0; i < n; i++) {
        if (b[i] == i || a[i] >= a[b[i]] + w[b[i]]) {
            col[i] = 1;
            continue;
        }
        if (a[i] < a[b[i]]) {
            col[i] = 2;
            continue;
        }
        e[b[i]].push_back(i);
    }

    vector<int> vis(n, -1);
    vector<pair<int, int>> cir;
    auto dfs = [&](auto self, int u, int c) -> void {
        vis[u] = c;
        for (auto v : e[u]) {
            if (vis[v] == -1) {
                self(self, v, c);
            } else if (vis[v] == c) {
                cir.emplace_back(u, v);
            }
        }
    };
    for (int i = 0; i < n; i++) {
        if (vis[i] == -1) {
            dfs(dfs, i, i);
        }
    }

    // cerr << "ok" << endl;

    for (auto [u, stop] : cir) {
        while (u != stop) {
            col[u] = 1;
            u = b[u];
        }
        col[stop] = 1;
    }

    vector<int> in(n, 0);
    vector<vector<int>> g(n);
    for (int i = 0; i < n; i++) {
        if (col[i] == 0 && col[b[i]] != 1) {
            g[b[i]].push_back(i);
            in[i]++;
        }
    }

    vector<int> ans(n);
    for (int i = 0; i < n; i++) {
        if (col[i] == 1) {
            ans[i] = a[i];
        } else if (col[i] == 2) {
            ans[i] = (a[i] + w[i]) % mod;
        }

        if (in[i] == 0) {
            queue<pair<int, int>> q;
            q.emplace(i, 1);

            while (!q.empty()) {
                auto [u, dep] = q.front();
                q.pop();

                for (auto v : g[u]) {
                    q.emplace(v, dep + 1);
                    ans[v] = (1LL * a[v] + 1LL * w[v] * inv[dep + 1] % mod) % mod;
                }
            }
        }
    }

    for (int i = 0; i < n; i++) {
        cout << ans[i] << " \n"[i == n - 1];
    }
}

int main(void)
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    for (int i = 2; i < N; i++) {
        inv[i] = 1LL * inv[i - 1] * power(i, mod - 2) % mod;
    }

    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: 67ms
memory: 5104kb

input:

4
4
2 5 5 2
4 2 1 3
3 2 1 4
3
5 4 3
1 1 1
6 6 6
3
5 4 3
2 3 1
1 2 3
5
2 1 3 2 1
5 1 1 3 4
1 3 4 2 4

output:

500000007 5 5 6
5 10 9
166666673 5 6
500000006 4 3 4 5

result:

ok 15 numbers

Test #2:

score: -100
Wrong Answer
time: 213ms
memory: 5256kb

input:

50000
5
508432375 168140163 892620793 578579275 251380640
3 4 4 1 3
346232959 736203130 186940774 655629320 607743104
1
863886789
1
364158084
18
864679185 463975750 558804051 604216585 694033700 499417132 375390750 337590759 467353355 111206671 983760005 984444619 322277587 138763925 205122047 97736...

output:

854665334 904343293 590444253 906393935 859123744
863886789
871186919 814243920 968784984 206455474 17527050 449261413 196759729 901433117 519383814 907574792 983760005 984444619 489899014 435736558 113628626 977360756 482247153 963066959
0 0 132646723 421298438 601054667 99438820 94575413
819542612...

result:

wrong answer 25th numbers differ - expected: '665922935', found: '0'