G - Ice_cream's world I (并查集)
InputIn the case, first two integers N, M (N<=1000, M<=10000) is represent the number of watchtower and the number of wall. The watchtower numbered from 0 to N-1. Next following M lines, every line contain two integers A, B mean between A and B has a wall(A and B are distinct). Terminate by end of file. OutputOutput the maximum number of ACMers who will be awarded.
One answer one line.
Sample Input
8 10
0 1
1 2
1 3
2 4
3 4
0 5
5 6
6 7
3 6
4 7
Sample Output
3
AC代码
#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; const int maxn=; int n,m;
int father[maxn]; void makeSet(){
for(int i=;i<n;i++){
father[i]=i;
}
} int findSet(int x){
if(x!=father[x]){
father[x]=findSet(father[x]);
}
return father[x];
} int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d%d",&n,&m)){
makeSet();
int ans=;
int a,b;
while(m--){
scanf("%d%d",&a,&b);
int fx=findSet(a);
int fy=findSet(b);
if(fx==fy)
ans++;
else
father[fx]=fy;
}
printf("%d\n",ans);
}
return ;
}
这有一个并查集详解,讲得很好
G - Ice_cream's world I (并查集)的更多相关文章
- G - 小希的迷宫(并查集)
1今天准备复习三道以前做过的题呢,结果只看了一道,也因为交题的时候没把测试时候为了方便而改的数字改过来而wrong answer,浪费了好长时间,啊啊啊~~ 不过这道题应该是掌握了,嘿嘿…… Desc ...
- BZOJ4602:[SDOI2016]齿轮(并查集)
Description 现有一个传动系统,包含了N个组合齿轮和M个链条.每一个链条连接了两个组合齿轮u和v,并提供了一个传动比x : y.即如果只考虑这两个组合齿轮,编号为u的齿轮转动x圈,编号为v ...
- hdu1325 Is It A Tree? 基础并查集
#include <stdio.h> #include <string.h> ], g[]; int find(int x) //并查集的查找,找到共同的父亲 { if (f[ ...
- Educational Codeforces Round 51 (Rated for Div. 2) G. Distinctification(线段树合并 + 并查集)
题意 给出一个长度为 \(n\) 序列 , 每个位置有 \(a_i , b_i\) 两个参数 , \(b_i\) 互不相同 ,你可以进行任意次如下的两种操作 : 若存在 \(j \not = i\) ...
- Ice_cream's world I(并查集成环)
Problem Description ice_cream's world is a rich country, it has many fertile lands. Today, the queen ...
- Codeforces Gym 101194G Pandaria (2016 ACM-ICPC EC-Final G题, 并查集 + 线段树合并)
题目链接 2016 ACM-ICPC EC-Final Problem G 题意 给定一个无向图.每个点有一种颜色. 现在给定$q$个询问,每次询问$x$和$w$,求所有能通过边权值不超过$w$的 ...
- Codeforces Round #582 (Div. 3)-G. Path Queries-并查集
Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...
- luogu4269 Snow Boots G (并查集)
对于某个靴子,如果0代表某个格能走,1代表不能走,那么只要连续的1的个数的最大值>=靴子的步长,那这个靴子就不能用. 那么只要对靴子和格子都按深度排个序,然后从大到小来扫一遍(靴子越来越浅,能走 ...
- [USACO18FEB] Snow Boots G (离线+并查集)
题目大意:略 网上各种神仙做法,本蒟蒻只想了一个离线+并查集的做法 对所有靴子按最大能踩的深度从大到小排序,再把所有地砖按照积雪深度从大到小排序 一个小贪心思想,我们肯定是在 连续不能踩的地砖之前 的 ...
随机推荐
- How do I list subversion repository's ignore settings
If it is Windows and you are using TortoiseSVN, then right-click on a folder of the working copy, go ...
- vc通过进程名返回进程id
std::string WcharToChar(const wchar_t* wp, size_t m_encode = CP_ACP) { std::string str; , wp, wcslen ...
- oracle ROW_NUMBER用法
Oracle中row_number().rank().dense_rank() 的区别 row_number的用途非常广泛,排序最好用它,它会为查询出来的每一行记录生成一个序号,依次排序且不会重复 使 ...
- 迷你MVVM框架 avalonjs 1.3.1发布
avalon1.3.1发布. interpolate支持注释节点做定界符,avalon.config({interpolate:["<!--","-->&qu ...
- SpringMVC 中xml 配置多数据源
1,配置jdbc.properties jdbc.driver_one=... jdbc.url_one=..... jdbc.username_one=... jdbc.password_one=. ...
- Eclipse自动补全修改
一.前言 之前敲代码用的是文本工具sublime,转到Eclipse之后发现补全功能特别不方便,所以想根据自己的情况进行调整,具体有两点: 输入某些语句的前几个字母就能自动提示相关的完整语句 用tab ...
- jquery text() html() val()
test text() 返回 test html() 返回 test val() 没值, val() 适用于有value属性的元素, 例如: input
- golang之切片与排序
1.排序与查找操作 排序操作在sort包中,sort.Ints对整数进行排序,sort.Strings对字符串进行排序,sort.Float64对浮点数进行排序 package main import ...
- linux网络编程模型
1.编程模型 Linux网络编程模型是基于socket的编程模型
- 如何用Python实现常见机器学习算法-4
四.SVM支持向量机 1.代价函数 在逻辑回归中,我们的代价为: 其中: 如图所示,如果y=1,cost代价函数如图所示 我们想让,即z>>0,这样的话cost代价函数才会趋于最小(这正是 ...