线段树 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 ...
随机推荐
- std::string::assign函数
string& assign (const string& str); string& assign (const string& str, size_t subpos ...
- EL表达式---自定义函数(转)
EL表达式---自定义函数(转) 有看到一个有趣的应用了,转下来,呵呵!! 1.定义类MyFunction(注意:方法必须为 public static) package com.tgb.jstl; ...
- .NET Core技术研究-主机
前一段时间,和大家分享了 ASP.NET Core技术研究-探秘Host主机启动过程 但是没有深入说明主机的设计.今天整理了一下主机的一些知识,结合先前的博文,完整地介绍一下.NET Core的主机的 ...
- cucumber学习索引
Cucumber(1) —— 环境配置 Cucumber(2)——目录结构以及基本语法 Cucumber(3)——命令以及日志 Cucumber(4)——jenkins的集成
- 爬虫需要登陆怎么办?这份python登陆代码请收下
模拟登陆思路 通过selenium中的webdriver控制浏览器登录目标网站,然后获取模拟登陆需要的Cookie,再利用此Cookie来达到登录的效果.本次我们使用webdriver来驱动火狐浏览器 ...
- C#开发BIMFACE系列33 服务端API之模型对比4:获取模型对比结果
系列目录 [已更新最新开发文章,点击查看详细] 模型对比可以对两个文件/模型进行差异性分析,确定两个文件/模型之间构件的几何和属性差异,包括增加的构件.删除的构件和修改的构件. 模型对应可以用 ...
- kioptrix靶机记录
靶机地址:172.16.1.193 Kali地址:172.16.1.107 首页为Apache测试页,没看到有价值信息 尝试目录扫描: 点击查看: http://172.16.1.193/index. ...
- Upload-Labs 实验操作记录
0x01 安装 下载:https://github.com/c0ny1/upload-labs 环境:简单搭建phpstudy环境即可,记得在upload-labs根目录下创建该文件夹 0x02 文件 ...
- Tidyverse|数据列的分分合合,爱恨情仇
Tidyverse|数据列的分分合合,爱恨情仇 本文首发于“生信补给站”Tidyverse|数据列的分分合合,一分多,多合一 TCGA数据挖掘可做很多分析,前期数据“清洗”费时费力但很需要. 比如基因 ...
- [机器学习实战-Logistic回归]使用Logistic回归预测各种实例
目录 本实验代码已经传到gitee上,请点击查收! 一.实验目的 二.实验内容与设计思想 实验内容 设计思想 三.实验使用环境 四.实验步骤和调试过程 4.1 基于Logistic回归和Sigmoid ...