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个箱子,长和宽都告诉你,把箱子靠左放,求箱子的底部有多高。
22 0
|
4天前
【洛谷 P1219】[USACO1.5]八皇后 Checker Challenge 题解(深度优先搜索+回溯法)
**USACO1.5八皇后挑战**是关于在$n\times n$棋盘上放置棋子的,确保每行、每列及两条主对角线上各有一个且仅有一个棋子。给定$6$作为输入,输出前$3$个解及解的总数。例如,对于$6\times6$棋盘,正确输出应包括解的序列和总数。代码使用DFS解决,通过跟踪对角线占用状态避免冲突。当找到所有解时,显示前三个并计数。样例输入$6$产生输出为解的前三个排列和总数$4$。
10 0
|
7月前
hdu 1052 Tian Ji -- The Horse Racing【田忌赛马】(贪心)
hdu 1052 Tian Ji -- The Horse Racing【田忌赛马】(贪心)
33 0
AtCoder Beginner Contest 216 G - 01Sequence (并查集 贪心 树状数组 差分约束)
AtCoder Beginner Contest 216 G - 01Sequence (并查集 贪心 树状数组 差分约束)
122 0
|
人工智能 vr&ar Perl
codeforces1509 C. The Sports Festival (区间DP)
codeforces1509 C. The Sports Festival (区间DP)
93 0
codeforces1253——D. Harmonious Graph(并查集)
codeforces1253——D. Harmonious Graph(并查集)
73 0
|
机器学习/深度学习 人工智能 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.
146 0
codeforces-1242-B 0-1 MST
|
人工智能
Codeforces-1260-E. Tournament贪心
题意: 有n个人,第i个人有力量值i,这n个人中,每次对局两两之间进行solo,如果说一个人的力量之大于他的对手,这个人是赢的,赢得比赛的选手会进入下一轮比赛中。 然而里面有一个人为你的朋友,他或许并不是里面最强的人,要想让朋友成为冠军,就需要对必要的人进行贿赂,求出最少需要花费多少才能使得朋友成为冠军。在每次对局中,都可以进行任意的对局安排,对局安排只取决于自己
80 0