usaco 2002 月赛 Fiber Communications 题解

时间:2022-10-19 14:13:29

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(or just from barn 3 to 1,if n=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. 

这道题。。。哎,说多了都是泪啊!这道题大意是:有n个谷仓环状连在一起,只有相邻两个之间可以铺一条路。给出p个要求,每个要求x,y表示x和y想联系。当然,因为环形,可以双向铺路。求最少铺多少路。



        一看以为是二分图,然后发现自己不会。。。

一看他人的简要题解,发现思路很简单:首先我们假设只是直线式的,那么像“火烧赤壁”那样的线段树可以轻松解决。然后我们就枚举断点,把效率乘上一个n就行了。

       自己一琢磨,不对啊!因为是枚举断点,所以每个要求可能x和y会变化(倘若我们像破碎的项链把它展开的话)。这样,每次枚举后,就要重新快排,而且之前要p的效率把a拷贝和处理一下。这样,效率是O(n*p*log(p)),看来要爆了。正在吭哧吭哧写代码,晨阳发现一个直接暴力桶排染色的程序,它竟然A了!它的最坏效率应该是O(n^2*p)!!!!因为我那个相对优化的程序比较麻烦,我就毅然决定也写暴力的,关键是常数要压的小一点。

      附注:我们在poj开了个小号:JSBLHYSYC。那里各种的编译器真是令人眼花缭乱。终于,我在C++的编译器中过了!!(以前我都是用G++的)
代码(要求1000ms,压着就969ms过去了)
#include<stdio.h>
#include<cstring>
using namespace std;
struct arr{long x,y;}a[10001];
char f[2001];
long start,end,xx,yy,i,j,k,min,n,p,t;
inline long c(long x)
{
  if (x<start) return x+n;if (x>end) return x-n;
  return x;
}
int main()
{
  scanf("%ld %ld",&n,&p);
  for (i=1;i<=p;i++)
    {
      scanf("%ld %ld",&a[i].x,&a[i].y);
      if (a[i].x>a[i].y) {t=a[i].x;a[i].x=a[i].y;a[i].y=t;}
    }
  long ans=n;
  for (i=1;i<=n;i++)
  {
    start=i;end=start+n-1;min=0;
    memset(f,0,sizeof(f));
    for (j=1;j<=p;j++)
    {
      xx=c(a[j].x);yy=c(a[j].y);
      if (xx>yy) {t=xx;xx=yy;yy=t;}
      for (k=xx;k<yy;k++) f[k]=1;
    }
    for (j=start;j<=end;j++) if (f[j]==1) min++;
    if (min<ans) ans=min;
  }
  printf("%ld",ans);
  //scanf("%ld %ld",&n,&p);
  return 0;
}