agc016 D - XOR Replace

Problem Statement

There is a tree with N vertices, numbered 1 through N. The i-th edge in this tree connects Vertices Ai and Bi and has a length of Ci.

Joisino created a complete graph with N vertices. The length of the edge connecting Vertices u and v in this graph, is equal to the shortest distance between Vertices u and v in the tree above.

Joisino would like to know the length of the longest Hamiltonian path (see Notes) in this complete graph. Find the length of that path.

Notes

A Hamiltonian path in a graph is a path in the graph that visits each vertex exactly once.

Constraints

2≤N≤105
1≤Ai

题目大意

给定两个序列a,b。
每次可以执行一个操作:
就序列的异或和来代换原序列的某个数。
求从a序列变为b序列的最少操作数。

题解

一个序列的异或和代换了一个原序列的数之后,
新序列的异或和就是这个被替换的数。
如果将序列的异或和单独作为一个数,
那么操作就变成了数与数之间的两两交换。
要使操作数最少,相同的必然是不作任何修改。
如果aibiai和bi不同,就在aibiai和bi之间连一条边,
在连完边之后,就在相应的联通块里面进行交换,花费就是联通块大小。
如果一个联通块原先并没有包含整个的异或和,
那么它还需要花费1的操作数来跟整个的异或和交换。

code

#include<queue>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <time.h>
#define ll long long
#define N 100003
#define M 103
#define db double
#define P putchar
#define G getchar
#define inf 998244353
using namespace std;
char ch;
void read(int &n)
{
    n=0;
    ch=G();
    while((ch<'0' || ch>'9') && ch!='-')ch=G();
    ll w=1;
    if(ch=='-')w=-1,ch=G();
    while('0'<=ch && ch<='9')n=(n<<3)+(n<<1)+ch-'0',ch=G();
    n*=w;
}

int max(int a,int b){return a>b?a:b;}
int min(int a,int b){return a<b?a:b;}
ll abs(ll x){return x<0?-x:x;}
ll sqr(ll x){return x*x;}
void write(ll x){if(x>9) write(x/10);P(x%10+'0');}

int a[N],b[N],c[N],d[N],n,ans,f[N],g[N],s,x,y;
bool bz[N];

int get(int x)
{
    return f[x]=(x==f[x]?x:get(f[x]));
}

int main()
{
    read(n);s=0;
    for(int i=1;i<=n;i++)
        read(a[i]),s^=a[i];
    a[n+1]=s;s=0;
    for(int i=1;i<=n;i++)
        read(b[i]),s^=b[i];
    b[++n]=s;

    for(int i=1;i<=n;i++)
        c[i]=a[i],d[i]=b[i],f[i]=i;

    sort(c+1,c+1+n);
    sort(d+1,d+1+n);

    for(int i=1;i<=n;i++)
        if(c[i]!=d[i])
        {
            P('-');P('1');
            return 0;
        }

    for(int i=1;i<=n;i++)
    {
        if(a[i]!=b[i] || i==n)
        {
            if(i!=n)ans++;
            x=lower_bound(c+1,c+1+n,a[i])-c;
            y=lower_bound(c+1,c+1+n,b[i])-c;
            bz[x]=bz[y]=1;
            f[get(x)]=get(y);
        }
    }
    if(ans==0)
    {
        P('0');
        return 0;
    }

    for(int i=1;i<=n;i++)
        if(bz[i] && f[i]==i)ans++;

    write(ans-1);

    return 0;
}
### ANDI、ORI、XOR 指令的用法与差异 在汇编语言中,`ANDI`、`ORI` 和 `XOR` 是逻辑操作指令,用于对寄存器中的值进行按位逻辑运算。以下是这些指令的具体用法和差异: #### 1. ANDI 指令 `ANDI` 指令执行按位与(AND)操作,通常将一个寄存器的值与一个立即数进行比较,并将结果存储回目标寄存器。其语法通常为: ```assembly ANDI target_register, source_register, immediate_value ``` - `target_register`:目标寄存器,存储结果。 - `source_register`:源寄存器,提供操作数。 - `immediate_value`:立即数,直接嵌入指令中的常量。 此操作会逐位比较两个操作数,并仅当两者均为 1 时返回 1[^2]。 #### 2. ORI 指令 `ORI` 指令执行按位或(OR)操作,通常将一个寄存器的值与一个立即数进行比较,并将结果存储回目标寄存器。其语法通常为: ```assembly ORI target_register, source_register, immediate_value ``` - `target_register`:目标寄存器,存储结果。 - `source_register`:源寄存器,提供操作数。 - `immediate_value`:立即数,直接嵌入指令中的常量。 此操作会逐位比较两个操作数,并当任意一位为 1 时返回 1[^3]。 #### 3. XOR 指令 `XOR` 指令执行按位异或(XOR)操作,通常将一个寄存器的值与另一个寄存器或立即数进行比较,并将结果存储回目标寄存器。其语法通常为: ```assembly XOR target_register, source_register, operand ``` - `target_register`:目标寄存器,存储结果。 - `source_register`:源寄存器,提供操作数。 - `operand`:可以是寄存器或立即数。 此操作会逐位比较两个操作数,并仅当两位不同(一个为 0,另一个为 1)时返回 1[^4]。 #### 示例代码 以下是一些示例代码,展示如何使用这些指令: ```assembly # 假设寄存器 $t0 = 0b1100, $t1 = 0b1010, 立即数 = 0b0101 # 使用 ANDI ANDI $t2, $t0, 0b0101 # 结果 $t2 = 0b0100 # 使用 ORI ORI $t3, $t1, 0b0101 # 结果 $t3 = 0b1111 # 使用 XOR XOR $t4, $t0, $t1 # 结果 $t4 = 0b0110 ``` #### 差异总结 - **功能**:`ANDI` 执行按位与操作,`ORI` 执行按位或操作,`XOR` 执行按位异或操作。 - **输入类型**:`ANDI` 和 `ORI` 通常使用立即数作为第二个操作数,而 `XOR` 可以接受寄存器或立即数。 - **用途**:`ANDI` 常用于清零某些位;`ORI` 常用于设置某些位;`XOR` 常用于翻转某些位或加密操作[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值