Codeforces 336D Dima and Trap Graph 并查集
枚举区间的左端点, 然后那些左端点比枚举的左端点小的都按右端点排序然后并查集去check
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long
using namespace std; const int N = 3e3 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-; int n, m, tot, ans;
int fa[N];
struct Edge {
bool operator < (const Edge& rhs) const {
return l < rhs.l;
}
int a, b, l, r;
} e[N], tmp[N]; int getRoot(int x) {
return fa[x] == x ? x : fa[x] = getRoot(fa[x]);
} int Merge(int id) {
int u = tmp[id].a, v = tmp[id].b;
fa[getRoot(u)] = getRoot(v);
if(getRoot() == getRoot(n)) return tmp[id].r;
else return -;
}
int main() {
scanf("%d%d", &n, &m);
for(int i = ; i <= m; i++)
scanf("%d%d%d%d", &e[i].a, &e[i].b, &e[i].l, &e[i].r);
sort(e + , e + + m);
for(int i = ; i <= m; i++) {
for(int j = ; j <= n; j++) fa[j] = j;
tmp[++tot] = e[i];
int ret = Merge(tot);
for(int j = ; j < tot && ret == -; j++) ret = Merge(j);
if(~ret) ans = max(ans, min(e[i].r, ret) - e[i].l + );
for(int j = tot; j > ; j--)
if(tmp[j].r > tmp[j - ].r) swap(tmp[j], tmp[j - ]);
}
if(ans) printf("%d\n", ans);
else puts("Nice work, Dima!");
return ;
} /*
*/
Codeforces 336D Dima and Trap Graph 并查集的更多相关文章
- Codefroces 366 D Dima and Trap Graph (最短路)
Dima and Trap Graph 题意:Dima和Inna越来越喜欢对方,但是Dima的室友缺很没有热情出去玩,然后Dima就把他室友Seryozha骗进了陷阱里.现在Seryozha想要从陷阱 ...
- Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph 并查集
D. Mr. Kitayuta's Colorful Graph Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/ ...
- cf 366D D. Dima and Trap Graph (计算所有线段共同覆盖的某段区间)
http://codeforces.com/problemset/problem/366/D 题意:给出n个点,m条边,a,b,ll,rr分别代表点a,点b相连,点a和点b的区间范围(ll,rr),然 ...
- codeforces 400 D Dima and Bacteria【并查集 Floyd】
题意:给出n个点,分别属于k个集合,判断每个集合里面的点的距离都为0,为0的话输出yes,并输出任意两个集合之间的最短路 这道题目有两个地方不会处理, 先是n个点,分别属于k个集合,该怎么记录下来这里 ...
- Codeforces Round #600 (Div. 2) - D. Harmonious Graph(并查集)
题意:对于一张图,如果$a$与$b$连通,则对于任意的$c(a<c<b)$都有$a$与$c$连通,则称该图为和谐图,现在给你一张图,问你最少添加多少条边使图变为和谐图. 思路:将一个连通块 ...
- Codeforces Round #376 (Div. 2) C. Socks---并查集+贪心
题目链接:http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,每只都有一个颜色,现在他的妈妈要去出差m天,然后让他每天穿第 L 和第 R 只 ...
- Codeforces 766D. Mahmoud and a Dictionary 并查集 二元敌对关系 点拆分
D. Mahmoud and a Dictionary time limit per test:4 seconds memory limit per test:256 megabytes input: ...
- Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序
https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[] ...
- CodeForces Roads not only in Berland(并查集)
H - Roads not only in Berland Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
随机推荐
- 前端学习 -- Html&Css -- 相对定位 绝对定位 固定定位
相对定位 - 定位指的就是将指定的元素摆放到页面的任意位置,通过定位可以任意的摆放元素. - 通过position属性来设置元素的定位. -可选值: static:默认值,元素没有开启定位: rela ...
- POJ 2235 Frogger / UVA 534 Frogger /ZOJ 1942 Frogger(图论,最短路径)
POJ 2235 Frogger / UVA 534 Frogger /ZOJ 1942 Frogger(图论,最短路径) Description Freddy Frog is sitting on ...
- 伤不起:File.toPath() & Paths.get()
java.nio.file.Path这个类应该是从java7才开始有的. 通过File类有两个方法可以转换成Path. 1. Path p = Paths.get(file.toURI()); // ...
- Mac 删除应用卸载后无法正常移除的图标
经常会不通过appstore下载软件,也就是从网页中下载dmg,自己安装,但是当我不再想要这个软件,然后把它卸载掉之后就会发现,launchpad里还是遗留了这个软件的图标,而且删不掉.这个时候,就可 ...
- 目标检测评价指标(mAP)
常见指标 precision 预测出的所有目标中正确的比例 (true positives / true positives + false positives). recall 被正确定位识别的目标 ...
- OpenStack 图形化服务 Horizon使用(十三)
构建一台云主机 上图中Count可以选择同时创建多台 最终“启动实例” 创建成功后,可以进入控制台,操作新建云主机
- GUI起头
package com.lovo.frame; import java.awt.Color;import java.awt.Container;import java.awt.Font;import ...
- 在angularJs实现批量删除
原理:在js中定义一个数组,然后给每个复选框一个点击事件,点击事件的方法参数有两个,一个是事件源$event,一个是id.点击复选框根据事件源判断是否被选中,然后进而是向这个数组增加或者删除id. $ ...
- CF11D A Simple Task(状压DP)
\(solution:\) 思路大家应该都懂: 状压DP:\(f[i][j]\),其中 \(i\) 这一维是需要状压的,用来记录19个节点每一个是否已经走过(走过为 \(1\) ,没走为 \(0\) ...
- org.hibernate.TransientObjectException异常
代码如下: /** * 测试4:新增一个秘书角色,并赋给张三该角色 */ @Test public void test4(){ Session session = HibernateUtils.ope ...