-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2008d.cpp
More file actions
53 lines (47 loc) · 1.18 KB
/
2008d.cpp
File metadata and controls
53 lines (47 loc) · 1.18 KB
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
#include <bits/stdc++.h>
using namespace std;
#define NO cout<<"NO\n"
#define YES cout<<"YES\n"
#define F first
#define S second
#define cases int _; cin >> _; while(_--)
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef set<int> si;
void solve() {
int a; cin >> a;
vi A(a); for(int i = 0; i < a; i++) cin >> A[i];
string s; cin >> s;
vi result(a, 0);
vector<bool> visited(a, false);
for(int i = 0; i < a; i++) {
if(!visited[i]) {
vi cycle;
int current = i;
while(!visited[current]) {
visited[current] = true;
cycle.push_back(current);
current = A[current] - 1;
}
int zero_count = 0;
for(int node : cycle) {
if(s[node] == '0') {
zero_count++;
}
}
for(int node : cycle) {
result[node] = zero_count;
}
}
}
for(int i = 0; i < a; i++) {
cout << result[i] << ' ';
}
cout << '\n';
}
int main() {
cases solve();
return 0;
}