#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cctype>
using namespace std;
int Read() {
char c;
while (c = getchar(), !isdigit(c));
int x = c - 48;
while (c = getchar(), isdigit(c)) x = x * 10 + c - 48;
return x;
}
void Write(int x) {
int a[10], k = 0;
do a[++k] = x % 10;
while (x /= 10);
while (k) putchar(a[k--] + 48);
putchar('\n');
return;
}
typedef pair<int, int> PII;
#define fi first
#define se second
#define mp make_pair
const int N = 200010, M = 400010;
int n, m, q, k, s;
int tot, head[N];
struct Edge {
int p, nxt, w;
Edge(int p = 0, int nxt = 0, int w = 0) : p(p), nxt(nxt), w(w) {}
} edge[M * 2];
inline void Add(int u, int v, int w) {
edge[++tot] = Edge(v, head[u], w);
head[u] = tot;
return;
}
void Init() {
tot = -1;
memset(head, -1, sizeof(head));
return;
}
struct Node {
int u, v, w;
bool operator < (const Node &a) const {
return w < a.w;
}
} a[M];
vector<int> va;
priority_queue<PII> pq;
int d[N];
void Dijkstra() {
memset(d, 0x7f, sizeof(d));
pq.push(mp(-(d[1] = 0), 1));
while (!pq.empty()) {
PII cur = pq.top();
pq.pop();
int u = cur.se;
if (d[u] < -cur.fi) continue;
for (int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].p, w = edge[i].w;
if (d[v] > d[u] + w) pq.push(mp(-(d[v] = d[u] + w), v));
}
}
return;
}
namespace ST {
int tot;
struct Node {
int lc, rc, l, r, t;
} p[N * 4 + M * 40];
void Init() {
tot = 0;
return;
}
void Build(int &k, int l, int r) {
k = ++tot;
p[k].l = l;
p[k].r = r;
if (l == r) {
p[k].t = -d[l];
return;
}
int m = (l + r) / 2;
Build(p[k].lc, l, m);
Build(p[k].rc, m + 1, r);
return;
}
void Modify(int &k, int _k, int x, int t) {
p[k = ++tot] = p[_k];
if (p[k].l == p[k].r) {
p[k].t = t;
return;
}
if (x <= p[p[k].lc].r) Modify(p[k].lc, p[_k].lc, x, t);
else Modify(p[k].rc, p[_k].rc, x, t);
return;
}
int Query(int k, int x) {
while (p[k].l != p[k].r)
k = (x <= p[p[k].lc].r) ? (p[k].lc) : (p[k].rc);
return p[k].t;
/*
if (p[k].l == p[k].r) return p[k].t;
if (x <= p[p[k].lc].r) return Query(p[k].lc, x);
return Query(p[k].rc, x);
*/
}
}
int Gf(int v, int k) {
int t = ST::Query(v, k);
return (t <= 0) ? -t : Gf(v, t);
}
int f[N], size[N];
int Gf(int k) {
return (f[k] != k) ? (f[k] = Gf(f[k])) : k;
}
int root[M];
void Solve() {
scanf("%d%d", &n, &m);
Init();
va.clear();
for (int i = 1; i <= m; ++i) {
int u = Read(), v = Read(), l = Read(), a = Read();
//int u, v, l, a;
//scanf("%d%d%d%d", &u, &v, &l, &a);
Add(u, v, l);
Add(v, u, l);
::a[i].u = u;
::a[i].v = v;
::a[i].w = a;
va.push_back(a);
}
sort(a + 1, a + m + 1);
sort(va.begin(), va.end());
va.erase(unique(va.begin(), va.end()), va.end());
Dijkstra();
int t = va.size();
ST::Init();
ST::Build(root[t], 1, n);
for (int i = 1; i <= n; ++i) {
f[i] = i;
size[i] = 1;
}
for (int i = t - 1, j = m; ~i; --i) {
root[i] = root[i + 1];
for (; j && a[j].w >= va[i]; --j) {
int x = Gf(a[j].u), y = Gf(a[j].v);
if (x != y) {
if (size[x] < size[y]) swap(x, y);
f[y] = x;
size[x] += size[y];
d[x] = min(d[x], d[y]);
ST::Modify(root[i], root[i], y, x);
ST::Modify(root[i], root[i], x, -d[x]);
}
}
}
int q, k, s, la = 0;
scanf("%d%d%d", &q, &k, &s);
while (q--) {
int v = Read(), p = Read();
//int v, p;
//scanf("%d%d", &v, &p);
v = (v + k * la - 1) % n + 1;
p = (p + k * la) % (s + 1);
int j = upper_bound(va.begin(), va.end(), p) - va.begin();
la = Gf(root[j], v);
Write(la);
//printf("%d\n", la);
}
return;
}
int main() {
int t;
scanf("%d", &t);
while (t--) Solve();
return 0;
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 1.512 ms | 10 MB + 740 KB | Accepted | Score: 5 | 显示更多 |
Testcase #2 | 1.522 ms | 10 MB + 744 KB | Accepted | Score: 5 | 显示更多 |
Testcase #3 | 1.615 ms | 10 MB + 764 KB | Accepted | Score: 5 | 显示更多 |
Testcase #4 | 1.727 ms | 10 MB + 792 KB | Accepted | Score: 5 | 显示更多 |
Testcase #5 | 4.928 ms | 11 MB + 580 KB | Accepted | Score: 5 | 显示更多 |
Testcase #6 | 962.464 ms | 171 MB + 960 KB | Accepted | Score: 5 | 显示更多 |
Testcase #7 | 4.965 ms | 11 MB + 548 KB | Accepted | Score: 5 | 显示更多 |
Testcase #8 | 4.971 ms | 11 MB + 552 KB | Accepted | Score: 5 | 显示更多 |
Testcase #9 | 4.98 ms | 11 MB + 548 KB | Accepted | Score: 5 | 显示更多 |
Testcase #10 | 1.249 s | 169 MB + 736 KB | Accepted | Score: 5 | 显示更多 |
Testcase #11 | 1.247 s | 169 MB + 748 KB | Accepted | Score: 5 | 显示更多 |
Testcase #12 | 1.23 s | 171 MB + 708 KB | Accepted | Score: 5 | 显示更多 |
Testcase #13 | 1.233 s | 173 MB + 192 KB | Accepted | Score: 5 | 显示更多 |
Testcase #14 | 1.233 s | 173 MB + 216 KB | Accepted | Score: 5 | 显示更多 |
Testcase #15 | 6.649 ms | 11 MB + 584 KB | Accepted | Score: 5 | 显示更多 |
Testcase #16 | 6.609 ms | 11 MB + 588 KB | Accepted | Score: 5 | 显示更多 |
Testcase #17 | 1.234 s | 173 MB + 380 KB | Accepted | Score: 5 | 显示更多 |
Testcase #18 | 1.235 s | 173 MB + 316 KB | Accepted | Score: 5 | 显示更多 |
Testcase #19 | 2.36 s | 175 MB + 120 KB | Accepted | Score: 5 | 显示更多 |
Testcase #20 | 2.351 s | 176 MB + 912 KB | Accepted | Score: 5 | 显示更多 |