QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#188303 | #5487. Movie Night | Beevo# | WA | 1ms | 3508kb | C++20 | 2.5kb | 2023-09-25 18:38:47 | 2023-09-25 18:38:47 |
Judging History
answer
#include <bits/stdc++.h>
#define el '\n'
#define Beevo ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
typedef long long ll;
typedef long double ld;
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const int M = 1e9 + 7;
int mul(int a, int b) {
return 1LL * a * b % M;
}
int add(int a, int b) {
b = (b + M) % M;
return (a + b) % M;
}
vector<vector<int>> adj, adj_rev;
vector<bool> used;
vector<int> order, component;
void dfs1(int v) {
used[v] = true;
for (auto u : adj[v])
if (!used[u])
dfs1(u);
order.push_back(v);
}
void dfs2(int v) {
used[v] = true;
component.push_back(v);
for (auto u : adj_rev[v])
if (!used[u])
dfs2(u);
}
int dfs(int u, vector<vector<int>> &g) {
if (g[u].empty())
return 1;
return dfs(g[u][0], g) + 1;
}
void testCase() {
int n;
cin >> n;
adj.resize(n), adj_rev.resize(n);
for (int i = 0; i < n; i++) {
int a;
cin >> a;
a--;
adj[i].push_back(a);
adj_rev[a].push_back(i);
}
used.assign(n, false);
for (int i = 0; i < n; i++)
if (!used[i])
dfs1(i);
used.assign(n, false);
reverse(order.begin(), order.end());
vector<int> roots(n, 0);
vector<int> root_nodes;
vector<vector<int>> adj_scc(n);
for (auto v : order)
if (!used[v]) {
dfs2(v);
int root = component.front();
for (auto u : component) roots[u] = root;
root_nodes.push_back(root);
component.clear();
}
vector<int> in(n);
for (int v = 0; v < n; v++)
for (auto u : adj[v]) {
int root_v = roots[v],
root_u = roots[u];
if (root_u != root_v)
adj_scc[root_u].push_back(root_v);
}
int sol = 1;
for (int i = 0; i < n; i++) {
if (!in[i] && roots[i] == i) {
if (adj_scc[i].empty()) {
sol = mul(sol, 2);
continue;
}
int cur = 1;
for (auto &j: adj_scc[i])
cur = mul(cur, dfs(j, adj_scc) + 1);
sol = mul(sol, add(cur, 1));
}
}
cout << add(sol, -1);
}
signed main() {
Beevo
int t = 1;
// cin >> t;
while (t--)
testCase();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3508kb
input:
4 2 3 4 3
output:
23
result:
wrong answer 1st lines differ - expected: '3', found: '23'