zoukankan      html  css  js  c++  java
  • CodeForces

    Long time ago, there was a great kingdom and it was being ruled by The Great Arya and Pari The Great. These two had some problems about the numbers they like, so they decided to divide the great kingdom between themselves.

    The great kingdom consisted of n cities numbered from 1 to n and m bidirectional roads between these cities, numbered from 1 to m. The i-th road had length equal to wi. The Great Arya and Pari The Great were discussing about destructing some prefix (all road with numbers less than some x) and suffix (all roads with numbers greater than some x) of the roads so there will remain only the roads with numbers l, l + 1, ..., r - 1 and r.

    After that they will divide the great kingdom into two pieces (with each city belonging to exactly one piece) such that the hardness of the division is minimized. The hardness of a division is the maximum length of a road such that its both endpoints are in the same piece of the kingdom. In case there is no such road, the hardness of the division is considered to be equal to  - 1.

    Historians found the map of the great kingdom, and they have q guesses about the l and r chosen by those great rulers. Given these data, for each guess li and ri print the minimum possible hardness of the division of the kingdom.

    Input

    The first line of the input contains three integers nm and q (1 ≤ n, q ≤ 1000, ) — the number of cities and roads in the great kingdom, and the number of guesses, respectively.

    The i-th line of the following m lines contains three integers ui, vi and wi (1  ≤  ui,  vi  ≤  n, 0 ≤ wi ≤ 109), denoting the road number i connects cities ui and vi and its length is equal wi. It's guaranteed that no road connects the city to itself and no pair of cities is connected by more than one road.

    Each of the next q lines contains a pair of integers li and ri (1  ≤ li ≤ ri ≤ m) — a guess from the historians about the remaining roads in the kingdom.

    Output

    For each guess print the minimum possible hardness of the division in described scenario.

    Example

    Input
    5 6 5
    5 4 86
    5 1 0
    1 3 38
    2 1 33
    2 4 28
    2 3 40
    3 5
    2 6
    1 3
    2 3
    1 6
    Output
    -1
    33
    -1
    -1
    33

    题意:给定N点,M无向边。Q次询问,每次询问给出区间[L,R],现在需要把点集分为两个集合,求最小化集合内的最大边权。

    思路:对于这个区间[L,R],从大到小处理,那么就是求二分图,如果假如长度为X的边染色失败,则答案就是X,因为是一条一条的加边,用2-sat不方便,我们用并查集来解决二分图判定。

    (不过整体复杂度还是有些玄学)

    1<=x<=N表示一色,N+1<=x<=N+N表示二色,4320ms:

    #include<bits/stdc++.h>
    #define rep(i,a,b) for(int i=a;i<=b;i++)
    using namespace std;
    const int maxn=1010;
    struct in{ 
        int u,v,w,id;
        bool operator <(const in &x) const { return w>x.w;}
    };
    in a[maxn*maxn]; int fa[maxn<<1],p[maxn*maxn];
    int find(int x){
        if(x!=fa[x]) fa[x]=find(fa[x]); return fa[x]; 
    }
    int un(int x,int y){ int fx=find(x),fy=find(y); fa[fx]=fy; }
    int main()
    {
        int N,M,Q,L,R;
        scanf("%d%d%d",&N,&M,&Q);    
        rep(i,1,M) scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].w),a[i].id=i;
        sort(a+1,a+M+1);
        rep(i,1,Q){
            int ans=-1; scanf("%d%d",&L,&R);
            rep(j,1,N+N) fa[j]=j; 
            rep(j,1,M){
                if(a[j].id<L||a[j].id>R) continue;
                int f1=find(a[j].u),f2=find(a[j].v);
                if(f1==f2){ ans=a[j].w; break; }
                else un(a[j].u+N,a[j].v),un(a[j].u,a[j].v+N);
            }
            printf("%d
    ",ans);
        }
        return 0;
    }

    用带权并查集优化,点全部在1<=x<=N范围内,所以理论上复杂度会降低一半,dis表示点到根的距离奇偶性。2932ms。

    #include<bits/stdc++.h>
    #define rep(i,a,b) for(int i=a;i<=b;i++)
    using namespace std;
    const int maxn=1010;
    struct in{ 
        int u,v,w,id;
        bool operator <(const in &x) const { return w>x.w;}
    };
    in a[maxn*maxn]; int fa[maxn<<1],dis[maxn];
    int find(int x){
        if(x!=fa[x]){
            int fx=find(fa[x]);
            dis[x]^=dis[fa[x]];
            fa[x]=fx; 
        }
        return fa[x]; 
    }
    bool Union(int x,int y){
        int fx=find(x),fy=find(y);
        if(fx==fy){
            if(dis[x]==dis[y]) return false;
            return true;
        }
        fa[fx]=fy; dis[fx]=dis[x]^dis[y]^1;
        return true;
    }
    int main()
    {
        int N,M,Q,L,R;
        scanf("%d%d%d",&N,&M,&Q);    
        rep(i,1,M) scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].w),a[i].id=i;
        sort(a+1,a+M+1);
        rep(i,1,Q){
            int ans=-1; scanf("%d%d",&L,&R);
            rep(j,1,N) fa[j]=j,dis[j]=0; 
            rep(j,1,M){
                if(a[j].id<L||a[j].id>R) continue;
                if(!Union(a[j].u,a[j].v)) { ans=a[j].w; break; }
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    Halcon实例转OpenCV:计算回形针方向
    OpenCV常用图像拼接方法(四):基于Stitcher类拼接
    OpenCV常用图像拼接方法(三):基于特征匹配拼接
    OpenCV常用图像拼接方法(二):基于模板匹配拼接
    OpenCV常用图像拼接方法(一) :直接拼接
    OpenCV显示图像type位深度输出
    目标检测 缺陷检测 视觉项目开发定制
    餐盘识别/菜品识别 自动计费/自动计价 视觉项目开发定制
    TensorFlow OpenCV表情识别 毕业设计 毕设
    【转载】---手写LRU缓存算法
  • 原文地址:https://www.cnblogs.com/hua-dong/p/9559603.html
Copyright © 2011-2022 走看看