2018 UESTC Training for Data Structures--并查集

6 篇文章 0 订阅
3 篇文章 0 订阅

I.不如把并查集加上个计数功能吧
给并查集加上计数功能

#include <bits/stdc++.h>
using namespace std;

const int MAX = 100010;
int uset[MAX];
int num[MAX];

void makeSet(int s)
{
    for(int i = 0; i < s; i++)
    {
        uset[i] = i;
        num[i] = 1;
    }
}
int findSet(int x)
 {
    if (x != uset[x])
        uset[x] = findSet(uset[x]);
    return uset[x];
}
void unionSet(int x,int y)
{
    int fx = findSet(x);
    int fy = findSet(y);

    if(fx!=fy)
    {
        num[fy] += num[fx];
        uset[fx] = fy;
    }
}

int n,m,q,x,y,z;
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        makeSet(n);
        for(int i = 0;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            unionSet(x,y);
        }
        scanf("%d",&q);
        for(int i = 0;i<q;i++)
        {
            scanf("%d",&z);
            printf("%d\n",num[findSet(z)]);
        }
    }
    return 0;
}

J.老头马桶枪!
经典题poj 1182“食物链”改编,这种环状的关系用”带边权“并查集很简单,几乎就是个模板题,当然也能用”扩展域”并查集写

当sett[x]=y的情况下(即x与y有关系):
val[x]==val[y]相等说明x和y是同类
(val[x]-val[y]) % 3 = 1说明x克制y
(val[x]-val[y]) % 3 = -1 = 2说明y克制x
路径压缩:儿子和爷爷之间的关系=(儿子和父亲的关系+父亲和爷爷的关系)%3

#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
int sett[maxn];
int val[maxn];


int findSet(int x)
{
    if(x==sett[x])
        return  x;
    int y=findSet(sett[x]);
    val[x]=(val[x]+val[sett[x]])%3;
    return sett[x]=y;
}

int n,m,k,x,y;

int main()
{
    scanf("%d%d",&n,&m);
    for(int i =1; i<=n; i++)
    {
        sett[i] = i;
        val[i] = 0;
    }
    int ans = -1;
    for(int i = 0; i<m; i++)
    {
        scanf("%d%d%d",&k,&x,&y);
        if(ans == -1)
        {
            int x1=findSet(x);
            int y1=findSet(y);
            if(x1!=y1)
            {
                sett[y1] = x1;
                val[y1]=(-val[y]+k-1+val[x]+3)%3;
            }
            else
            {
                if(k==1)
                {
                    if(val[x]!=val[y])
                        ans =  (i%3)+1;
                }
                else
                {
                    if((3-val[x]+val[y])%3!=1)
                        ans =  (i%3)+1;
                }
            }
        }
    }
    printf("%d\n",ans);
}

K.爱吃瓜的伊卡洛斯(1)
两种并查集都能写。。。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 200050;

int sett[maxn];
int val[maxn];

int findSet(int x)
{
    if(x==sett[x])
        return x;
    int y = findSet(sett[x]);
    val[x] = (val[x]+val[sett[x]])%2;
    return sett[x] = y;
}

int x,y,k;

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i = 1; i<=n; i++)
    {
        sett[i] = i;
        val[i]  = 0;
    }
    char op;
    for(int i =0; i<m; i++)
    {
        cin>>op;
        if(op=='A')
        {
            scanf("%d%d%d",&x,&y,&k);
            int x1 = findSet(x);
            int y1 = findSet(y);
            if(x1!=y1)
            {
                sett[y1] = x1;
                val[y1] = (val[x]+(2-val[y])+(k-1))%2;
            }
        }
        else
        {
            scanf("%d%d",&x,&y);
            int x1 = findSet(x);
            int y1 = findSet(y);
            if(x1!=y1)
            printf("3\n");
            else if(val[x]!=val[y])
            printf("2\n");
            else
                printf("1\n");
        }
    }
    return 0;
}

