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 并查集的更多相关文章

  1. Codefroces 366 D Dima and Trap Graph (最短路)

    Dima and Trap Graph 题意:Dima和Inna越来越喜欢对方,但是Dima的室友缺很没有热情出去玩,然后Dima就把他室友Seryozha骗进了陷阱里.现在Seryozha想要从陷阱 ...

  2. 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/ ...

  3. 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),然 ...

  4. codeforces 400 D Dima and Bacteria【并查集 Floyd】

    题意:给出n个点,分别属于k个集合,判断每个集合里面的点的距离都为0,为0的话输出yes,并输出任意两个集合之间的最短路 这道题目有两个地方不会处理, 先是n个点,分别属于k个集合,该怎么记录下来这里 ...

  5. Codeforces Round #600 (Div. 2) - D. Harmonious Graph(并查集)

    题意:对于一张图,如果$a$与$b$连通,则对于任意的$c(a<c<b)$都有$a$与$c$连通,则称该图为和谐图,现在给你一张图,问你最少添加多少条边使图变为和谐图. 思路:将一个连通块 ...

  6. Codeforces Round #376 (Div. 2) C. Socks---并查集+贪心

    题目链接:http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,每只都有一个颜色,现在他的妈妈要去出差m天,然后让他每天穿第 L 和第 R 只 ...

  7. Codeforces 766D. Mahmoud and a Dictionary 并查集 二元敌对关系 点拆分

    D. Mahmoud and a Dictionary time limit per test:4 seconds memory limit per test:256 megabytes input: ...

  8. Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序

    https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[] ...

  9. CodeForces Roads not only in Berland(并查集)

    H - Roads not only in Berland Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d ...

随机推荐

  1. Luogu 1613 跑路(最短路径,倍增)

    Luogu 1613 跑路(最短路径,倍增) Description 小A的工作不仅繁琐,更有苛刻的规定,要求小A每天早上在6:00之前到达公司,否则这个月工资清零.可是小A偏偏又有赖床的坏毛病.于是 ...

  2. [0,x)的随机数

    #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #in ...

  3. 用HashSet存储不重复的对象

    直接进入主题,先来创建一个类:Bean public class Bean { private int id; private String name; public Bean() { super() ...

  4. sqlite3数据库的简要应用

    Sqlite3数据库升级方案的变化. 1,  若是讲要升级的数据库版本更高,则从低版本数据库中拷贝与新数据库相同字段的内容,其他字段按照默认值创建.A->B->C这样逐个版本升级,每个版本 ...

  5. CSS 笔记(一)

    学习了张鑫旭大神的浮动教程,记一下笔记. 浮动最开始是为了文字环绕图片效果. 浮动会破坏父元素的包裹. 清除浮动:在父元素加上clearfix(after不兼容ie6 7,zoom为ie6 7下方法) ...

  6. Lua程序设计(一)面向对象概念介绍

    完整代码 local mt = {} mt.__add = function(t1,t2) print("两个Table 相加的时候会调用我") end local t1 = {} ...

  7. 论文中“but”与“however”的区别

  8. Git 操作指南

    http://blog.csdn.net/troy__/article/details/40082657

  9. java concurrent 中ExecutorService和CompletionService简单区别

    举个例子,现在需要执行10个任务,这些任务都是有返回值,并且需要使用10个线程同时执行.一般的做法就是创建ExecutorService线程池,pool大小10,每个任务实现Callable接口,然后 ...

  10. 20155211 2016-2017-2 《Java程序设计》第六周学习总结

    20155211 2016-2017-2 <Java程序设计>第六周学习总结 教材学习内容总结 第十章 输入/输出 一.InputStream与OutputStream (一)串流设计的概 ...