QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#283448 | #6847. Hide-And-Seek Game | iee | AC ✓ | 948ms | 67300kb | C++17 | 1.9kb | 2023-12-14 17:21:12 | 2023-12-14 17:21:12 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
array<int, 3> exgcd(int a, int b) {
if (!b) return {a, 1, 0};
auto [g, y, x] = exgcd(b, a % b);
y -= a / b * x;
return {g, x, y};
}
int solve(int m1, int a1, int m2, int a2) {
a1 = (a1 % m1 + m1) % m1;
a2 = (a2 % m2 + m2) % m2;
auto [g, p, q] = exgcd(m1, m2);
assert(g);
if ((a2 - a1) % g) return 1e18;
p *= (a2 - a1) / g, q *= (a2 - a1) / g;
q *= -1;
while (p < 0 || q < 0) p += m2 / g, q += m1 / g;
while (p >= m2 / g) p -= m2 / g, q -= m1 / g;
assert(p * m1 + a1 == q * m2 + a2);
return p * m1 + a1;
}
void solve() {
int n, m;
cin >> n >> m;
vector<vector<int>> e(n);
for (int i = 0, u, v; i < n - 1; i++) {
cin >> u >> v;
u--;
v--;
e[u].push_back(v);
e[v].push_back(u);
}
vector<vector<int>> dis(n, vector<int>(n, -1));
for (int S = 0; S < n; S++) {
dis[S][S] = 0;
static queue<int> q;
q.push(S);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v : e[u]) {
if (dis[S][v] == -1) {
dis[S][v] = dis[S][u] + 1;
q.push(v);
}
}
}
}
while (m--) {
int sa, ta, sb, tb;
cin >> sa >> ta >> sb >> tb;
sa--, ta--, sb--, tb--;
int da = dis[sa][ta], db = dis[sb][tb], ans = 1e18, ansid = -2;
for (int i = 0; i < n; i++) {
if (dis[i][sa] + dis[i][ta] == da && dis[i][sb] + dis[i][tb] == db) {
int t = 1e18;
t = min(t, solve(da * 2, dis[i][sa], db * 2, dis[i][sb]));
t = min(t, solve(da * 2, da * 2 - dis[i][sa], db * 2, dis[i][sb]));
t = min(t, solve(da * 2, dis[i][sa], db * 2, db * 2 - dis[i][sb]));
t = min(t, solve(da * 2, da * 2 - dis[i][sa], db * 2, db * 2 - dis[i][sb]));
if (t < ans) {
ans = t;
ansid = i;
}
}
}
cout << ansid + 1 << "\n";
}
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 948ms
memory: 67300kb
input:
500 6 20 1 2 1 3 3 4 2 5 3 6 1 6 6 5 2 1 6 2 5 1 2 6 2 6 1 2 6 3 6 4 4 1 1 3 5 6 3 4 2 1 3 5 2 4 1 2 6 2 1 2 6 2 1 4 1 6 1 6 2 5 2 6 2 5 6 2 5 2 1 3 5 6 6 4 4 1 4 5 5 4 4 1 3 6 1 2 6 1 4 3 71 30 1 2 2 3 2 4 4 5 2 6 6 7 4 8 2 9 1 10 9 11 3 12 5 13 5 14 10 15 6 16 13 17 17 18 4 19 5 20 17 21 20 22 4 2...
output:
3 -1 -1 -1 6 3 -1 1 -1 1 3 1 2 -1 -1 3 4 1 -1 3 -1 11 2 -1 5 -1 17 -1 -1 5 -1 -1 -1 -1 2 -1 5 53 5 7 -1 -1 2 4 -1 -1 -1 -1 -1 -1 -1 5 -1 1 5 1 -1 -1 -1 -1 33 5 1 1 -1 1 -1 -1 -1 -1 7 -1 -1 9 5 3 -1 -1 -1 19 -1 -1 6 5 -1 -1 5 -1 -1 -1 -1 1 -1 7 -1 -1 1 -1 -1 -1 8 -1 13 -1 -1 -1 -1 4 5 -1 -1 5 -1 -1 8...
result:
ok 53199 lines