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件商品,中午的时候会有顾客需要购买 ...
随机推荐
- 01_3Java Application初步
01_3Java Application初步 l Java源文件以“java”为扩展名.源文件的基本组成部分是类(class),如本例中的HelloWorld类. l 一个源文件中最多只有一个publ ...
- 简单的Datable转List方法
public static class DataTableUtils<T> where T : new() { public static List<T> ConvertToM ...
- token_get_all()函数
token_get_all (PHP 4 >= 4.2.0, PHP 5) token_get_all — 将提供的源码按 PHP 标记进行分割,可以用作php源代码的压缩,会按照固定的分解方法 ...
- Voyager下的Dashboard Widgets
widgets设置,voyager.php下找到'widgets': 'widgets' => [ 'TCG\\Voyager\\Widgets\\UserDimmer', 'TCG\\Voya ...
- python之路-双下方法
双下方法 定义: 双下方法是特殊方法,他是解释器提供的,由双下线加方法名加双下划线 __方法名__具有特殊意义的方法 双下方法主要是Python源码程序员使用的,元编程 我们在开发中尽量不要使用双下方 ...
- Java-basic-4-数据类型
Number类 装箱:将内置数据类型作为包装类对象使用:拆箱:相反 public class test{ public static void main(String args[]) { // box ...
- poj 3280 回文字符串问题 dp算法
题意:给一个字符串,构成回文(空也是回文) 其中增删都需要代价.问:代价最少? 思路:把字符串s变空 dp[i][j]表示变成回文的最小代价 for(i=m-1;i>=0;--i) ...
- Java中对象方法的调用过程&动态绑定(Dynamic Binding)
Java面向对象的最重要的一个特点就是多态, 而多态当中涉及到了一个重要的机制是动态绑定(Dynamic binding). 之前只有一个大概的概念, 没有深入去了解动态绑定的机理, 直到很多公司都问 ...
- LayoutInflater的用法
Instantiates a layout XML file into its corresponding View objects. It is never used directly. Inste ...
- js---post与get请求的区别
request获取请求参数 最为常见的客户端传递参数方式有两种: 浏览器地址栏直接输入:一定是GET请求: 超链接:一定是GET请求: 表单:可以是GET,也可以是POST,这取决与<form& ...