QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#648600 | #8237. Sugar Sweet II | yixuanoct | WA | 0ms | 7516kb | C++20 | 2.1kb | 2024-10-17 19:38:46 | 2024-10-17 19:38:47 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
ll qsm(ll x, ll n) {
ll res = 1;
while (n) {
if (n & 1) res = res * x % mod;
x = x * x % mod;
n >>= 1;
}
return res;
}
ll inv(ll a) { return qsm(a, mod - 2); }
ll fac[500003];
void solve() {
ll n;cin >> n;
vector<ll> a(n + 1), b(n + 1), w(n + 1), ans(n + 1), visit(n + 1);
for (ll i = 1; i <= n; i++) cin >> a[i];
for (ll i = 1; i <= n; i++) cin >> b[i];
for (ll i = 1; i <= n; i++) cin >> w[i];
vector<vector<ll>> g(n + 1);
for (ll i = 1; i <= n; i++) {
if (a[i] < a[b[i]]) ans[i] = a[i] + w[i];
else if (a[i] >= a[b[i]] + w[b[i]]) ans[i] = a[i], visit[i] = 1;
else if (i == b[i]) ans[i] = a[i], visit[i] = 1;
else g[i].push_back(b[i]);
}
vector<ll> sz(n + 1, 1);
vector<ll> v2(n + 1);
ll flag = 0, st = 0;
auto del = [&](ll u, auto&& del) {
v2[u] = 1;
if (g[u].size() == 0) return;
for (auto i : g[u]) {
if (v2[u]) {
flag = 1, st = u;
g[u].clear();
ans[u] = a[u];
return;
}
del(i, del);
}
if (flag) {
ans[u] = a[u];
g[u].clear();
if (u == st) flag = 0;
}
};
auto dfs = [&](ll u, auto&& dfs) {
visit[u] = 1;
if (g[u].size() == 0) return;
for (auto i : g[u]) {
if (!visit[i]) dfs(i, dfs);
sz[u] += sz[i];
ans[u] = (inv(fac[sz[u]]) * (a[u] + w[u]) % mod + ((fac[sz[u]] - 1 + mod) % mod * a[u]) % mod * inv(fac[sz[u]]) % mod) % mod;
}
};
for (ll i = 1; i <= n; i++) {
if (!v2[i]) del(i, dfs);
}
for (ll i = 1; i <= n; i++) {
if (!visit[i]) dfs(i, dfs);
cout << ans[i] << ' ';
}
cout << endl;
}
int main() {
fac[0] = 1;
fac[1] = 1;
for (ll i = 2; i <= 500000; i++) fac[i] = fac[i - 1] * i % mod;
ll t;cin >> t;
while (t--) solve();
}
详细
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 7516kb
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:
2 5 5 6 5 10 9 5 4 6 2 4 3 4 5
result:
wrong answer 1st numbers differ - expected: '500000007', found: '2'