首页 > 其他分享 >ABC 293 ABCD(并查集)

ABC 293 ABCD(并查集)

时间:2023-03-13 19:44:47浏览次数:54  
标签:ABCD typedef ABC const int LL 查集 cin long

A - Swap Odd and Even

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18,MINN=-1e18;
const LL N=1e6+10,M=2023;
const LL mod=998244353;
const double PI=3.1415926535;
#define endl '\n'
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    int T=1;
    //cin>>T;
    while(T--)
    {
        string s;
        cin>>s;
        for(int i=0;i<s.size();i+=2)
        {
            cout<<s[i+1]<<s[i];
        }
    }
    return 0;
}

B - Call the ID Number

难在读题,题目读懂了就没问题

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18,MINN=-1e18;
const LL N=1e6+10,M=2023;
const LL mod=998244353;
const double PI=3.1415926535;
#define endl '\n'
LL a[N],sum[N];
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    int T=1;
    //cin>>T;
    while(T--)
    {
        LL n;
        cin>>n;
        map<LL,LL> mp;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            if(mp[i]!=0) continue;
            else if(mp[i]==0&&mp[a[i]]==0) mp[a[i]]++;
        }
        vector<LL> v;
        for(int i=1;i<=n;i++)
        {
            if(mp[i]==0) //cout<<i<<" ";
                v.push_back(i);
        }
        cout<<v.size()<<endl;
        for(int i=0;i<v.size();i++)
            cout<<v[i]<<" ";
        cout<<endl;
    }
    return 0;
}

C - Make Takahashi Happy

直接爆搜

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18,MINN=-1e18;
const LL N=1e6+10,M=2023;
const LL mod=998244353;
const double PI=3.1415926535;
#define endl '\n'
LL n,m,a[M][M];
LL sum=0;
LL dx[]={-1,0,0,1},dy[]={0,1,-1,0};
vector<LL> v;
map<LL,LL> mp;
void dfs(LL x,LL y)
{
    if(x==n&&y==m)
    {
        bool flag=true;
        mp.clear();
        for(int i=0;i<v.size();i++)
        {
            mp[v[i]]++;
            if(mp[v[i]]>=2)
            {
                flag=false;
                break;
            }
        }
        if(flag==true) sum++;
        return ;
    }
    if(x+1<=n)
    {
        v.push_back(a[x+1][y]);
        dfs(x+1,y);
        v.pop_back();
    }
    if(y+1<=m)
    {
        v.push_back(a[x][y+1]);
        dfs(x,y+1);
        v.pop_back();
    }
}
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    int T=1;
    //cin>>T;
    while(T--)
    {
        cin>>n>>m;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                cin>>a[i][j];
            }
        }
        v.push_back(a[1][1]);
        dfs(1,1);
        cout<<sum<<endl;
    }
    return 0;
}

D - Tying Rope

https://atcoder.jp/contests/abc293/tasks/abc293_d

题目大意:

N根绳子从1到N。每根绳子的一端被涂成红色,另一端被涂成蓝色。

M次系绳子的操作。

在第i个操作中,绳子A的某个颜色的一端和绳子B的某个颜色的一端相连。(对于每根绳子,颜色相同的一端不会被系多次)

在所有运算之后,找出形成循环的相连绳子的组数,以及不形成循环的绳子的组数。
Sample Input 1  
5 3
3 R 5 B
5 R 3 B
4 R 2 B
Sample Output 1  
1 2

