zoukankan      html  css  js  c++  java
  • Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 暴力并查集

    D. Dividing Kingdom II

    题目连接:

    http://www.codeforces.com/contest/687/problem/D

    Description

    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 n, m 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.

    Sample 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

    Sample Output

    -1
    33
    -1
    -1
    33

    Hint

    题意

    给你一个图

    然后给你n,m,q

    表示这个图有n个点,m条边,一共q次询问

    每次询问给你l,r

    然后用l,r以内的边,去构成的一个图集合。

    把这个集合的点分成两个部分,然后这个图的hardness,定义为这个图里面的最长边,且这个边左右连的城市相同

    你想让这个边的大小尽量小,问是多少。

    题解

    如果只有一个询问的话,很容易想到是把边从大到小排序,然后贪心去扔到并查集看一看就好了,这样就是一个傻逼题。

    然后他有Q次询问,感觉上来说好像是个莫队。

    但是CF机器很快,所以就没必要写莫队了,直接NQ暴力去写这个就好了。

    代码

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 1e3+6;
    struct node
    {
        int a,b,c,d;
    }e[maxn*maxn];
    bool cmp(node a,node b){
        return a.c>b.c;
    }
    int fa[maxn*2];
    int fi(int x){
        if(fa[x]==x)return x;
        return fa[x]=fi(fa[x]);
    }
    int uni(int x,int y){
        fa[fi(x)]=fi(y);
    }
    int main(){
        int n,m,q;
        scanf("%d%d%d",&n,&m,&q);
        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&e[i].a,&e[i].b,&e[i].c);
            e[i].d=i;
        }
        sort(e+1,e+1+m,cmp);
        while(q--){
            int l,r;
            scanf("%d%d",&l,&r);
            for(int i=1;i<=2*n;i++)
                fa[i]=i;
            int flag=0;
            for(int i=1;i<=m;i++){
                if(e[i].d<=r&&e[i].d>=l){
                    if(fi(e[i].a)==fi(e[i].b)){
                        flag=1;
                        printf("%d
    ",e[i].c);
                        break;
                    }
                    else{
                        uni(e[i].a,e[i].b+n);
                        uni(e[i].b,e[i].a+n);
                    }
                }
            }
            if(!flag)printf("-1
    ");
        }
    }
  • 相关阅读:
    springboot+mybatis集成多数据源MySQL/Oracle/SqlServer
    向Spring容器中注册组件的方法汇总小结
    使用spring initialization创建SpringBoot项目
    mybatis-generator 的坑我都走了一遍
    初识Jsp,JavaBean,Servlet以及一个简单mvc模式的登录界面
    【Linux】用户只显示$问题
    【Linux】Ubuntu创建用户、删除用户、设置用户密码,root和普通用户切换
    【linux】ubuntu安装ssh
    【整理】【JS】map的基本操作
    【整理】【JS】数组定义、添加、删除、替换、遍历基本操作
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5631281.html
Copyright © 2011-2022 走看看