QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#638560#8237. Sugar Sweet IIxuemanWA 165ms20036kbC++173.1kb2024-10-13 16:14:202024-10-13 16:14:20

Judging History

This is the latest submission verdict.

  • [2024-11-04 16:59:03]
  • hack成功,自动添加数据
  • (/hack/1109)
  • [2024-10-13 16:14:20]
  • Judged
  • Verdict: WA
  • Time: 165ms
  • Memory: 20036kb
  • [2024-10-13 16:14:20]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
const int N = 5e5 + 10;
const int maxn = 1e6 + 10;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;

ll qpow(ll a, ll n)
{
    ll res = 1;
    while (n)
    {
        if (n & 1)
            res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

struct egde
{
    int to, ne;
} e[maxn];
int head[N], ecnt;

void add(int x, int y)
{
    e[++ecnt].to = y;
    e[ecnt].ne = head[x];
    head[x] = ecnt;
}

ll a[N], b[N], w[N];
int fa[N];
bool vis[N];

ll P[N];
ll ans[N];
bool ok = 0;

int getfa(int x)
{
    // cout << x << ' ';
    while (fa[x] != x && fa[x] != 0)
    {
        x = fa[x];
    }
    if (x != 0 && fa[x] == x)
    {
        ok = 1;
    }
    return x;
}

void dfs(int x, int f, int dep, bool op)
{
    vis[x] = 1;
    // cout << w[b[x] ] << ' ' << P[dep] << endl;
    if (!op)
        ans[x] = (P[dep] * w[x] % mod + a[x]) % mod;
    else
        ans[x] = a[x] % mod;
    for (int i = head[x]; i; i = e[i].ne)
    {
        int y = e[i].to;
        if (y == f)
            continue;
        dfs(y, x, dep + 1, op);
    }
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll res = 1;
    for (int i = 1; i < N; i++)
    {
        res = res * i % mod;
        P[i] = qpow(res, mod - 2) % mod;
    }
    int T;
    cin >> T;
    while (T--)
    {
        int n;
        cin >> n;

        for (int i = 0; i <= n; i++)
        {
            head[i] = 0;
            fa[i] = vis[i] = 0;
        }
        ecnt = 0;

        for (int i = 1; i <= n; i++)
            cin >> a[i];
        for (int i = 1; i <= n; i++)
            cin >> b[i];
        for (int i = 1; i <= n; i++)
            cin >> w[i];
        for (int i = 1; i <= n; i++)
        {
            // if (i == b[i]) continue;
            if (a[i] >= a[b[i]] && a[i] < a[b[i]] + w[b[i]])
            {
                add(b[i], i);
                // cout << i << ' '
                fa[i] = b[i];

                // cout << b[i] << ' ' << i << endl;
            }
        }

        // for (int i = 1; i <= n; i++)
        //     cout << fa[i] << ' ';
        // cout << endl;

        for (int i = 1; i <= n; i++)
        {
            if (!vis[i])
            {
                // if (i == fa[i])
                // {
                //     ans[i] = a[i] % mod;
                //     vis[i] = 1;
                //     continue;
                // }

                if (a[i] >= a[b[i]] + w[b[i]])
                {
                    // cout << i << ' ';
                    ans[i] = a[i] % mod;
                    vis[i] = 1;
                    continue;
                }

                ok = 0;
                int root = getfa(i);
                // cout << root << ' ';
                // if (root = 1)
                // cout << root << ' ';
                dfs(root, 0, 1, ok);
            }
        }

        for (int i = 1; i <= n; i++)
            cout << ans[i] << ' ';
        cout << endl;
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 51ms
memory: 20036kb

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: 165ms
memory: 19992kb

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 
665922935 577926775 132646723 421298438 601054667 99438820...

result:

wrong answer 60th numbers differ - expected: '693925571', found: '358639004'