QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#631247 | #8237. Sugar Sweet II | foolnine# | WA | 189ms | 3812kb | C++20 | 3.8kb | 2024-10-11 23:18:30 | 2024-10-11 23:18:40 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const i64 mod = 1e9 + 7;
struct node {
int u, c;
i64 cal;
node() {}
node(int u, int c, i64 cal) : u(u), c(c), cal(cal) {}
};
i64 power(i64 x, int n) {
i64 ret = 1;
while (n > 0) {
if (n & 1) {
ret = ret * x % mod;
}
x = x * x % mod;
n >>= 1;
}
return ret;
}
i64 inv(i64 x) {
return power(x, mod - 2);
}
void solve() {
int n;
cin >> n;
vector<i64> 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<i64> w(n);
for (int i = 0; i < n; i++) {
cin >> w[i];
}
vector<i64> ans(n, -1);
vector<vector<int>> e(n);
vector<int> deg(n, 0);
for (int i = 0; i < n; i++) {
if (b[i] == i) {
ans[i] = a[i];
continue;
}
i64 cx = a[i], cy = a[b[i]];
if (cy > cx) {
ans[i] = a[i] + w[i];
continue;
}
if (cx >= cy + w[b[i]]) {
ans[i] = a[i];
continue;
}
e[b[i]].push_back(i);
deg[i]++;
}
for (int i = 0; i < n; i++) {
if (deg[i] == 0 && ans[i] != -1 && ans[i] == a[i]) {
int f = (ans[i] == a[i]);
queue<int> q;
q.push(i);
while (!q.empty()) {
auto u = q.front();
q.pop();
for (auto v : e[u]) {
ans[v] = a[v];
q.push(v);
}
}
}
}
vector<int> pre(n, -1), vis(n, -1);
vector<int> f;
for (int i = 0; i < n; i++) {
if (vis[i] == -1) {
queue<pair<int, int>> q;
vis[i] = i;
q.emplace(i, i);
while (!q.empty()) {
auto [u, flag] = q.front();
q.pop();
for (auto v : e[u]) {
if (vis[v] == flag) {
f.push_back(u);
} else if (vis[v] == -1) {
vis[v] = flag;
pre[v] = u;
q.emplace(v, flag);
}
}
}
}
}
for (auto u : f) {
while (u != -1) {
ans[u] = a[u];
u = pre[u];
}
}
vector<int> in(n, 0);
vector<vector<int>> g(n);
// cerr << "---" << endl;
// for (int i = 0; i < n; i++) {
// cerr << e[i].size() << " \n"[i == n - 1];
// }
// for (int i = 0; i < n; i++) {
// if (in[i] != 0) {
// cerr << i + 1 << " ";
// }
// }
// cerr << endl;
for (int i = 0; i < n; i++) {
if (ans[i] == -1) {
g[b[i]].push_back(i);
in[i]++;
}
}
for (int i = 0; i < n; i++) {
if (in[i] == 0) {
queue<node> q;
q.emplace(i, 1, 1);
while (!q.empty()) {
auto [u, c, cal] = q.front();
q.pop();
for (auto v : g[u]) {
i64 nc = cal * inv(i64(c + 1)) % mod;
// cerr << cal << " " << inv(1LL * c + 1) << endl;
ans[v] = (a[v] + w[v] * nc) % mod;
q.emplace(v, c + 1, nc);
}
}
}
}
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);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3812kb
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: 189ms
memory: 3668kb
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 1206455481 17527050 1449261420 196759729 901433117 519383814 907574792 983760005 984444619 489899014 435736558 1113628633 977360756 482247153 963066959 665922935 577926775 132646723 421298438 601054667 10994388...
result:
wrong answer 10th numbers differ - expected: '206455474', found: '1206455481'