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. /usr/local/sbin/fping -s www.baidu.com www.google.com

    /usr/local/sbin/fping -s www.baidu.com www.google.com

  2. 修改字体篇:css3@font-face@字体转换@浏览器字体支持

    @font-face { font-family: 'MyWebFont'; src: url('webfont.eot'); /* IE9 Compat Modes */ src: url('web ...

  3. itextsharp-5.2.1-修正无法签名大文件问题

    PDF文件格式几乎是所有开发平台或者业务系统都热爱的一种文档格式. 目前有很多优秀的开源PDF组件和类库.主要平时是使用.NET和Java开发,所以比较偏好使用iText,当然,它本身就很强大.iTe ...

  4. Android(java)学习笔记119:BroadcastReceiver之 短信发送的广播接收者

    有时候,我们需要开发出来一个短信监听器,监听用户发送的短信记录,下面就是一个案例,这里同样需要使用广播机制. 下面同样是代码示例,MainActivity.java 和  activity_main. ...

  5. FMDB浅析(思想)

    http://www.cnblogs.com/OTgiraffe/p/5931800.html 一.FMDB介绍 FMDB是一种第三方的开源库,FMDB就是对SQLite的API进行了封装,加上了面向 ...

  6. Happy Equation

    Source: The 10th Shandong Provincial Collegiate Programming Contest 题解: 因为2^p为偶数,所以a,x的奇偶性相同 1.当a为奇数 ...

  7. Harvest of Apples

    问题 B: Harvest of Apples 时间限制: 1 Sec  内存限制: 128 MB提交: 18  解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 Ther ...

  8. 用python Image读图

    https://www.cnblogs.com/kongzhagen/p/6295925.html import os name = [] with open('/media/hdc/xing/Dee ...

  9. redis入门指南书中概要

    一.简介 1.redis是一个开源的.高性能的.基于键值对的缓存和存储系统,通过提供多种键值数据类型适应不同场景下的缓存和存储需求,同时redis高级功能能胜任消息队列.任务队列等不同角色. 2.内存 ...

  10. BZOJ-1833(数位DP)

    #include <bits/stdc++.h> using namespace std; typedef long long ll; ll a,b; int k[20]; ll dp[2 ...