-
Notifications
You must be signed in to change notification settings - Fork 0
/
contest-10235.cpp
102 lines (100 loc) · 1.78 KB
/
contest-10235.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//Be Name Khoda
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int MOD = (int)1e9 + 7;
const int MAXN = (int)2e5 + 7;
const ll inf = (ll)1e18;
const int infint = (int)2e9;
int a[MAXN], b[MAXN], out[MAXN], visited[MAXN], c[MAXN], indx[MAXN];
int comp = 0;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a[i];
indx[a[i]] = i;
}
for (int i = 0; i < n; i++)
{
cin >> b[i];
out[a[i]] = b[i];
}
for (int i = 0; i < n; i++)
cin >> c[i];
vector<int> first;
for (int i = 1; i <= n; i++)
{
if(!visited[i])
{
int root = -1, j = i;
comp++;
vector<int> all;
all.push_back(j);
visited[j] = comp;
while(!visited[out[j]])
{
all.push_back(out[j]);
visited[out[j]] = comp;
j = out[j];
}
j = out[j];
if(visited[j] < comp)
continue;
//cout << j << "\n";
int ind = -1;
for (int k = 0; k < all.size(); k++)
{
if(all[k] == j)
{
ind = k;
}
if(ind != -1)
first.push_back(all[k]);
}
}
}
fill(out, out + MAXN, -1);
fill(visited, visited + MAXN, 0);
comp = 0;
for (int i = 0; i < first.size(); i++)
out[first[i]] = c[indx[first[i]]];
int ans1 = 0;
for (auto i : first)
{
if(!visited[i])
{
int root = -1, j = i;
comp++;
vector<int> all;
all.push_back(j);
visited[j] = comp;
while(out[j] != 0 and !visited[out[j]])
{
all.push_back(out[j]);
visited[out[j]] = comp;
j = out[j];
}
j = out[j];
if(visited[j] < comp or out[j] == 0)
continue;
//cout << j << "\n";
int ind = -1;
for (int k = 0; k < all.size(); k++)
{
if(all[k] == j)
{
ind = k;
break;
}
}
ans1 += all.size() - ind;
}
}
cout << n - ans1;
}