题目链接:见这里
D. Dividing Kingdom II
time limit per test
6 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.
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。表示这个图有n个点,m条边,一共q次询问。每次询问给你l,r。然后用l,r以内的边,去构成的一个图集合。把这个集合的点分成两个部分,然后这个图的hardness,定义为这个图里面的最长边,且这个边左右连的城市相同。你想让这个边的大小尽量小,问是多少。

解法1:
如果只有一个询问的话,很容易想到是把边从大到小排序,然后贪心去扔到并查集看一看就好了。但是这里有Q个询问,但是注意到这里q比较小,直接O(q*m)复杂度应该是足够的,所以我们直接对每个询问按照这种思路去做就可以了。第一种判断二分图,是新建N个点和原来的N个点构成二分图的左右子图去判断,这个在纸上画一画就明白了。

//CF 687D O(n*m)
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
int n, m, q;
struct edge{
    int u, v, w, id;
    edge(){}
    edge(int u, int v, int w, int id):u(u), v(v), w(w), id(id) {}
    bool operator < (const edge &rhs) const{
        return w > rhs.w;
    }
}E[maxn*maxn];
namespace dsu{
    int fa[maxn*2];
    inline void init(){for(int i = 1; i <= 2*n; i++) fa[i] = i;}
    inline int find_set(int x){if(x == fa[x]) return x; return fa[x] = find_set(fa[x]);}
    inline void union_set(int x, int y){int fx = find_set(x), fy = find_set(y); if(fx != fy) fa[fx] = fy;}
}
using namespace dsu;
int main()
{
    scanf("%d%d%d", &n, &m, &q);
    for(int i = 1; i <= m; i++){
        scanf("%d%d%d", &E[i].u, &E[i].v, &E[i].w); E[i].id = i;
    }
    sort(E + 1, E + m + 1);
    while(q--)
    {
        int l, r, ans = -1;
        scanf("%d%d", &l, &r);
        init();
        for(int i = 1; i <= m; i++){
            if(E[i].id < l || E[i].id > r) continue;
            if(find_set(E[i].u) == find_set(E[i].v)){
                ans = E[i].w;
                break;
            }
            else{
                union_set(E[i].u, E[i].v + n);
                union_set(E[i].u + n, E[i].v);
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

解法2: 思路和上面一样,只不过是用带权并查集来实现的。具体是这样的。

    1:设计权值数组relation[i],代表i节点和它的根的关系,0代表属于一个部分,1代表不属于一个部分

   2:路径压缩,relation[i]=relation[i]^relation[fa[i]],递归得到和根的关系

   3:合并根节点,relation[i]=relation[u]^relation[v]^1;
//CF 687D O(n*m)
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5+7;
int n, m, q, fa[maxn], relation[maxn];
struct edge{
    int u, v, w, id;
    edge(){}
    edge(int u, int v, int w, int id) : u(u), v(v), w(w), id(id){}
    bool operator < (const edge &rhs) const{
        return w > rhs.w;
    }
}E[maxn*2];
int find_set(int x){
    if(x == fa[x]) return x;
    int fx = find_set(fa[x]);
    relation[x] ^= relation[fa[x]];
    return fa[x]= fx;
}
bool union_set(int x, int y){
    int fx = find_set(x), fy = find_set(y);
    if(fx == fy){
        if(relation[x] == relation[y]) return false;
        return true;
    }
    fa[fx] = fy;
    relation[fx] = relation[x] ^ relation[y] ^ 1;
    return true;
}
int main()
{
    scanf("%d%d%d", &n, &m, &q);
    for(int i = 1; i <= m; i++){
        scanf("%d%d%d", &E[i].u, &E[i].v, &E[i].w); E[i].id = i;
    }
    sort(E + 1, E + m + 1);
    while(q--)
    {
        int l, r, ans = -1;
        scanf("%d%d", &l, &r);
        for(int i = 1; i <= n; i++) fa[i] = i, relation[i] = 0;
        for(int i = 1; i <= m; i++){
            if(E[i].id < l || E[i].id > r) continue;
            if(!union_set(E[i].u, E[i].v)){
                ans = E[i].w;
                break;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

可以看出两种方式的复杂度一样,但是少了带权并查集的关系合并,下面的代码比上面要快800ms。