codeforces 366D 并查集
2015-05-14 21:16
417 查看
//给你一个无向图,图的每条边有一个范围,所选数x要在这个范围能过这条边
//求x最大范围
//枚举所有的边的右边,对于所选右边找左边最小值,用并查集判断是否可行
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std ;
const int maxn = 1010;
int F[maxn] ;
int find(int x)
{
if(x == F[x])return x ;
else return F[x] = find(F[x]) ;
}
int join(int x,int y)
{
int fx = find(x) ;
int fy = find(y) ;
if(fx != fy)
F[fx] = fy ;
}
struct node
{
int x , y ,l ,r;
}a[maxn*3] ;
bool cmp(struct node a ,struct node b)
{
return a.l < b.l ;
}
int main()
{
int n , m ;
while(~scanf("%d%d" ,&n , &m))
{
int x , y , l ,r ;
for(int i = 1;i <= m;i++)
scanf("%d%d%d%d" ,&a[i].x , &a[i].y ,&a[i].l ,&a[i].r) ;
sort(a+1 ,a+1+m,cmp) ;
int ans = 0 ;
for(int i = 1;i <= m;i++)
{
for(int j = 1;j <= n;j++)
F[j] = j ;
for(int j = 1;j <= m;j++)
{
if(a[j].l > a[i].r)break;
if(a[j].r < a[i].r)continue ;
join(a[j].x ,a[j].y);
if(find(1) == find(n))
{
ans = max(ans , a[i].r - a[j].l + 1) ;
break ;
}
}
}
if(ans)printf("%d\n",ans) ;
else puts("Nice work, Dima!");
}
return 0;
}
//求x最大范围
//枚举所有的边的右边,对于所选右边找左边最小值,用并查集判断是否可行
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std ;
const int maxn = 1010;
int F[maxn] ;
int find(int x)
{
if(x == F[x])return x ;
else return F[x] = find(F[x]) ;
}
int join(int x,int y)
{
int fx = find(x) ;
int fy = find(y) ;
if(fx != fy)
F[fx] = fy ;
}
struct node
{
int x , y ,l ,r;
}a[maxn*3] ;
bool cmp(struct node a ,struct node b)
{
return a.l < b.l ;
}
int main()
{
int n , m ;
while(~scanf("%d%d" ,&n , &m))
{
int x , y , l ,r ;
for(int i = 1;i <= m;i++)
scanf("%d%d%d%d" ,&a[i].x , &a[i].y ,&a[i].l ,&a[i].r) ;
sort(a+1 ,a+1+m,cmp) ;
int ans = 0 ;
for(int i = 1;i <= m;i++)
{
for(int j = 1;j <= n;j++)
F[j] = j ;
for(int j = 1;j <= m;j++)
{
if(a[j].l > a[i].r)break;
if(a[j].r < a[i].r)continue ;
join(a[j].x ,a[j].y);
if(find(1) == find(n))
{
ans = max(ans , a[i].r - a[j].l + 1) ;
break ;
}
}
}
if(ans)printf("%d\n",ans) ;
else puts("Nice work, Dima!");
}
return 0;
}
相关文章推荐
- Codeforces 366D 贪心+并查集
- Codeforces 366D Dima and Trap Graph 【并查集】
- Codeforces 468B Two Sets 并查集
- 【Codeforces 741 B. Arpa's weak amphitheater and Mehrdad's 】+ 并查集 + 01背包
- CodeForces - 893C Rumor(并查集)
- CodeForces 622C Not Equal on a Segment(并查集)
- codeforces 466E Information Graph 并查集LCA离线一通乱搞
- Codeforces 437D The Child and Zoo(贪心+并查集)
- codeforces 593D(并查集 + LCA)
- CodeForces - 744A (并查集)
- Codeforces 25 D.Roads not only in Berland(并查集)
- 【codeforces】Misha and Changing Handles(映射+并查集)
- 【 CodeForces 209C】 【欧拉回路推结论+并查集计算联通分量】 【给定n点m边无向图,可能有自环和重边。 问最少添加多少条边后,使得图存在从点1出发发又回到点1的欧拉回路】
- CodeForces - 731C Socks(并查集)(贪心)
- CodeForces - 698B Fix a Tree(并查集)
- 【并查集+贪心】 CodeForces - 731C - Socks
- codeforces 445 B. DZY Loves Chemistry (并查集)
- 【Codeforces】-DZY Loves Chemistry(并查集变形)
- Codeforces 788B 想法+并查集
- CodeForces 292D Connected Components(并查集 前后缀)