TOJ1550: Fiber Communications

1550: Fiber Communications 分享至QQ空间

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=10005;
int l[N],r[N],a[1005];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    int ans=n-1;
    for(int i=1;i<=m;i++)
        scanf("%d%d",&l[i],&r[i]);
    for(int i=0;i<n;i++)
    {
        memset(a,0,sizeof(a));
        for(int j=1;j<=m;j++)
        {
            int t=(l[j]-i+n-1)%n+1,w=(r[j]-i+n-1)%n+1;
            if(t>w)swap(t,w);
            a[t]++,a[w]--;
        }
        int k=0,w=0;
        for(int j=1;j<n;j++)
        {
            k+=a[j];
            if(k)w++;
        }
        ans=min(w,ans);
    }
    printf("%d",ans);
    return 0;
}

 

 

 

posted @ 2017-11-21 19:11  暴力都不会的蒟蒻  阅读(303)  评论(0编辑  收藏  举报