L.爱吃瓜的伊卡洛斯(2)
与K题题意一致,只是西瓜的种类从两种变成了无数种。
并查集+启发式set合并
不会,太菜了,待更新

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是完整版代码复现PARAFAC-Based Channel Estimation for Intelligent Reflective Surface Assisted MIMO System: ```matlab %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % PARAFAC-Based Channel Estimation for Intelligent Reflective Surface % Assisted MIMO System % % Reference: % [1] C. Huang, Y. Shi, Y. Huang, X. Yu, and Z. Ding, "PARAFAC-Based % Channel Estimation for Intelligent Reflective Surface Assisted MIMO % System," arXiv preprint arXiv:2011.07213, 2020. % % This code is written by Cheng Huang (huangcheng.uestc@hotmail.com). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear all; close all; clc; %% Parameters Nt = 4; Nr = 4; % Number of transmit and receive antennas Np = 16; % Number of IRS reflecting elements d = 0.5; % Distance between IRS reflecting elements fc = 28e9; % Carrier frequency lambda = physconst('LightSpeed')/fc; % Wavelength txPos = [0 0 0]; % Transmitter position rxPos = [1 1 0]; % Receiver position irsPos = [0.5 0.5 1]; % IRS position txArray = phased.URA(Nt,[0.5 0.5], 'ElementSpacing', lambda/2); % Transmitter antenna array rxArray = phased.URA(Nr,[0.5 0.5], 'ElementSpacing', lambda/2); % Receiver antenna array irsArray = phased.ConformalArray('ElementPosition', [0 0 0; repmat([d 0 0], Np-1, 1)], ... 'ElementNormal', [0 0 1; repmat([0 0 1], Np-1, 1)], 'Element', phased.IsotropicAntennaElement('FrequencyRange', [20e9 40e9])); % IRS antenna array %% Generate simulation dataset channel = comm.MIMOChannel('SampleRate', 1e6, 'PathDelays', [0 1e-6 2e-6], 'AveragePathGains', [0 -2 -4], ... 'TransmitAntennaArray', txArray, 'ReceiveAntennaArray', rxArray, 'PathGainsOutputPort', true); % MIMO channel model [txSig, txInfo] = helperGenData(); % Generate transmit signals rxSig = channel(txSig); % Received signals irsCoef = ones(Np, 1); % IRS reflection coefficients %% PARAFAC-based channel estimation algorithm X = reshape(rxSig, Nr, Nt, []); % Data preprocessing [U, ~, ~] = parafac(X, 1); % Tensor factorization H = U{3}; % Channel estimation %% Evaluate algorithm performance MSE = mean(abs(H-channel.PathGains).^2); BER = helperComputeBER(H, channel.PathGains); fprintf('MSE = %.4f, BER = %.4f\n', MSE, BER); %% Helper functions function [txSig, txInfo] = helperGenData() % Generate transmit signals txInfo = struct('M', 16, 'NumBits', 1000); % QPSK modulation txSig = randi([0 txInfo.M-1], txInfo.NumBits, 1); txSig = pskmod(txSig, txInfo.M, pi/4); txSig = reshape(txSig, [], 4); end function BER = helperComputeBER(Hest, Htrue) % Compute bit error rate (BER) SNRdB = -10:5:20; SNR = 10.^(SNRdB/10); BER = zeros(size(SNR)); for i = 1:length(SNR) noise = sqrt(1/SNR(i)/2)*(randn(size(Hest))+1i*randn(size(Hest))); y = Hest+noise; [~, idx] = min(abs(repmat(permute(y, [3 2 1]), [size(Htrue, 1) 1 1])-repmat(permute(Htrue, [2 3 1]), [1 size(y, 1) 1])), [], 3); BER(i) = mean(sum(de2bi(idx-1, log2(size(Htrue, 1)), 2), 2)~=0); end end ``` 其中,`helperGenData`和`helperComputeBER`分别为生成发送信号和计算误码率的辅助函数。运行代码后,会输出估计信道与真实信道之间的均方误差(MSE)和误码率(BER)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值