1550: Fiber Communications 

Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByte
Total Submit: 3            Accepted:2

Description

Farmer John wants to connect his N (1 <= N <= 1,000) barns (numbered 1..N) with a new fiber-optic network.  However, the barns are located in a circle around the edge of a large pond, so he can only connect pairs of adjacent barns. The circular configuration means that barn N is adjacent to barn 1.
FJ doesn't need to connect all the barns, though, since only certain pairs of cows wish to communicate with each other.  He wants to construct as few connections as possible while still enabling all of these pairs to communicate through the network.  Given the list of barns that wish to communicate with each other, determine the minimum number of lines that must be laid.  To communicate from barn 1 to barn 3, lines must be laid from barn 1 to barn 2 and also from barn 2 to barn 3.

Input

* Line 1: Two integers, N and P (the number of communication pairs, 1 <= P <= 10,000)
* Lines 2..P+1: two integers describing a pair of barns between which  communication is desired.  No pair is duplicated in the list.

Output

One line with a single integer which is the minimum number of direct connections FJ needs to make.

Sample Input

5 2
1 3
4 5

Sample Output

3

Hint

Which connect barn pairs 1-2, 2-3, and 4-5.

Source

USACO Feburary 2002

第一次我没看懂题意

n个数排成一个圈,刚开始都没有边相连,你可以给它们之间加上边,但能给相邻的数字之间加边,给出几对要求,要求这几对要求的2个点都必须相连,求最少的需要添加的边数

可能是个二分图?但是一项这个我直接枚举顶多1e7,还是可以跑过去的,枚举开始的点i,然后去连边,居然A掉了

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N=;
int l[N],r[N],a[];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int ans=n-;
for(int i=;i<=m;i++)
scanf("%d%d",&l[i],&r[i]);
for(int i=;i<n;i++)
{
memset(a,,sizeof(a));
for(int j=;j<=m;j++)
{
int t=(l[j]-i+n-)%n+,w=(r[j]-i+n-)%n+;
if(t>w)swap(t,w);
a[t]++,a[w]--;
}
int k=,w=;
for(int j=;j<n;j++)
{
k+=a[j];
if(k)w++;
}
ans=min(w,ans);
}
printf("%d",ans);
return ;
}

TOJ1550: Fiber Communications的更多相关文章

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

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

  2. POJ1944 Fiber Communications (USACO 2002 February)

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

  3. POJ 1944:Fiber Communications

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

  4. POJ 1944 - Fiber Communications

    原题地址:http://poj.org/problem?id=1944 题目大意:有n个点排成一圈,可以连接任意两个相邻的点,给出 p 对点,要求这 p 对点必须直接或间接相连,求最少的连接边数 数据 ...

  5. usaco 2002 月赛 Fiber Communications 题解

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

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

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

  7. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  8. 转载:hdu 题目分类 (侵删)

    转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...

  9. Coroutine in Java - Quasar Fiber实现--转载

    转自 https://segmentfault.com/a/1190000006079389?from=groupmessage&isappinstalled=0 简介 说到协程(Corout ...

随机推荐

  1. ALTER AVAILABILITY GROUP (Transact-SQL)

    更改 SQL Server 中现有的 AlwaysOn 可用性组.              只有当前主副本支持大多数 ALTER AVAILABILITY GROUP 参数. 但是,只有辅助副本支持 ...

  2. SQL 事物回滚

    转自https://www.cnblogs.com/delphinet/archive/2010/08/17/1801424.html 第一种:   declare   @iErrorCount    ...

  3. ssh连接github连不上

    连接github报端口22连接不上: 输入命令展示出ssh_config内容后: vim /etc/ssh/ssh_config 或者使用open /etc/ssh/ssh_config命令在文本编辑 ...

  4. BZOJ 4423: [AMPPZ2013]Bytehattan 并查集+平面图转对偶图

    4423: [AMPPZ2013]Bytehattan Time Limit: 3 Sec  Memory Limit: 128 MB Submit: 277  Solved: 183 [Submit ...

  5. FiraCode 字体 => 箭头函数变成 整体 还有 等于 不等于

    https://github.com/tonsky/FiraCode Enable in Settings → Editor → Color Scheme → Color Scheme Font →  ...

  6. 2018.4.8 Mac/Win 破解StartUml软件

    Mac破解 在桌面选择前往----前往文件夹-----输入"/应用程序/StarUML.app/Contents/www/license/node/LicenseManagerDomain. ...

  7. JSONPath - XPath for JSON

    http://goessner.net/articles/JsonPath/ [edit] [comment] [remove] |2007-02-21| e1 # JSONPath - XPath ...

  8. java基础—流

    一.JAVA流式输入/输出原理

  9. java基础—线程(一)

    一.线程的基本概念

  10. Java基础操作面试题:Map集合排序 需要TreeMap 构造方法参数有比较器 输入字符串,统计A、B、C、D、出现次数,由高到低输出字母和出现次数,使用Map集合完成此题

    Map和Collections是同级别的,不能像List排序那样直接用Collections.sort(new Comparator<?>(){ 复写compara方法}); HashMa ...