我自己的写法没有加上sz,卡了两天(傻狗爆哭

学一下佬儿的做法,一下子过了hh(:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18,MINN=-1e18;
const LL N=1e6+10,M=2023;
const LL mod=998244353;
const double PI=3.1415926535;
#define endl '\n'
LL n,m;
LL a[N],c[N];
char b[N],d[N];
LL father[N],sz[N];
LL find(LL x)
{
    if(father[x]!=x) father[x]=find(father[x]);
    return father[x];
}
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    int T=1;
    //cin>>T;
    while(T--)
    {
        cin>>n>>m;
        for(int i=1;i<=m;i++)
        {
            cin>>a[i]>>b[i]>>c[i]>>d[i];
        }
        for(int i=1;i<=n;i++)
        {
            father[i]=i;
            sz[i]=1;
        }
        for(int i=1;i<=m;i++)
        {
            if(a[i]>c[i]) swap(a[i],c[i]),swap(b[i],d[i]);
            LL fai=find(a[i]);
            LL fci=find(c[i]);
            if(fai==fci) ;
            else
            {
                //从前往后
                sz[fai]+=sz[fci];
                father[fci]=fai;
            }
        }
        map<LL,LL> mp;
        for(int i=1;i<=m;i++)//m条边中最早的那根线的位置在哪里
        {
            LL fai=find(a[i]);
            mp[fai]++;//标注位置
        }
        LL sum1=0,sum2=0;
        for(int i=1;i<=n;i++)
        {
            if(find(i)==i)//根节点
            {
                //如果以它为根节点的数量和我们直接添加边长数量是一样的
                if(mp[i]==sz[find(i)]) sum1++;//环++
                else sum2++;
            }
        }
        cout<<sum1<<" "<<sum2<<endl;
    }
    return 0;
}

标签:ABCD,typedef,ABC,const,int,LL,查集,cin,long
From: https://www.cnblogs.com/Vivian-0918/p/17209197.html

相关文章

  • ATABC293D Tying Rope
    ATABC293DTyingRope题意有\(N\)根一端涂成红色,另一端涂成蓝色的绳子,现进行\(M\)次操作,第\(i\)次操作给出两个整数\(A_i\),\(C_i\)与两个字符\(B_i\),\(D_i\),表......
  • 题解 ABC293D【Tying Rope】
    颜色是不好处理的,我们不妨不区分绳子的两个端点,将每条绳子作为一个节点,每条边直接将两个节点连接起来。每个绳子的端点本质上是保证了每个点的度数不超过\(2\),也就是说图......
  • 题解 ABC293E【Geometric Progression】
    由于模数不一定是大质数,我们不能直接套等比数列求和公式。换一种思路,数列\(\langle1,A,A^2,\cdots,A^{X-1}\rangle\)可以看做线性递推,因此设计矩阵:\[\boldsymbolT=\b......
  • 题解 ABC293F【Zero or One】
    我们可以暴力检查进制数不超过\(B\)的是否符合要求;然后对于进制数大于\(B\)的,位数不超过\(\log_BN\),可以暴力枚举每一位的值然后二分进制数检查。代码中\(B=10^3\)......
  • 题解 ABC293G【Triple Index】
    莫队板子。类似于小B的询问,在移动指针过程中,维护每个数出现次数\(cnt_i\),同时维护\(\sum\binom{cnt_i}{3}\)即可。取序列分块块长\(B=\frac{n}{\sqrt{m}}\),有最优......
  • [ABC293E] Geometric Progression 题解
    [ABC293E]GeometricProgression题解神中神数论题目描述给定整数\(A,X,M\),求\[\sum_{i=0}^{X-1}A^i\bmodM\]\(1\leA,M\le10^9\)\(1\leX\le10^......
  • 并查集一
    并查集一当我们需要快速判断两个元素是否归属于同一个集合或者将两个集合合并时,就会使用并查集 #include<iostream>usingnamespacestd;constintN=1e5+1......
  • [蓝桥杯 2019 省 A] 修改数组 ———并查集练习(大学复健)
    本题拿到第一反应桶排序思想似乎可以水过,但是很明显出问题了。#include<bits/stdc++.h>usingnamespacestd;intN;inlineintkd(){ intx=0,f=1;charch=getchar......
  • ABC292F题解
    首先先钦定\(a\leb\),否则交换一下就行。方法1:二分答案。容易发现,答案至少为\(a\),并且用左下角为一个顶点一定不会更劣,并且另外两个点一定都在线段上(否则可以调整)。我们......
  • 毕业学生就业数据可视化平台-NABCD
    您好,各位领导/投资人/合作伙伴。我很高兴今天能够向大家介绍我们的全新产品——毕业学生就业数据可视化平台。我们的目标用户是大学和高校的毕业生、职业规划师和招聘人员......