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 贪心+生成树的更多相关文章

  1. BZOJ 3060: [Poi2012]Tour de Byteotia 并查集

    前 $k$ 个节点形成的结构必定是森林,而 $[k+1,r]$ 之间肯定是都连上,而剩下的一个在 $[1,k],$一个在 $[k+1,r]$ 的节点就能连多少连多少即可. Code: #include ...

  2. Bzoj3060 [Poi2012]Tour de Byteotia

    3060: [Poi2012]Tour de Byteotia Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 251  Solved: 161 Des ...

  3. 【BZOJ3060】[Poi2012]Tour de Byteotia 并查集

    [BZOJ3060][Poi2012]Tour de Byteotia Description 给定一个n个点m条边的无向图,问最少删掉多少条边能使得编号小于等于k的点都不在环上. Input     ...

  4. [bzoj3060][Poi2012]Tour de Byteotia_并查集

    [Poi2012]Tour de Byteotia 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3060 题解: 这类题有一个套路,就是 ...

  5. [POI2012]Tour de Bajtocja

    [POI2012]Tour de Bajtocja 题目大意: 给定一个\(n(n\le10^6)\)个点\(m(m\le2\times10^6)\)条边的无向图,问最少删掉多少条边能使得编号小于等于 ...

  6. 【[POI2012]TOU-Tour de Byteotia】

    [[POI2012]TOU-Tour de Byteotia] 洛谷P3535 https://www.luogu.org/problemnew/show/P3535 JDOJ 2193旅游景点(同类 ...

  7. 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 这 ...

  8. bzoj 2792: [Poi2012]Well【二分+贪心】

    #include<iostream> #include<cstdio> #include<algorithm> using namespace std; const ...

  9. BZOJ_2802_[Poi2012]Warehouse Store_堆+贪心

    BZOJ_2802_[Poi2012]Warehouse Store_堆+贪心 Description 有一家专卖一种商品的店,考虑连续的n天. 第i天上午会进货Ai件商品,中午的时候会有顾客需要购买 ...

随机推荐

  1. 201621123080《Java程序设计》第三周学习总结

    Week03-面向对象入门 1. 本周学习总结 2. 书面作业 1.以面向对象方式改造数据结构作业'有理数'(重点) 1.1 截图你主要代码(需要在程序中出现你的学号和姓名)并粘贴程序的git地址. ...

  2. nodejs开发过程中遇到的一些插件记录

    1.chalk Github:https://github.com/chalk/chalk   终端样式定制插件,可自定义输出日志的样式. 1.semver   管网:https://semver.o ...

  3. stm32L011F3使用开发小记——开发坏境

    今日,因工作需要,使用到了stm32L011F3芯片,此芯片基于CM0+内核,属于低功耗芯片 开发平台可以免费用于KEILMDK,keil公司用免费的许可证,网址:https://www2.keil. ...

  4. centos7 安全配置

    CentOS是最多人用来运行服务器的 Linux 版本,最新版本是 CentOS 7.当你兴趣勃勃地在一台主机或 VPS 上安装 CentOS 7 后,首要的工作肯定是加强它的安全性,以下列出的七件事 ...

  5. win7 怎么进入注册表

    windows图标键(就是ALT旁边的windows小旗子)+R键----输入"regedit"---回车

  6. 基于TCP协议的网络通讯流程

    不多说了,先上个图: 从上面的图中可以看出来,基于TCP协议进行通讯可以大致分成以下几个阶段: 1. 首先是在服务器端, TCP Sever调用socket(), bind(), listen()完成 ...

  7. Splay的用法

    splay区间增减查询 #include<cstdio> #include<algorithm> using namespace std; ; const int INF = ...

  8. JAVA-基础(六) Java.io

    由于IDEA相对于我这种新手来说,对学习JAVA还是比较方便,后记都移步到IDEA中进行基础学习 1.File(文件类) 尽管java.io定义的大多数类是实行流式操作的,File类不是.它直接处理文 ...

  9. mongo以及mysql常用语句

    db.user.find({']}}).pretty(); db.user.find({primaryPhone:).forEach(function(user){print('my phonenum ...

  10. [python][oldboy]关键字参数和位置参数,默认参数,可变长参数(无名,有名)

    关键字参数和位置参数是在函数调用的时候定义 关键字参数是以键-值对出现,无序,多一个不行,少一个不行 位置参数,有序,多一个不行,少一个不行 混搭:所有位置参数必须在关键字参数的左边 默认参数是在函数 ...