线段树 C - Connected Components? CodeForces - 920E
这个题目居然可以用线段树写,好震惊,如果不是在线段树专题肯定想不到,但是就算在线段树的专题里面,我也不太会怎么写。
这个题目大意是,给你n m n代表n个点,m代表m条边,然后就是m行,每行两个数字,一个u一个v。
这个意思是u和v不想连,然后问你这个n个点形成了多少个联通块。
思路大概是这样,首先随意枚举一个点,然后直接更新每一个点的值+1,先消除自己的影响,然后对于每一个和它连的点的值都-1
然后查找一个值大于0 的点,再继续循环这个过程,如果找不到了就推出这个循环。
这个复杂度我不太会算。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <stack>
#include <map>
#include <string>
#define inf 0x3f3f3f3f
#define inf64 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 4e5 + 10;
int cnt[maxn * 4], maxs[maxn * 4];
int lazy[maxn * 4]; void push_up(int id)
{
if (maxs[id << 1] < maxs[id << 1 | 1]) {
maxs[id] = maxs[id << 1 | 1];
cnt[id] = cnt[id << 1 | 1];
}
else {
maxs[id] = maxs[id << 1];
cnt[id] = cnt[id << 1];
}
// printf("cnt[%d]=%d cnt[%d]=%d\n", id << 1, cnt[id << 1], id << 1 | 1, cnt[id << 1 | 1]);
// printf("cnt[%d]=%d\n", id, cnt[id]);
} void build(int id,int l,int r)
{
lazy[id] = 0;
if(l==r)
{
cnt[id] = l;
maxs[id] = 0;
return;
}
int mid = (l + r) >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
push_up(id);
} void push_down(int id)
{
//printf("id=%d\n", id);
if (lazy[id] == 0) return;
maxs[id << 1] += lazy[id];
maxs[id << 1 | 1] += lazy[id];
lazy[id << 1] += lazy[id];
lazy[id << 1 | 1] += lazy[id];
lazy[id] = 0;
} void update(int id,int l,int r,const int x,const int y,int val)
{
// printf("id=%d l=%d r=%d x=%d y=%d\n", id, l, r, x, y);
if(x<=l&&y>=r)
{
maxs[id] += val;
lazy[id] += val;
return;
}
push_down(id);
int mid = (l + r) >> 1;
if (x <= mid) update(id << 1, l, mid, x, y, val);
if (y > mid) update(id << 1 | 1, mid + 1, r, x, y, val);
push_up(id);
} struct node
{
int v, nxt;
node(int v=0,int nxt=0):v(v),nxt(nxt){}
}ex[maxn];
int head[maxn], tot = 0, num;
void init()
{
memset(head, -1, sizeof(head));
tot = 0, num = 0;
} void add(int u,int v)
{
ex[tot] = node(v, head[u]);
head[u] = tot++;
ex[tot] = node(u, head[v]);
head[v] = tot++;
}
int a[maxn];
bool vis[maxn];
int n, m; int dfs(int x)
{
int res = 0;
build(1, 1, n);
while(1)
{
vis[x] = 1;
res++;
update(1, 1, n, 1, n, 1);
update(1, 1, n, x, x, -inf);
for (int i = head[x]; i != -1; i = ex[i].nxt)
{
int v = ex[i].v;
update(1, 1, n, v, v, -1);
}
// printf("\n\n");
if (maxs[1] <= 0) break;
x = cnt[1];
}
return res;
} int main()
{
init();
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
add(u, v);
}
for(int i=1;i<=n;i++)
{
if (vis[i]) continue;
a[num++] = dfs(i);
}
sort(a, a + num);
printf("%d\n", num);
for (int i = 0; i < num; i++) printf("%d ", a[i]);
return 0;
}
线段树 C - Connected Components? CodeForces - 920E的更多相关文章
- Connected Components? Codeforces - 920E || 洛谷 P3452 &&bzoj1098 [POI2007]BIU-Offices
https://codeforces.com/contest/920/problem/E https://www.luogu.org/problemnew/show/P3452 https://www ...
- Connected Components? CodeForces - 920E (bfs)
大意:给定无向图, 求补图的连通块数 bfs模拟即可, 这里用了map存图, set维护未划分的点集, 复杂度$O(nlog^2n)$, 用链表的话可以$O(n)$ #include <iost ...
- 线段树+矩阵快速幂 Codeforces Round #373 (Div. 2) E
http://codeforces.com/contest/719/problem/E 题目大意:给你一串数组a,a[i]表示第i个斐波那契数列,有如下操作 ①对[l,r]区间+一个val ②求出[l ...
- 数据结构(线段树):Educational Codeforces Round 6 620E. New Year Tree
E. New Year Tree time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...
- codeforces 1217E E. Sum Queries? (线段树
codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...
- 线段树详解 (原理,实现与应用)(转载自:http://blog.csdn.net/zearot/article/details/48299459)
原文地址:http://blog.csdn.net/zearot/article/details/48299459(如有侵权,请联系博主,立即删除.) 线段树详解 By 岩之痕 目录: 一:综述 ...
- Codeforces 1270H - Number of Components(线段树)
Codeforces 题目传送门 & 洛谷题目传送门 首先需发现一个性质,那就是每一个连通块所对应的是一个区间.换句话说 \(\forall l<r\),若 \(l,r\) 在同一连通块 ...
- Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)
题目链接:http://codeforces.com/contest/522/problem/D 题目大意: 给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...
- Educational Codeforces Round 6 E. New Year Tree dfs+线段树
题目链接:http://codeforces.com/contest/620/problem/E E. New Year Tree time limit per test 3 seconds memo ...
随机推荐
- MySQL锁---InnoDB行锁需要注意的细节
前言 换了工作之后,接近半年没有发博客了(一直加班),emmmm.....今天好不容易有时间,记录下工作中遇到的一些问题,接下来应该重拾知识点了.因为新公司工作中MySQL库经常出现查询慢,锁等待,节 ...
- 【three.js第四课】自定义材料、贴图。
1.先去下载6张不同的图片素材放到项目中. 2.在[three.js第三课]的代码基础上添加自定义的材料 //自定义材料 cubeMaterial 数组 //map:用于加载图片,THREE.Text ...
- 从3dMax导出供threeJS使用的带动作模型与加载
评论区发现的建议,最近没空测试,先贴这 还有好多人说找不到插件的 https://pan.baidu.com/s/1Q5g0... 密码:b43e . 应该是他们现在只是维护blender,只有这个的 ...
- Map使用foreach遍历方式,Map获取第一个键值
List<Map<String, Object>> mapList = new ArrayList<>(); for (Map.Entry<String,O ...
- 如何使用python在短时间内寻找完数
完数:完全数(Perfect number),又称完美数或完备数,是一些特殊的自然数.它所有的真因子(即除了自身以外的约数)的和(即因子函数),恰好等于它本身.如果一个数恰好等于它的因子之和,则称该数 ...
- CSS 中你应该了解的 BFC
我们常说的文档流其实分为定位流.浮动流和普通流三种.而普通流其实就是指BFC中的FC.FC是formatting context的首字母缩写,直译过来是格式化上下文,它是页面中的一块渲染区域,有一套渲 ...
- Joomla 3.4.6 RCE 分析
Joomla 3.4.6 RCE 漏洞分析,首发先知社区: https://xz.aliyun.com/t/6522 漏洞环境及利用 Joomla 3.4.6 : https://downloads. ...
- java第八周课后作业
1.系统小练习 package homework; import java.util.Random; import java.util.Scanner; public class Menu { pub ...
- MVC-基础01
MVC体系结构将应用程序分成三个主要组件:模型(Model).视图(View).和控制器(Controller).在ASP.NET MVC应用程序中,数据操控的逻辑包含在Models文件夹下,数据的展 ...
- java中String StringBuilder StringBuffer比较和效率(性能)测试
string stringbuilder stringbuffer三者的区别 从JDK源码看,String.StringBuilder.StringBuffer都是存放在char[] 数组字符串. 简 ...