LU ICPC Selection Contest 2020 and KFU Open Contest 2020

Question URL: https://codeforces.ml/gym/102862/problem/K
Question meaning:
give us a 0 1 sequence, this is the target sequence, we only have an initial sequence of all 0s at the beginning, and then give m pairs of operations (i,j), which means that we can select a pair (i,j) from these m pairs each time, so that a[i] and a[j] 01 are reversed, and ask whether we can change from the initial sequence to the target sequence .

Not at first, but later I asked the boss to know how to write it. It was wonderful.
Solution:
We can find that for each operation, if it is 0 0->1 1, either 1 1->0 0, or 0 1->1 0, that is to say, the number of 0 1 is either unchanged or even multiple , Then the problem is simple, we only need to construct the edges of these m pairs, and then calculate whether the number of 1s in each connected block is even. Here I directly use Union Check to connect these points, and then maintain the number of 1.
Code:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<set>
#define iss ios::sync_with_stdio(false)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int MAXN=1e5+5;
const int mod=1e9+7;
const int INF=0x3f3f3f3f;
int n;
int b[MAXN];
int m;
int l[MAXN];
int r[MAXN];
int fa[MAXN];
int sum[MAXN];
void init()
{
    
    
    for(int i=1;i<=n;i++) fa[i]=i;
}
int finder(int x)
{
    
    
    if(fa[x]==x) return x;
    else return fa[x]=finder(fa[x]);
}
void combine(int u,int v)
{
    
    
    int x=finder(u);
    int y=finder(v);
    if(x!=y)
    {
    
    
        fa[x]=y;
        b[y]+=b[x];
    }
}
int main()
{
    
    
   
    scanf("%d",&n);init();
    for(int i=1;i<=n;i++)
    {
    
    
        scanf("%d",&b[i]);
    }
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
    
    
        scanf("%d%d",&l[i],&r[i]);
        combine(l[i],r[i]);
    }
    int flag=0;
    for(int i=1;i<=n;i++)
    {
    
    
        if(finder(i)==i)
        {
    
    
            if(b[i]&1)
            {
    
    
                flag=1;
                break;
            }
        }
    }
    if(flag) printf("No\n");
    else printf("Yes\n");
}

Guess you like

Origin blog.csdn.net/weixin_45755679/article/details/110209257