-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path319-where-is-the-pizza.cpp
More file actions
66 lines (63 loc) · 1.52 KB
/
Copy path319-where-is-the-pizza.cpp
File metadata and controls
66 lines (63 loc) · 1.52 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
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* Where is the Pizza? [1670C]
* Problem: https://codeforces.com/problemset/problem/1670/C
* Verdict: ACCEPTED Solved: 2025-03-03
* Language: C++17 (GCC 7-32)
* Runtime: 140 ms Memory: 2400 KB
* Tags: data structures, dfs and similar, dsu, graphs, implementation, math
* Author: BidoTeima
* Source: https://codeforces.com/contest/1670/submission/308777148
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1e9 + 7;
const int N = 1e5 + 3;
bool vis[N];
vector<int>adj[N];
void dfs(int node){
if(vis[node])return;
vis[node]=1;
for(int child : adj[node]){
dfs(child);
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
for(int i = 1; i <= n; i++)vis[i]=0,adj[i].clear();
int a[n],b[n],c[n];
for(auto&i:a)cin>>i;
for(auto&i:b)cin>>i;
for(auto&i:c)cin>>i;
for(int i = 0; i < n; i++){
if(!c[i]){
adj[a[i]].push_back(b[i]);
adj[b[i]].push_back(a[i]);
}
}
for(int i = 0; i < n; i++){
if(c[i]){
dfs(c[i]);
}
if(a[i] == b[i]){
dfs(a[i]);
}
}
ll ans = 1;
for(int i = 1; i <= n; i++){
if(!vis[i]){
dfs(i);
ans=(ans*2)%mod;
}
}
cout<<ans<<'\n';
}
return 0;
}