QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#210194 | #5487. Movie Night | hagry# | WA | 1ms | 3428kb | C++14 | 2.3kb | 2023-10-11 08:33:30 | 2023-10-11 08:33:31 |
Judging History
answer
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define MP make_pair
#define all(x) x.begin(),x.end()
#define Hagry ios::sync_with_stdio(false);cout.tie(NULL);cin.tie(NULL);
using namespace std;
using ll = long long;
using pi = pair<int, int>;
using vi = vector<int>;
using vpi = vector<pair<int, int>>;
using vvi = vector<vector<int>>;
const int OO = 1e9 + 5;
const int N = 2e5 + 5;
const int MOD = 1e9 + 7;
ll mul(ll a, ll b) {
return a * b % MOD;
}
ll sub(ll a, ll b) {
a -= b;
if (a < 0) a += MOD;
return a;
}
void TC() {
int n;
cin >> n;
int nxt[n + 1];
nxt[0] = 0;
for (int i = 1; i <= n; ++i)
cin >> nxt[i];
vi len(n + 1), vis(n + 1), in(n + 1), cycID(n + 1);
ll ans = 1;
int cycNum = 0;
for (int i = 1; i <= n; ++i) {
++in[nxt[i]];
if (vis[i])continue;
stack<int> st;
st.push(i);
while (!vis[st.top()]) {
vis[st.top()] = i;
st.push(nxt[st.top()]);
}
if (vis[st.top()] == i) {
++cycNum;
int prv;
int start = st.top();
st.pop();
while (!st.empty() && vis[st.top()] == i) {
cycID[st.top()] = cycNum;
prv = st.top();
st.pop();
if (prv == start) break;
}
}
if (!st.empty()) {
int prv = st.top();
st.pop();
while (!st.empty()) {
cycID[st.top()] = cycID[prv];
len[st.top()] = len[prv] + 1;
prv = st.top();
st.pop();
}
}
}
vector<ll> ways(cycNum + 1, 1);
for (int i = 1; i <= n; ++i) {
if (in[i] == 0)
ways[cycID[i]] = mul(ways[cycID[i]], len[i] + 1);
}
for (int i = 1; i <= cycNum; ++i)
ans = mul(ans, ways[i] + 1);
ans = sub(ans, 1);
cout << ans;
}
int32_t main() {
#ifndef ONLINE_JUDGE
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
#endif
Hagry
int t = 1;
// cin >> t;
while (t--) {
TC();
cout << '\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3428kb
input:
4 2 3 4 3
output:
1
result:
wrong answer 1st lines differ - expected: '3', found: '1'