【LeetCode Weekly Contest 26 Q3】Friend Circles
【题目链接】:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/
【题意】
告诉你任意两个人是不是朋友.
问你最后有多少个连通块;
【题解】
裸并查集。
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 250;
int f[N];
bool bo[N];
int ff(int x)
{
if (f[x] == x) return x;
else
return f[x] = ff(f[x]);
}
class Solution {
public:
int findCircleNum(vector<vector<int>>& M) {
memset(bo, false, sizeof bo);
int n = M.size();
rep1(i, 0, n - 1)
f[i] = i;
rep1(i,0,n-1)
rep1(j, 0, n - 1)
if (M[i][j])
{
int r1 = ff(i), r2 = ff(j);
if (r1 != r2)
{
f[r1] = r2;
}
}
int cnt = 0;
rep1(i, 0, n - 1)
{
int x = ff(i);
if (!bo[x])
{
bo[x] = true;
cnt++;
}
}
return cnt;
}
};
【LeetCode Weekly Contest 26 Q3】Friend Circles的更多相关文章
- 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...
- 【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...
- 【LeetCode Weekly Contest 26 Q1】Longest Uncommon Subsequence I
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...
- LeetCode Weekly Contest 26
写的有点晚了. 我每次都是先看一下这里http://bookshadow.com/leetcode/的思路,然后再开始写我自己的. 1. 521. Longest Uncommon Subsequen ...
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- LeetCode Weekly Contest
链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...
随机推荐
- open_basedir restriction in effect,解决php引入文件权限问题 lnmp
1.配置了虚拟域名 vim /usr/local/nginx/conf/vhost/siemens.conf server { listen 80; #listen [::]:80 default_s ...
- mst
https://www.zybuluo.com/ysner/note/1245941 题面 给一个\(n\)点完全图,点权均小于\(2^m\).定义边权等于两端点点权的与和(即\(a_i\&b ...
- C++11 function使用
function是一组函数对象包装类的模板,实现了一个泛型的回调机制. 引入头文件 #include <functional>using namespace std;using names ...
- CSS盒子居中的常用的几种方法
第一种: 用css的position属性 <style type="text/css"> .div1 { width: 100px; height: 100px; bo ...
- E20170925-hm
arc n. 综合症状; 弧(度); 天穹; 电弧,弧光.; vi. 形成拱状物; 循弧线行进; wrap vt. 包; 缠绕; 用…包裹(或包扎.覆盖等); 掩护; n. ...
- js模拟复制
现在浏览器种类也越来越多,诸如 IE.Firefox.Chrome.Safari等等,因此现在要实现一个js复制内容到剪贴板的小功能就不是一件那么容易的事了. 一.实现点击按钮,复制文本框中的的内 ...
- Treap(模板)
人生第一次平衡树,Treap板子 #include<iostream> #include<cstdio> #include<cstring> #include< ...
- [Luogu 1966] noip13 火柴排队
[Luogu 1966] noip13 火柴排队 Problem 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度. 现在将每盒中的火柴各自排成一列, 同一列火柴的高度互不相同, 两列火柴之 ...
- ACM_Repeating Characters
Repeating Characters Time Limit: 2000/1000ms (Java/Others) Problem Description: For this problem, yo ...
- Android开发中的SQLite事务处理,即beginTransaction()方法
使用SQLiteDatabase的beginTransaction()方法可以开启一个事务,程序执行到endTransaction() 方法时会检查事务的标志是否为成功,如果程序执行到endTrans ...