QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#211965 | #5487. Movie Night | gondozu# | WA | 2ms | 8136kb | C++14 | 2.0kb | 2023-10-13 02:30:08 | 2023-10-13 02:30:09 |
Judging History
answer
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define Gondozu 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 = 1e5 + 5;
const int MOD = 1e9+7;
ll add(ll a, ll b) {
a += b;
if (a >= MOD) a -= MOD;
return a;
}
ll sub(ll a, ll b) {
a -= b;
if (a < 0) a += MOD;
return a;
}
ll mul(ll a, ll b) { return a * b % MOD; }
vi adj[N], adjr[N];
bool vis[N], inCyc[N];
vi nodes, cyc;
void dfs(int v){
vis[v] = true;
nodes.push_back(v);
for(int u : adj[v]){
if(vis[u]){
if(!inCyc[u]){
for (int i = nodes.size()-1; i >= 0; --i) {
inCyc[nodes[i]] = true;
cyc.pb(nodes[i]);
if(nodes[i] == u)
break;
}
}
continue;
}
dfs(u);
}
nodes.pop_back();
}
ll calc(int v, int par){
ll ret = 1;
for(int u : adjr[v]){
if(u == par || inCyc[u])
continue;
ret = mul(ret, calc(u, v));
}
return add(ret, 1);
}
void TC()
{
int n;
cin >> n;
int to;
for (int i = 1; i <= n; ++i) {
cin >> to;
adj[i].push_back(to);
adjr[to].push_back(i);
}
ll ans = 1;
for (int i = 1; i <= n; ++i) {
if(vis[i])
continue;
nodes.clear();
cyc.clear();
dfs(i);
ll cur = 1;
for(auto v : cyc){
cur = mul(cur, sub(calc(v,v), 1));
}
ans = mul(ans, add(cur, 1));
}
cout << sub(ans,1);
}
int32_t main() {
Gondozu
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: 100
Accepted
time: 0ms
memory: 8044kb
input:
4 2 3 4 3
output:
3
result:
ok single line: '3'
Test #2:
score: 0
Accepted
time: 2ms
memory: 8076kb
input:
5 2 3 1 5 4
output:
3
result:
ok single line: '3'
Test #3:
score: -100
Wrong Answer
time: 0ms
memory: 8136kb
input:
6 2 4 2 6 1 3
output:
7
result:
wrong answer 1st lines differ - expected: '3', found: '7'