codeforces731——C. Socks(并查集)

简介: codeforces731——C. Socks(并查集)

原题链接

题意:

有n只袜子,k种颜色,在m天中,问最少修改几只袜子的颜色,可以使每天穿的袜子左右两只都同颜色。

思路:

将在一天里穿的放到一个连通块里,染成当前连通块最多的一种颜色。并查集或dfs维护。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll>PLL;
typedef pair<int,int>PII;
typedef pair<double,double>PDD;
#define I_int ll
inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
char F[200];
inline void out(I_int x)
{
    if (x == 0) return (void) (putchar('0'));
    I_int tmp = x > 0 ? x : -x;
    if (x < 0) putchar('-');
    int cnt = 0;
    while (tmp > 0)
    {
        F[cnt++] = tmp % 10 + '0';
        tmp /= 10;
    }
    while (cnt > 0) putchar(F[--cnt]);
    //cout<<" ";
}
ll ksm(ll a,ll b,ll p)
{
    ll res=1;
    while(b)
    {
        if(b&1)res=res*a%p;
        a=a*a%p;
        b>>=1;
    }
    return res;
}
const ll inf = 0x3f3f3f3f3f3f3f3f;
const int maxn=2e5+100,mod=1e8;
const double PI = atan(1.0)*4;
const double eps=1e-6;
int root[maxn];
int n,m,k,c[maxn];
vector<int>v[maxn];
int Find(int x){
    if(x!=root[x]) root[x]=Find(root[x]);
    return root[x];
}
void Union(int u,int v){
    u=Find(u),v=Find(v);
    if(u!=v)
        root[v]=u;
}
int main(){
    n=read(),m=read(),k=read();
    for(int i=1;i<=n;i++) c[i]=read(),root[i]=i;
    while(m--){
        int l=read(),r=read();
        Union(l,r);
    }
    for(int i=1;i<=n;i++)
        v[Find(i)].push_back(i);
    int cnt=0;
    for(int i=1;i<=n;i++)
        if(i==Find(i)){
            int pos=0,maxx=0;
            map<int,int>mp;
            for(auto t:v[i]){
                mp[c[t]]++;
                maxx=max(maxx,mp[c[t]]);
            }
            cnt+=v[i].size()-maxx;
        }
    out(cnt);
    return 0;
}
目录
相关文章
|
8月前
codeforces 272C. Dima and Staircase(线段树)
题目很长,看加猜加谷歌翻译才看懂了题目。每级台阶的宽都是1,但高不同,并且告诉你了,然后给你m个箱子,长和宽都告诉你,把箱子靠左放,求箱子的底部有多高。
19 0
AtCoder Beginner Contest 216 G - 01Sequence (并查集 贪心 树状数组 差分约束)
AtCoder Beginner Contest 216 G - 01Sequence (并查集 贪心 树状数组 差分约束)
118 0
codeforces1253——D. Harmonious Graph(并查集)
codeforces1253——D. Harmonious Graph(并查集)
72 0
|
算法 C语言 C++
【牛客-算法】NC38 螺旋矩阵
【牛客-算法】NC38 螺旋矩阵
81 0
【牛客-算法】NC38 螺旋矩阵
|
算法 C++
【牛客-算法】NC52 有效括号序列
【牛客-算法】NC52 有效括号序列
141 0
【牛客-算法】NC52 有效括号序列
|
算法
【牛客刷题-算法】NC16 对称的二叉树
【牛客刷题-算法】NC16 对称的二叉树
72 0
【牛客刷题-算法】NC16 对称的二叉树
|
机器学习/深度学习 算法 C语言
【牛客-算法】NC55 最长公共前缀
【牛客-算法】NC55 最长公共前缀
97 0
【牛客-算法】NC55 最长公共前缀
|
机器学习/深度学习 人工智能 BI
codeforces-1242-B 0-1 MST
B. 0-1 MST time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output Ujan has a lot of useless stuff in his drawers, a considerable part of which are his math notebooks: it is time to sort them out.
145 0
codeforces-1242-B 0-1 MST
|
算法
51Nod 1632 B君的连通(递归,快速幂)
1632 B君的连通 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 B国拥有n个城市,其交通系统呈树状结构,即任意两个城市存在且仅存在一条交通线将其连接。
1042 0

热门文章

最新文章