Where is the Pizza?

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;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值