给定一个图和很多次询问 x i , y i , z i x_i,y_i,z_i xi,yi,zi,问两个人分别从 x i , y i x_i,y_i xi,yi出发,一共经过了 z i z_i zi个不同的节点,需要经过的边的最大编号最小是多少。
裸的整体二分题。
用并查集维护,查一下 x i , y i x_i,y_i xi,yi所在联通块大小即可。
/************************************************
* Au: Hany01
* Date: Oct 11th, 2018
* Prob: agc002d
* Email: [email protected] & [email protected]
* Inst: Yali High School
************************************************/
#include
using namespace std;
typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
#define Rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define X first
#define Y second
#define PB(a) push_back(a)
#define MP(a, b) make_pair(a, b)
#define SZ(a) ((int)(a).size())
#define ALL(a) a.begin(), a.end()
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia
template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
template <typename T> inline T read() {
static T _, __; static char c_;
for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
//EOT
const int maxn = 1e5 + 5;
int n, m, q;
PII E[maxn];
struct Query {
int x, y, z, id;
}qry[maxn], tmp[maxn];
int ans[maxn];
struct DSU {
int fa[maxn], sz[maxn];
inline void init(int n) { For(i, 1, n) fa[i] = i, sz[i] = 1; }
inline int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
inline bool merge(int x, int y) {
return (x = find(x)) != (y = find(y)) ? fa[x] = y, sz[y] += sz[x], 1 : 0; }
inline int query(int x, int y) {
if ((x = find(x)) != (y = find(y))) return sz[x] + sz[y];
return sz[x];
}
}dsu[21];
void solve(int ql, int qr, int vl, int vr, int dep) {
if (vl == vr) {
dsu[dep].merge(E[vl].X, E[vl].Y);
For(i, ql, qr) ans[qry[i].id] = vl;
return;
}
int mid = (vl + vr) >> 1, cur1 = ql - 1, cur2 = qr + 1;
For(i, vl, mid) dsu[dep].merge(E[i].X, E[i].Y);
For(i, ql, qr)
if (dsu[dep].query(qry[i].x, qry[i].y) >= qry[i].z) tmp[++ cur1] = qry[i];
else tmp[-- cur2] = qry[i];
For(i, mid + 1, vr) dsu[dep].merge(E[i].X, E[i].Y);
For(i, ql, qr) qry[i] = tmp[i];
solve(ql, cur1, vl, mid, dep + 1);
solve(cur2, qr, mid + 1, vr, dep + 1);
}
int main()
{
#ifdef hany01
freopen("agc002d.in", "r", stdin);
freopen("agc002d.out", "w", stdout);
#endif
n = read<int>(), m = read<int>();
For(i, 1, m) E[i].X = read<int>(), E[i].Y = read<int>();
q = read<int>();
For(i, 1, q) qry[i].x = read<int>(), qry[i].y = read<int>(), qry[i].z = read<int>(), qry[i].id = i;
For(i, 0, 20) dsu[i].init(n);
solve(1, q, 1, m, 0);
For(i, 1, q) printf("%d\n", ans[i]);
return 0;
}