QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#638376 | #8237. Sugar Sweet II | tzw | TL | 51ms | 19968kb | C++23 | 2.7kb | 2024-10-13 15:46:14 | 2024-10-13 15:46:15 |
Judging History
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];
int getfa(int x)
{
while (fa[x])
x = fa[x];
return x;
}
void dfs(int x, int f, int dep)
{
vis[x] = 1;
// cout << w[b[x] ] << ' ' << P[dep] << endl;
ans[x] = (P[dep] * w[x] % mod + 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);
}
}
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 (a[i] >= a[b[i]] && a[i] < a[b[i]] + w[b[i]])
{
if (i != b[i])
{
add(b[i], i);
}
fa[i] = b[i];
// cout << b[i] << ' ' << i << 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] >= b[i] + w[b[i]])
{
ans[i] = a[i] % mod;
vis[i] = 1;
continue;
}
int root = getfa(i);
// cout << root << ' ';
dfs(root, 0, 1);
}
}
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: 19968kb
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
Time Limit Exceeded
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...