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件商品,中午的时候会有顾客需要购买 ...
随机推荐
- WINDOWS-基础:SafeArray的使用方法
1 SafeArray的作用 使用SafeArray SafeArray是VB中的数组存储方式.通过SafeArray,可以在VC++和VB间相互调用. SafeArray也是Automation中的 ...
- 万恶之源 Python
学IT真他妈难受 从早上起来坐到晚上 一天对着电脑啪啪啪
- 瀑布流封装(仿写UITableView)
本篇文章将会仿照苹果系统提供的UITableView类,封装一个瀑布流效果的控件!!! 该控件和系统的UITableView是相同级别的 (继承自系统的UIScrollView) GitHub中Dem ...
- C#_接口基础学习
参考:https://www.cnblogs.com/hamburger/p/4681681.html
- 【markdown】图片的处理
1st:  2ed: ![tip][id] [id]:base64string 本地图片 先把本地图片文件转换成base64位编码 然后把 link 替换成生成的base64编 ...
- 【android】安卓的权限提示及版本相关
Only dangerous permissions require user agreement. The way Android asks the user to grant dangerous ...
- Java代码中的(解压7z加密版)
maven:需要加上这个下载这两个包 <dependency> <groupId>net.sf.sevenzipjbinding</groupId> <art ...
- Linux学习-Linux 主机上的用户讯息传递
查询使用者: w, who, last, lastlog 如果你想要知道目前已登入在系统上面的用户呢?可以透过 w 或 who 来查询喔!如下范例所示: [root@study ~]# w 01:49 ...
- 解决VMware vSphere Client无法连接ESXi虚拟主机方法
1 一般情况下重启services.sh就可以解决(或图形界面下restart management agent)services.sh restart2 若重启services.sh报错且仍然无法连 ...
- CSS 预处理器框架
CSS 预处理器框架 可以按照需求来使用别人的代码 1.sass (compass) 2.less (lesshat/EST) 3.提供现成的 mixin 4.类似 JS 类库 ,封装常用功能 css ...