QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#211108 | #5487. Movie Night | hagry | WA | 1ms | 6572kb | C++14 | 2.0kb | 2023-10-12 09:52:37 | 2023-10-12 09:52:38 |
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 = 1e5 + 5;
const int MOD = 1e9 + 7;
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 from[N];
bool inCycle[N];
// 0 not vis, 1 vis in cur loop, 2 vis in prev loop
int vis[N], nxt[N], cycID[N], sNode, numCyc;
bool dfs(int u){
if(vis[u] == 2)return false;
if(vis[u] == 1){
sNode = u;
inCycle[u] = true;
cycID[u] = ++numCyc;
return true;
}
vis[u] = 1;
bool inCyc = dfs(nxt[u]);
inCycle[u] = inCyc;
if(inCyc && sNode == u){
inCyc = false;
cycID[u] = numCyc;
}
vis[u] = 2;
return inCyc;
}
ll dfs2(int u){
ll ways = 1;
for(auto e:from[u]){
if(inCycle[e])continue;
ways = mul(ways, dfs2(e)+1);
}
return ways;
}
void TC(){
int n;
cin >> n;
for(int i=1; i<=n; ++i){
cin >> nxt[i];
from[nxt[i]].pb(i);
}
for(int i=1; i<=n; ++i){
if(vis[i])continue;
dfs(i);
}
vector<ll> cycWays(numCyc+1, 1);
for(int i=1; i<=n; ++i){
if(inCycle[i])
cycWays[cycID[i]] = mul(cycWays[cycID[i]], dfs2(i));
}
ll ans = 1;
for(int i=1; i<=numCyc; ++i)
ans = mul(ans, cycWays[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;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 6040kb
input:
4 2 3 4 3
output:
3
result:
ok single line: '3'
Test #2:
score: 0
Accepted
time: 1ms
memory: 6364kb
input:
5 2 3 1 5 4
output:
3
result:
ok single line: '3'
Test #3:
score: 0
Accepted
time: 1ms
memory: 6016kb
input:
6 2 4 2 6 1 3
output:
3
result:
ok single line: '3'
Test #4:
score: -100
Wrong Answer
time: 0ms
memory: 6572kb
input:
8 2 3 4 1 3 3 1 4
output:
2
result:
wrong answer 1st lines differ - expected: '16', found: '2'