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. sshd_config配置注释

    # $OpenBSD: sshd_config,v 1.80 2008/07/02 02:24:18 djm Exp $ # This is the sshd server system-wide c ...

  2. python_99_面向对象多态

    #多态:一种接口,多种实现.主要作用:实现接口重用 #方法1: class Animal(object): def __init__(self,name): self.name=name class ...

  3. window.addEventListener介绍说明

    原型 public override function addEventListener(type:String, listener:Function, useCapture:Boolean = fa ...

  4. 用C#(ASP.Net)在Exchange Server环境下发送邮件

    普通的邮件, 用System.Net.Mail 类 或 System.Web.Mail 类 处理即可, 但是Exchange Server 环境下, 这两个类起不了作用-------至少目前我看到的情 ...

  5. C#MySQL增删改查

    首先在项目中添加引用 using MySql.Data.MySqlClient; 连接字符串  private string connString="server=localhost;use ...

  6. d3.js--04(enter和exit)

    enter() 当DOM数量少于data的数量,或者压根一个都没有的时候,我们一般会希望让程序帮忙创建. <!DOCTYPE html> <html> <head> ...

  7. Spinal Tap Case -freecodecamp算法题目

    Spinal Tap Case 1.要求 将字符串转换为 spinal case. Spinal case 是 all-lowercase-words-joined-by-dashes 这种形式的,也 ...

  8. Jquery之 Ajax /json

    前言: Ajax = Asynchronous JavaScript and XML(异步的JavaScript和XML) Ajax不是新的编程语言,而是一种使用现有标准的新方法. Ajax最大的优点 ...

  9. 解决iPhone滑动不流畅问题

    前段时间在做一个手机端的页面时遇到了iOS上滑动不流畅的问题,后来才发现安卓上没有问题,才意识到这是兼容性问题引起的,所以遇到问题后快速定位到问题根源非常重要.在网上一搜就找到了解决方案.以后遇到类似 ...

  10. 8.Yii2.0框架控制器接收get.post数据

    8.Yii2.0框架控制器接收get.post数据 一.get传参 <?php /** * Created by Haima. * Author:Haima * QQ:228654416 * D ...