Codeforces Round #788 (Div. 2)
Problem - 1670C - Codeforces
题意:给你abd三个数组,要求你恢复处c数组,问你c数组有多少种可能。对于限制如下。1.如果d[i] !=0, c[i] = d[i], 其他c[i] = {a[i], b[i]};
思路:我们可以将一个数对(a[i], b[i])建一条边, 因为当d[i] == 0时,这条边只能2选1上一个点,那么对于图的每一个连通分量,只要其中一个位置的取法确定了, 其他就都确定了。
下面考虑一些特殊情况, 1.d[i] != 0, 那么显然c[i]是恒定的, 对答案是没有贡献的,因为这个位置不会改变. 2.a[i] = b[i], 同理这样c[i]也是固定的,不会改变, 否则的话,我们只需要去统计连通块的个数, 然后每个连通块的贡献是2,用并查集维护出来即可!
#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define endl '\n'
#define rep(i,a,n) for (int i = a; i < n; i ++ )
#define repn(i,a,n) for (int i = a; i <= n; i ++ )
#define pb push_back
#define IOS ios::sync_with_stdio(false); cin.tie(0);cout.tie(0);
typedef long long ll;
typedef unsigned long long ull;
#define all(x) (x).begin(),(x).end()
typedef pair<int,int> PII;
ll lowbit(ll x) { return x & (- x); }
ll gcd(ll a,ll b) { return b ? gcd(b,a % b) : a; }
const int mod = 1e9+7;
const int N = 200010;
int fa[N];
int n;
void init() {
for (int i = 0; i <= n; i ++ ) fa[i] = i;
}
int find(int x) {
if(x == fa[x]) return x;
fa[x] = find(fa[x]);
return fa[x];
}
void merge(int a, int b) {
fa[find(a)] = find(b);
}
void solve() {
cin >> n;
vector<array<int, 3>> arr(n);
rep(i, 0, n) cin >> arr[i][1];//a
rep(i, 0, n) cin >> arr[i][2];//b
rep(i, 0, n) cin >> arr[i][0];//d
init();
for(auto [d, a, b]: arr)
if(d != 0) merge(0, d);
for(auto [d, a, b]: arr)
if(a == b) merge(0, a);
for(auto [_, a, b]: arr)
merge(a, b);
set<int> s;
for (int i = 1; i <= n; i ++ ) {
if(find(i) == find(0)) continue;
s.insert(find(i));
}
ll ans = 1;
for (int i = 1; i <= s.size(); i ++ ) ans *= 2, ans %= mod;
cout << ans << endl;
}
int main(){
IOS;
int t;
cin >> t;
while(t -- ){
solve();
}
return 0;
}