D. Cow and Snacks 并查集

D. Cow and Snacks

题意:有n种小吃,m个人,每个人有两种喜欢的小吃,当一个人遇到两种自己都喜欢的小吃,可以都吃掉,问在最优的吃小吃顺序下,不能吃到自己喜欢的小吃的人数最少是多少?

题解:把n种小吃当作n个点,m个人当作m条边,每个连通图里面第一个吃的人,一定是可以吃两种自己喜欢的小吃。每次判断这条边是否在已有的联通图里面,对已经在连通图里面的边,是一定不能吃到小吃,若不在连通图里面,则一定可以吃到小吃,用cnt统计可以吃到小吃的人数,最后m-cnt就是答案

#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
using namespace std;
int p[1000005], r[1000005];
int n,t=0;
void init()//初始化集合,每个元素的老板都是自己
{
    for (int i = 1; i <= n; i++)
    {
        p[i] = i;
    }
}

int find(int x)//查找元素x的老板是谁
{
    if (x == p[x])
        return x;
    else
        return p[x] = find(p[x]);
}

void join(int x, int y)//合并两个集合
{
    int xRoot = find(x);
    int yRoot = find(y);

    if (xRoot == yRoot) //老板相同,不合并
        return;
    //cnt=cnt-1;
    if (r[xRoot] < r[yRoot]) //r[i]是元素i所在树的高度,矮树的根节点认高树的根节点做老板
        p[xRoot] = yRoot;
    else if (r[xRoot] > r[yRoot])
        p[yRoot] = xRoot;
    else
    {
        p[yRoot] = xRoot;//树高相同,做老板的树高度要加一
        r[xRoot]++;
    }
}

bool sameRoot(int x, int y)//查询两个元素的老板是否相同
{
    return find(x) == find(y);
}
//这里也可以用cnt求不同子集个数,初始化cnt=n,每加入一条边,cnt=cnt-1;
int main()
{
    ios::sync_with_stdio(false);
    int m,cnt=0;
    cin>>n>>m;
    init();
    for(int i=0;i<m;i++)
    {
        int x,y;
        cin>>x>>y;
        if(!sameRoot(x,y))
        {
            join(x,y);
            cnt++;
        }
    }
    cout<<m-cnt<<endl;
    return 0;
}

原文地址:https://www.cnblogs.com/-citywall123/p/11587394.html

时间: 2024-07-30 22:50:47

D. Cow and Snacks 并查集的相关文章

【bzoj1604】[Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 并查集+Treap/STL-set

题目描述 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l≤Xi,Yi≤[1..10^9]:Xi,Yi∈整数.当满足下列两个条件之一,两只奶牛i和j是属于同一个群的: 1.两只奶牛的曼哈顿距离不超过C(1≤C≤10^9),即lXi - xil+IYi - Yil≤C. 2.两只奶牛有共同的邻居.即,存在一只奶牛k,使i与k,j与k均同属一个群. 给出奶牛们的位置,请计算

【USACO 2008 Open Gold】 3.Cow Neighborhoods 平衡树、并查集

题解: 首先曼哈顿距离有些不好维护,但是它可以转化: 一个点本来的坐标是(x,y),那么可以转化成(x+y,x-y) 这样就人为构造出一种性质:1.2两点曼哈顿距离=max(|x1-x2|,|y1-y2|); 这样我们就可以排序单调搞掉一维,然后另一维只需要求前驱后继到该点的距离 满足则加并查集. 这个过程可以用权值线段树,也可以用平衡树.但是权值线段树还需要离散化,反而代码多了. 代码: #include <cmath> #include <cstdio> #include &l

G - Brain Network (easy)(并查集水题)

G - Brain Network (easy) Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u CodeForces 690C1 Description One particularly well-known fact about zombies is that they move and think terribly slowly. While we still don't know

POJ 3660Cow Contest(并查集+拓扑排序)

Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7567   Accepted: 4206 Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all know, some cows code better than others

CodeForces 745C Hongcow Builds A Nation 并查集

题意: 给了你n个城市 m条边 k个政府 每个政府管辖的区域内不能和其他政府的区域有相连 即政府之间不存在路径 问你在维护这种关系的同时 最多再加多少条边 思路: 先找出来每个联通块 再找出来没有归属的孤立的点 把他们都放到最大的联通块里 然后每个联通块之间的点两两连边是n*(n-1)/2条边 最后算出来的ans-m就好了 (看别人的博客学了一个max_element 1 #include<bits/stdc++.h> 2 #define cl(a,b) memset(a,b,sizeof(a

并查集(个人模版)

并查集: 1 int find(int a) 2 { 3 int r=a; 4 while(f[r]!=r) 5 r=f[r]; 6 int i=a; 7 int j; 8 while(i!=r) 9 { 10 j=f[i]; 11 f[i]=r; 12 i=j; 13 } 14 return r; 15 } 16 int merge(int a,int b) 17 { 18 int A,B; 19 A=find(a); 20 B=find(b); 21 if(A!=B) 22 { 23 f[B

并查集应用

题目描述: One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of all the phone calls

【bzoj3674】 可持久化并查集加强版

http://www.lydsy.com/JudgeOnline/problem.php?id=3674 (题目链接) 题意 维护并查集3个操作:合并:回到完成第k个操作后的状态:查询. Solution 其实就是用主席树的叶子节点维护并查集的可持久化数组fa[]. 细节 终于认识到了按秩合并的强大,单纯写个路径压缩Re飞,写了路径压缩+按秩合并比单纯的按秩合并每快多少→_→ 代码 // bzoj3674 #include<algorithm> #include<iostream>

BZOJ1015[JSOI2008]星球大战starwar[并查集]

1015: [JSOI2008]星球大战starwar Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 5253  Solved: 2395[Submit][Status][Discuss] Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过特殊的以太隧道互相直接或间接地连接. 但好景不长,很快帝国又重