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.18,英语,《Vocabulary Builder》Unit 15
term/termin, comes from the Latin verb terminare, 'to limit, bound, or set limits to', or the relate ...
- oc18--self1
// // Iphone.h // day13 #import <Foundation/Foundation.h> typedef enum { kFlahlightStatusOpen, ...
- DNS通道检测 国外学术界研究情况——研究方法:基于流量,使用机器学习分类算法居多,也有使用聚类算法的;此外使用域名zif low也有
http://www.ijrter.com/papers/volume-2/issue-4/dns-tunneling-detection.pdf <DNS Tunneling Detectio ...
- [codeforces contest 1119 F] Niyaz and Small Degrees 解题报告 (树形DP+堆)
interlinkage: http://codeforces.com/contest/1119/problem/F description: 有一颗$n$个节点的树,每条边有一个边权 对于一个$x$ ...
- Hdu-6230 2017CCPC-哈尔滨站 A.Palindrome Manacher 主席树
题面 题意:给你一个字符串,问你满足s[i]=s[2n-i]=s[2n+i-2]的子串(这子串长度为3n-2)有多少个,原字符串长度<=5e5 题解:对于这种子串,其实要满足2个回文,跑过一次M ...
- Java Break和continue实现goto功能
continue实验 1 public class test { static int i =0; public static void main(String[] args) { lable1: w ...
- Qt5.9 WebChannel
Qt WebChannel enables peer-to-peer communication between a server (QML/C++ application) and a client ...
- 多个tomcat配置,解决冲突问题
一.一般修改 路径: /opt/apache-tomcat/conf/server.xml 1.第一个tomcat使用默认配置 2.第二个tomcat一般配置 二.特殊修改 1.第二个tomcat特殊 ...
- canvas的常用api
canvas 标签 <canvas width="600" height="400" id="canvas"></canv ...
- HDFS开发中的一些问题(逐步补充)
1.windows操作系统下运行时报:Failed to locate the winutils binary in the hadoop binary path java.io.IOExcept ...