QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#323996 | #8237. Sugar Sweet II | ucup-team045# | WA | 161ms | 7504kb | C++20 | 5.7kb | 2024-02-10 15:05:00 | 2024-02-10 15:05:00 |
Judging History
answer
#include<iostream>
#include<cstring>
#include<vector>
#include<array>
#include<queue>
using namespace std;
using LL = long long;
const int maxn = 5e5 + 5, mod = 1e9 + 7;
template<const int T>
struct ModInt {
const static int mod = T;
int x;
ModInt(int x = 0) : x(x % mod) {}
ModInt(long long x) : x(int(x % mod)) {}
int val() { return x; }
ModInt operator + (const ModInt &a) const { int x0 = x + a.x; return ModInt(x0 < mod ? x0 : x0 - mod); }
ModInt operator - (const ModInt &a) const { int x0 = x - a.x; return ModInt(x0 < 0 ? x0 + mod : x0); }
ModInt operator * (const ModInt &a) const { return ModInt(1LL * x * a.x % mod); }
ModInt operator / (const ModInt &a) const { return *this * a.inv(); }
bool operator == (const ModInt &a) const { return x == a.x; };
bool operator != (const ModInt &a) const { return x != a.x; };
void operator += (const ModInt &a) { x += a.x; if (x >= mod) x -= mod; }
void operator -= (const ModInt &a) { x -= a.x; if (x < 0) x += mod; }
void operator *= (const ModInt &a) { x = 1LL * x * a.x % mod; }
void operator /= (const ModInt &a) { *this = *this / a; }
friend ModInt operator + (int y, const ModInt &a){ int x0 = y + a.x; return ModInt(x0 < mod ? x0 : x0 - mod); }
friend ModInt operator - (int y, const ModInt &a){ int x0 = y - a.x; return ModInt(x0 < 0 ? x0 + mod : x0); }
friend ModInt operator * (int y, const ModInt &a){ return ModInt(1LL * y * a.x % mod);}
friend ModInt operator / (int y, const ModInt &a){ return ModInt(y) / a;}
friend ostream &operator<<(ostream &os, const ModInt &a) { return os << a.x;}
friend istream &operator>>(istream &is, ModInt &t){return is >> t.x;}
ModInt pow(int64_t n) const {
ModInt res(1), mul(x);
while(n){
if (n & 1) res *= mul;
mul *= mul;
n >>= 1;
}
return res;
}
ModInt inv() const {
int a = x, b = mod, u = 1, v = 0;
while (b) {
int t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
if (u < 0) u += mod;
return u;
}
};
using mint = ModInt<mod>;
mint fact[maxn], invfact[maxn];
void init(){
fact[0] = invfact[0] = 1;
for(int i = 1; i < maxn; i++) fact[i] = fact[i - 1] * i;
invfact[maxn - 1] = fact[maxn - 1].inv();
for(int i = maxn - 2; i; i--)
invfact[i] = invfact[i + 1] * (i + 1);
}
inline mint C(int a, int b){
if (a < 0 || b < 0 || a < b) return 0;
return fact[a] * invfact[b] * invfact[a - b];
}
int main(){
#ifdef LOCAL
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
init();
int T;
cin >> T;
while(T--){
int n;
cin >> n;
vector<int> a(n + 1), b(n + 1), w(n + 1);
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];
vector<bool> v(n + 1), oncyle(n + 1);
vector<vector<int> > g(n + 1);
for(int i = 1; i <= n; i++){
g[b[i]].push_back(i);
g[i].push_back(b[i]);
}
vector<mint> ans(n + 1);
vector<bool> del(n + 1);
for(int i = 1; i <= n; i++){
if (v[i]) continue;
int t = i;
while(!v[t]){
v[t] = true;
t = b[t];
}
vector<int> cycle{t};
int j = b[t];
while(j != t){
cycle.push_back(j);
j = b[j];
}
for(auto x : cycle){
oncyle[x] = true;
}
auto dfs = [&](auto &&dfs, int u, int fa) -> void {
v[u] = true;
for(auto j : g[u]){
if (j == fa || oncyle[j]) continue;
dfs(dfs, j, u);
}
};
for(auto x : cycle) dfs(dfs, x, -1);
int ban = -1;
for(auto x : cycle){
if (b[x] == x){
ban = x;
break;
}
if ((a[x] < a[b[x]] + w[b[x]]) == (a[x] < a[b[x]])){
ban = x;
break;
}
}
if (ban == -1) ban = cycle[0];
del[ban] = true;
}
for(int i = 1; i <= n; i++) g[i].clear();
vector<int> din(n + 1);
for(int i = 1; i <= n; i++){
if (!del[i]){
g[b[i]].push_back(i);
din[i] += 1;
}
}
queue<int> q;
for(int i = 1; i <= n; i++){
if (din[i] == 0){
q.push(i);
}
}
const int INF = 1e9;
vector<int> f(n + 1, -INF);
while(!q.empty()){
int t = q.front();
q.pop();
if (a[t] >= a[b[t]] + w[b[t]] || b[t] == t){
f[t] = -INF;
ans[t] = a[t];
}
else if (a[t] < a[b[t]]){
f[t] = 1;
ans[t] = a[t] + w[t];
}
else{
f[t] = f[b[t]] + 1;
if (f[t] >= 0){
ans[t] = a[t] + invfact[f[t]] * w[t];
}
}
for(auto j : g[t]){
if (--din[j] == 0){
q.push(j);
}
}
}
for(int i = 1; i <= n; i++){
cout << ans[i] << " \n"[i == n];
}
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 7500kb
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: 161ms
memory: 7504kb
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'