codeforces 731C Socks

给你n,m,k
n袜子的种类
m天数
k你可以改的颜色范围
再给你n种袜子的颜色
再告诉你m天,我每天穿了哪种袜子(两只)
你必须用最少的次数去修改某些袜子的颜色,使得每天,两种袜子的颜色一样。

input
3 2 3
1 2 3
1 2
2 3
output
2
input
3 2 2
1 1 2
1 2
2 1
output
0

并查集把必须相等的种类合并起来,形成多个集合。
在每一个集合内,我肯定改成颜色最多的那种袜子(在集合内最多)
用边表把集合内的元素与根连起来,暴力累加。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define ll long long
using namespace std;
int n,m,k,x,y,u,v,tot,ans,Max;
int f[200005],c[200005],s[200005],have[200005];
int head[200005],Next[200005],to[200005];
int get(int x)
{
    if(f[x]==x) return x;else return f[x]=get(f[x]);
}
void add(int x,int y)
{
    tot++;
    Next[tot]=head[x];
    to[tot]=y;
    head[x]=tot;
}
int main()
{
    cin>>n>>m>>k;
    for(int i=1;i<=n;i++) scanf("%d",&c[i]);
    for(int i=1;i<=n;i++) f[i]=i,s[i]=1,head[i]=-1;
    for(int i=1;i<=m;i++) 
    {
        scanf("%d%d",&x,&y);
        u=get(x);
        v=get(y);
        if(v!=u) 
        {
            f[v]=u;
            s[u]=s[u]+s[v];
            s[v]=0;
        }
    }
    for(int i=1;i<=n;i++) 
    {
        f[i]=get(i);
        add(f[i],i);
    }
    for(int i=1;i<=n;i++) 
    if(f[i]==i) 
    {
        Max=0;
        for(int j=head[i];j!=-1;j=Next[j]) 
        {
            have[c[to[j]]]++;
            Max=max(Max,have[c[to[j]]]);
        }
        ans=ans+s[i]-Max;
        for(int j=head[i];j!=-1;j=Next[j]) have[c[to[j]]]--;
    }
    cout<<ans;  
    return 0;
}
### Codeforces Problem 1332C Explanation The provided references pertain specifically to problem 742B on Codeforces rather than problem 1332C. For an accurate understanding and solution approach for problem 1332C, it's essential to refer directly to its description and constraints. However, based on general knowledge regarding competitive programming problems found on platforms like Codeforces: Problem 1332C typically involves algorithmic challenges that require efficient data structures or algorithms such as dynamic programming, graph theory, greedy algorithms, etc., depending upon the specific nature of the task described within this particular question[^6]. To provide a detailed explanation or demonstration concerning **Codeforces problem 1332C**, one would need direct access to the exact statement associated with this challenge since different tasks demand tailored strategies addressing their unique requirements. For obtaining precise details about problem 1332C including any sample inputs/outputs along with explanations or solutions, visiting the official Codeforces website and navigating to contest number 1332 followed by examining section C is recommended. ```python # Example pseudo-code structure often seen in solving competitive coding questions. def solve_problem_1332C(input_data): # Placeholder function body; actual logic depends heavily on the specifics of problem 1332C. processed_result = process_input(input_data) final_answer = compute_solution(processed_result) return final_answer input_example = "Example Input" print(solve_problem_1332C(input_example)) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值