原题地址:http://poj.org/problem?id=1944

题目大意:有n个点排成一圈,可以连接任意两个相邻的点,给出 p 对点,要求这 p 对点必须直接或间接相连,求最少的连接边数

数据范围:n <= 1000, p <= 10000

算法分析:

一开始当最小生成树做的,才发现自己 SB 了……

先考虑不是环形而是线形的结构,直接贪心连接每两个点之间的所有点就好了。这样我们可以枚举环形的断点,然后逐次贪心,求最小解即可

很多同学在贪心的时候应用了线段树是复杂度高达O(np log n),其实丝毫没有必要,我们只需要每次断点时生成一个数组,在每对点的左边点处加1,再在右边点处减1,然后求一下部分和,部分和中正数的个数即为所求(详见代码)

参考代码:

 //date 20140205
#include <cstdio>
#include <cstring> const int maxn = ;
const int maxp = ;
const int INF = 0x7F7F7F7F; inline void swap(int &a, int &b){int x = a; a = b; b = x;}
inline int min(int a, int b){return a < b ? a : b;} int n, p;
int pa[maxp][];
int s[maxn << ]; int main()
{
scanf("%d%d", &n, &p);
for(int i = ; i <= p; ++i)
{
scanf("%d%d", &pa[i][], &pa[i][]);
if(pa[i][] > pa[i][])swap(pa[i][], pa[i][]);
} int ans = INF;
for(int i = ; i <= n; ++i)
{
memset(s, , sizeof s);
for(int j = ; j <= p; ++j)
{
int x = pa[j][], y = pa[j][];
if(x <= i)x += n;
if(y <= i)y += n;
if(x > y)swap(x, y);
++s[x]; --s[y];
}
int now = , res = ;
for(int j = ; j <= n; ++j)
{
now += s[i + j];
if(now > )++res;
}
ans = min(ans, res);
}
printf("%d\n", ans);
return ;
}

POJ 1944 - Fiber Communications的更多相关文章

  1. POJ 1944 Fiber Communications (枚举 + 并查集 OR 线段树)

    题意 在一个有N(1 ≤ N ≤ 1,000)个点环形图上有P(1 ≤ P ≤ 10,000)对点需要连接.连接只能连接环上相邻的点.问至少需要连接几条边. 思路 突破点在于最后的结果一定不是一个环! ...

  2. POJ 1944:Fiber Communications

    Fiber Communications Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4236   Accepted: 1 ...

  3. [USACO2002][poj1944]Fiber Communications(枚举)

    Fiber Communications Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3804   Accepted: 1 ...

  4. POJ1944 Fiber Communications (USACO 2002 February)

    Fiber Communications 总时间限制:  1000ms 内存限制:  65536kB 描述 Farmer John wants to connect his N (1 <= N ...

  5. TOJ1550: Fiber Communications

    1550: Fiber Communications  Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByteTotal ...

  6. POJ 2579 Fiber Network(状态压缩+Floyd)

    Fiber Network Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3328   Accepted: 1532 Des ...

  7. usaco 2002 月赛 Fiber Communications 题解

    Description Farmer John wants to connect his N (1 <= N <= 1,000) barns (numbered 1..N) with a ...

  8. POJ 2570 Fiber Network(最短路 二进制处理)

    题目翻译 一些公司决定搭建一个更快的网络.称为"光纤网". 他们已经在全世界建立了很多网站.这 些网站的作用类似于路由器.不幸的是,这些公司在关于网站之间的接线问题上存在争论,这样 ...

  9. ZOJ 1967 POJ 2570 Fiber Network

    枚举起点和公司,每次用DFS跑一遍图,预处理出所有的答案.询问的时候很快就能得到答案. #include<cstdio> #include<cmath> #include< ...

随机推荐

  1. Sencha Touch xtype对应的class

    Sencha Touch 2的有效xtype xtype Class ----------------- --------------------- actionsheet Ext.ActionShe ...

  2. 如何开始你的CTF比赛之旅-网站安全-

    在过去的两个星期里,我已经在DEFCON 22 CTF里检测出了两个不同的问题:“shitsco ”和“ nonameyet ”.感谢所有 的意见和评论,我遇到的最常见的问题是:“我怎么才能在CTFs ...

  3. java输出流实现文件下载

    //导出Excel try { HSSFWorkbook wb = carService.export(list); //调用service方法~! response.setContentType(& ...

  4. PHP PDO_MYSQL 操作类 YAF嵌入高性能类而准备

    https://github.com/indieteq/PHP-MySQL-PDO-Database-Class PHP-PDO-MySQL-Class A PHP MySQL PDO class s ...

  5. Ruby中的Profiling工具

    看看如何调试Ruby的性能问题 李哲 - APRIL 08, 2015 Ruby内置的profiler 内置的profiler实现的很简单,在ruby2.2中只有150行代码,大家可以看看它的实现pr ...

  6. 历代诗词咏宁夏注释3----蔡升元:<题大清渠>

    题大清渠 蔡升元 为怜□□□□□,□□□□□□□. □□□□沙碛里,凿开峡口贺兰旁. 支分九堡通沟浍,鼎峙三渠并汉唐. 作吏尽如君任事,不难到处乐丰穰. 两渠中划大清渠,畚筑无劳民力纾.[1] 心画万 ...

  7. javascript 图片延迟加载

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  8. hdu 3590 PP and QQ 博弈论

    思路: 在贾志豪神牛的论文 里,这两种游戏都有 其中树的删边游戏:叶子节点的SG值为0:中间节点的SG值为它的所有子节点的SG值加1 后的异或和. ANTI-SG:先手必胜当且仅当:(1)游戏的SG函 ...

  9. hdu 4462(状态压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4462 思路:由于数据不大,可以直接将所有的状态表示出来,然后枚举,判断能否将方格全部覆盖. http: ...

  10. sessionID的本质

    SessionID的本质 一.客户端用cookie保存了sessionID 客户端用cookie保存了sessionID,当我们请求服务器的时候,会把这个sessionID一起发给服务器,服务器会到内 ...