POJ 1944
明天补上。。。
这道题的思路确实很精致。考虑到连的边肯定不会是一个环,所以至少有一个断点。于是,可以枚举这个断点。断点一确定,那么连边的走向也就确定了。用D【i】表示由i开始可以到达的最远点即可。对于中间被断开的点,保存[1,left],[right,n]即可。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath> using namespace std;
const int N=1005;
const int P=10005;
int d[N];
struct ei{
int u,v;
}edge[P]; int main(){
int n,p,u,v;
while(scanf("%d%d",&n,&p)!=EOF){
for(int i=0;i<p;i++){
scanf("%d%d",&u,&v);
edge[i].u=min(u,v);
edge[i].v=max(u,v);
}
int mind=(1<<30);
for(int i=1;i<=n;i++){
memset(d,-1,sizeof(d));
int ans=0,pos=-1;
for(int e=0;e<p;e++){
if(edge[e].u<=i&&edge[e].v>i){
d[1]=max(d[1],edge[e].u);
d[edge[e].v]=max(d[edge[e].v],n);
ans=1;
}
else {
d[edge[e].u]=max(d[edge[e].u],edge[e].v);
}
}
for(int i=1;i<=n;i++){
if(pos==-1&&d[i]!=-1){
pos=i;
}
else{
if(d[i]!=-1){
if(i<=d[pos]&&d[i]>d[pos])
d[pos]=d[i];
else if(i>d[pos]){
ans+=d[pos]-pos;
pos=i;
}
}
}
}
ans+=d[pos]-pos;
mind=min(ans,mind);
}
printf("%d\n",mind);
}
return 0;
}
POJ 1944的更多相关文章
- POJ 1944 - Fiber Communications
原题地址:http://poj.org/problem?id=1944 题目大意:有n个点排成一圈,可以连接任意两个相邻的点,给出 p 对点,要求这 p 对点必须直接或间接相连,求最少的连接边数 数据 ...
- POJ 1944 Fiber Communications (枚举 + 并查集 OR 线段树)
题意 在一个有N(1 ≤ N ≤ 1,000)个点环形图上有P(1 ≤ P ≤ 10,000)对点需要连接.连接只能连接环上相邻的点.问至少需要连接几条边. 思路 突破点在于最后的结果一定不是一个环! ...
- POJ 1944 并查集(模拟)
思路: 肯定是要枚举断点的..就看枚举完断点以后怎么处理了-- 1.用类似并查集的思想- f[x]=max(f[x],y)表示x和y相连(一定要注意取max,,,血的教训) 复杂度O(np) 2.猥琐 ...
- POJ 1944:Fiber Communications
Fiber Communications Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4236 Accepted: 1 ...
- hdu 1536/1944 / POJ 2960 / ZOJ 3084 S-Nim 博弈论
简单的SG函数应用!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #inclu ...
- POJ数据的输入输出格式
POJ在评阅习题时需要向程序提供输入数据,并获取程序的输出结果.因此提交的程序需按照每个习题具体的输入输出格式要求处理输入输出.有的时候,测评系统给出程序的评判结果是“数据错误”或“结果错误”,有可能 ...
- POJ 1027:The Same Game 较(chao)为(ji)复(ma)杂(fan)的模拟
The Same Game Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5168 Accepted: 1944 Des ...
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
随机推荐
- 2016.04.22,英语,《Vocabulary Builder》Unit 17
anim, comes from the Latin anima, meaning 'breath' or 'soul'. animism: ['ænɪmɪzəm] n. 泛灵论,精神存在论,神创宇宙 ...
- 5187: [Usaco2018 Jan]Sprinklers
#include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> #inc ...
- hdoj--1150--Machine Schedule(最小点覆盖)
Machine Schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdoj--2767--Proving Equivalences (scc+缩点)
Proving Equivalences Time Limit : 4000/2000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other ...
- Python 字典(dict)操作(update)
1. get 注意以下两种形式的细微差别,差别在返回值的类型上: d.get(value, '') d.get(value, ['']) >> d = {} >> d.get( ...
- 【HDU 1846】 Brave Game
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1846 [算法] 巴什博弈 若有(m+1)个石子,显然先手不能直接取完,后手必胜 因此,我们可以把石 ...
- 关于H5优化的一些问题
required修改默认提示 : <form action="abc.php"> <input type="text" required o ...
- 46.Qt 使用OpenGL绘制立方体
main.cpp #include <QApplication> #include <iostream> #include "vowelcube.h" in ...
- A - I Wanna Be the Guy
Problem description There is a game called "I Wanna Be the Guy", consisting of n levels. L ...
- 最影响APP软件质量和成本的三个方面。希望大家一定要记在心里!
1.功能的开发方式 现在市场上存在的几种开发方式如下: a.web网页加壳生成APP web网页加壳生成APP的开发方式,先花几百块钱买个现成的手机网站模板,在加壳打包一个APP只需要5分钟,但是做出 ...