bzoj 3060[Poi2012]Tour de Byteotia 贪心+生成树
Description
给定一个n个点m条边的无向图,问最少删掉多少条边能使得编号小于等于k的点都不在环上。
Analysis
包含关键点的环中
包含从关键点连出的两条边
考虑我们删边删哪些边更优
根据贪心
我们会删与关键点相连的边
一直删我们发现不会删掉不与关键点相连的边
Solution
于是我们先把边顶点都大于k的先连起来
相当于合并了一些点
在新的图里,每个连通块里都不能生成环
那最多保留一棵生成树
Solution
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cctype>
#include <cmath>
using namespace std;
const int M=1000007;
inline int rd(){
int x=0;bool f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=0;
for(;isdigit(c);c=getchar()) x=x*10+c-48;
return f?x:-x;
}
int n,m,k;
int f[M],hav[M];
struct node{
int x,y;
node(int xx=0,int yy=0){x=xx;y=yy;}
}ed[M<<1];
int te;
int find(int x){
return (f[x]==x)?x:(f[x]=find(f[x]));
}
int link(int x,int y){
x=find(x);y=find(y);
if(x!=y){
f[x]=y;
hav[y]=hav[x]|hav[y];
return 0;
}
return hav[x];
}
int main(){
int i,x,y,ans=0;
n=rd(),m=rd(),k=rd();
for(i=1;i<=n;i++) f[i]=i;
for(i=1;i<=k;i++) hav[i]=1;
for(i=1;i<=m;i++){
x=rd(),y=rd();
if(x>k&&y>k){
link(x,y);
}
else ed[++te]=node(x,y);
}
for(i=1;i<=te;i++)
ans+=link(ed[i].x,ed[i].y);
printf("%d\n",ans);
return 0;
}
bzoj 3060[Poi2012]Tour de Byteotia 贪心+生成树的更多相关文章
- BZOJ 3060: [Poi2012]Tour de Byteotia 并查集
前 $k$ 个节点形成的结构必定是森林,而 $[k+1,r]$ 之间肯定是都连上,而剩下的一个在 $[1,k],$一个在 $[k+1,r]$ 的节点就能连多少连多少即可. Code: #include ...
- Bzoj3060 [Poi2012]Tour de Byteotia
3060: [Poi2012]Tour de Byteotia Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 251 Solved: 161 Des ...
- 【BZOJ3060】[Poi2012]Tour de Byteotia 并查集
[BZOJ3060][Poi2012]Tour de Byteotia Description 给定一个n个点m条边的无向图,问最少删掉多少条边能使得编号小于等于k的点都不在环上. Input ...
- [bzoj3060][Poi2012]Tour de Byteotia_并查集
[Poi2012]Tour de Byteotia 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3060 题解: 这类题有一个套路,就是 ...
- [POI2012]Tour de Bajtocja
[POI2012]Tour de Bajtocja 题目大意: 给定一个\(n(n\le10^6)\)个点\(m(m\le2\times10^6)\)条边的无向图,问最少删掉多少条边能使得编号小于等于 ...
- 【[POI2012]TOU-Tour de Byteotia】
[[POI2012]TOU-Tour de Byteotia] 洛谷P3535 https://www.luogu.org/problemnew/show/P3535 JDOJ 2193旅游景点(同类 ...
- Bzoj 3624: [Apio2008]免费道路 (贪心+生成树)
Sample Input 5 7 2 1 3 0 4 5 1 3 2 0 5 3 1 4 3 0 1 2 1 4 2 1 Sample Output 3 2 0 4 3 0 5 3 1 1 2 1 这 ...
- bzoj 2792: [Poi2012]Well【二分+贪心】
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; const ...
- BZOJ_2802_[Poi2012]Warehouse Store_堆+贪心
BZOJ_2802_[Poi2012]Warehouse Store_堆+贪心 Description 有一家专卖一种商品的店,考虑连续的n天. 第i天上午会进货Ai件商品,中午的时候会有顾客需要购买 ...
随机推荐
- JSONPath - XPath for JSON
http://goessner.net/articles/JsonPath/ [edit] [comment] [remove] |2007-02-21| e1 # JSONPath - XPath ...
- key directories in the linux file system
Key directories in the file system: */: Root directory (base of file system) /bin: Executable progra ...
- vue跨域处理(vue项目中baseUrl设置问题)
1.开发环境: 2.生产环境: 然后 const instance = axios.create({ baseURL: process.env.API })
- 用简单的语言描述C++ 是什么?
用简单的语言描述C++ 是什么? 答:C++是在C语言的基础上开发的一种面向对象编程语言,应用广泛.C++支持多种编程范式 --面向对象编程.泛型编程和过程化编程. 其编程领域众广,常用于系统开发,引 ...
- SpringBoot(一)_Eclipse的安装和使用
1.Eclipse中安装STS插件: Help -> Eclipse Marketplace… Search或选择“Popular”标签,选择Spring Tool Suite (STS) fo ...
- 01_8_Struts用DomainModel接收参数
01_8_Struts用DomainModel接收参数 1. 配置struts.xml文件 <package name="user" namespace="/use ...
- iOS 通过storyboard设置UIView或者其他layer图层的圆角
通常我们给Button或者UIView添加圆角是通过如下代码进行实现的 self.button.layer.cornerRadius=10; 但是如果你是使用的故事版或者xib进行设计视图的话,实际上 ...
- python爬虫基础15-python图像处理,PIL库
Python图像处理-Pillow 简介 Python传统的图像处理库PIL(Python Imaging Library ),可以说基本上是Python处理图像的标准库,功能强大,使用简单. 但是由 ...
- centos配置jdk
########## config jdk ########## export JAVA_HOME=/usr/local/java/jdk1.7.0_79 export CLASSPATH=.:${J ...
- hdu4864不是一般的贪心
题目表达的非常清楚,也不绕弯刚开始以为最大权匹配,仔细一想不对,这题的数据双循环建图都会爆,只能先贪心试一下,但一想贪心也要双循环啊,怎么搞? 想了好久没头绪,后来经学长提醒,可以把没用到的先记录下来 ...