Codeforces Round #788 (Div. 2)--C - Where is the Pizza?

 Codeforces Round #788 (Div. 2)--C - Where is the Pizza?

C. Where is the Pizza?

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

While searching for the pizza, baby Hosssam came across two permutations a� and b� of length n�.

Recall that a permutation is an array consisting of n� distinct integers from 11 to n� in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array) and [1,3,4][1,3,4] is also not a permutation (n=3�=3 but there is 44 in the array).

Baby Hosssam forgot about the pizza and started playing around with the two permutations. While he was playing with them, some elements of the first permutation got mixed up with some elements of the second permutation, and to his surprise those elements also formed a permutation of size n�.

Specifically, he mixed up the permutations to form a new array c� in the following way.

  • For each i� (1≤i≤n1≤�≤�), he either made ci=ai��=�� or ci=bi��=��.
  • The array c� is a permutation.

You know permutations a�, b�, and values at some positions in c�. Please count the number different permutations c� that are consistent with the described process and the given values. Since the answer can be large, print it modulo 109+7109+7.

It is guaranteed that there exists at least one permutation c� that satisfies all the requirements.

Input

The first line contains an integer t� (1≤t≤1051≤�≤105) — the number of test cases.

The first line of each test case contains a single integer n� (1≤n≤1051≤�≤105) — the length of the permutations.

The next line contains n� distinct integers a1,a2,…,an�1,�2,…,�� (1≤ai≤n1≤��≤�) — the first permutation.

The next line contains n� distinct integers b1,b2,…,bn�1,�2,…,�� (1≤bi≤n1≤��≤�) — the second permutation.

The next line contains n� distinct integers d1,d2,…,dn�1,�2,…,�� (di�� is either 00, ai��, or bi��) — the description of the known values of c�. If di=0��=0, then there are no requirements on the value of ci��. Otherwise, it is required that ci=di��=��.

It is guaranteed that there exists at least one permutation c� that satisfies all the requirements.

It is guaranteed that the sum of n� over all test cases does not exceed 5⋅1055⋅105.

Output

For each test case, print the number of possible permutations c�, modulo 109+7109+7.

#include<bits/stdc++.h>
using namespace std;
#define Acode ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
typedef long long ll;
const int inf = 0x3f3f3f3f;
#define endl '\n'
const int N = 1e6 + 10;
int a[N], b[N], c[N];
int siz[N];
int fa[N];
bool is_valid[N];
int x;
const ll p = 1e9 + 7;
void init()
{
	for (int i = 1; i <= x; i++)
	{
		fa[i] = i; siz[i] = 0;
		is_valid[i] = true;
	}
}
int find(int x)
{
	if (fa[x] == x) return x;
	else
	{
		fa[x] = find(fa[x]);
		return fa[x];
	}
}

void merge(int x, int y)
{
	int fx = find(x);
	int fy = find(y);
	fa[fx] = fy;
}

int main()
{
	Acode;
	int t;
	cin >> t;
	while (t--)
	{
		cin >> x;
		init();
		for (int i = 1; i <= x; i++) cin >> a[i];
		for (int i = 1; i <= x; i++) cin >> b[i];
		for (int i = 1; i <= x; i++) cin >> c[i];

		for (int i = 1; i <= x; i++)
		{
			merge(a[i], b[i]);
		}

		for (int i = 1; i <= x; i++)
		{
			if (c[i] != 0)
			{
				is_valid[find(c[i])] = false;
			}
			siz[find(a[i])] += 2;
		}

		int res = 1;
		for (int i = 1; i <= x; i++)
		{
			int m = find(a[i]);
			if (is_valid[m])
			{
				if (siz[m] > 2) res = (res * 2) % p;
			}
			is_valid[m] = false;
		}
		cout << res << endl;
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值