QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#189475#6847. Hide-And-Seek Gamespoonjunxi#AC ✓93ms3936kbC++142.7kb2023-09-27 15:49:542023-09-27 15:49:56

Judging History

你现在查看的是最新测评结果

  • [2023-09-27 15:49:56]
  • 评测
  • 测评结果:AC
  • 用时:93ms
  • 内存:3936kb
  • [2023-09-27 15:49:54]
  • 提交

answer

#include <bits/stdc++.h>
#define rep(i, j, k) for (int i = (j); i <= (k); i++)
#define per(i, j, k) for (int i = (k); i >= (j); i--)
#define pb push_back
#define eb emplace_back
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
using VI = std::vector<int>;
using ll = long long;
using PII = std::pair<int, int>;
using db = double;
using namespace std;

const int LOGN = 12, N = 3010;

int n, m, dep[N], p[N][13], d1[N], d2[N];
VI adj[N];

void dfs(int u, int f = 0) {
	p[u][0] = f;
	dep[u] = dep[f] + 1;
	for (auto v : adj[u]) if (v != f) {
		dfs(v, u);
	}
}

int lca(int u, int v) {
	if (dep[u] > dep[v]) swap(u, v);
	per(i, 0, LOGN) if (dep[p[v][i]] >= dep[u]) v = p[v][i];
	if (u == v) return u;
	per(i ,0 ,LOGN) if (p[v][i] != p[u][i]) u = p[u][i], v = p[v][i];
	return p[u][0];
}

ll exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) {
		x = 1; y = 0;
		return a;
	}
	ll z, d;
	d = exgcd(b, a % b, z, x);
	y = z - a / b * x;
	return d;
}

ll excrt(ll m1, ll r1, ll m2, ll r2) {
	ll w = r2 - r1;
	ll x, y;
	ll d = exgcd(m1, m2, x, y);
	if (w % d) return 1e18;
	w /= d;
	ll rmod = m2 / d;
	x = (x * w % rmod + rmod) % rmod;
	r1 += x * m1;
	m1 *= rmod;
	return r1;
}

void solve() {
	cin >> n >> m;
	rep(i, 1, n) adj[i].clear();
	rep(i, 1, n - 1) {
		int u, v;
		cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
	}

	dfs(1);

	rep(j, 1, LOGN) {
		rep(i, 1, n) {
			p[i][j] = p[p[i][j - 1]][j - 1];
		}
	}

	auto get = [&](int s, int t) {
		int w = lca(s, t);
		vector<int> up, down;
		while (s != w) {
			up.pb(s);
			s = p[s][0];
		}
		up.pb(w);
		while (t != w) {
			down.pb(t);
			t = p[t][0];
		}
		reverse(all(down));
		for (auto v : down) up.pb(v);
		return up;
	};

	rep(_, 1, m) {
		int s1, t1, s2, t2;
		cin >> s1 >> t1 >> s2 >> t2;
		auto p1 = get(s1, t1), p2 = get(s2, t2);
		int dd = 0;
		rep(i, 1, n) d1[i] = d2[i] = -1;
		for (auto v : p1) {
			d1[v] = dd++;
		}
		dd = 0;
		for (auto v : p2) {
			d2[v] = dd++;
		}
		int M1 = 2 * (SZ(p1) - 1), M2 = 2 * (SZ(p2) - 1);
		int ans = -1;
		ll anst = 1e18;
		rep(i, 1, n) if (~d1[i] && ~d2[i]) {
			for (auto x : {M1 - d1[i], d1[i]}) {
				for (auto y : {M2 - d2[i], d2[i]}) {
					ll t = excrt(M1, x % M1, M2, y % M2);
					// cerr << "eq : " << x % M1 << " " << M1 << " " << y % M2 << " " << M2 << "\n";
					// cerr << "ff : " << i << " " << t << "\n";
					if (t < anst) {
						anst = t;
						ans = i;
					//	cerr << "ff : " << i << "\n";
					}
				}
			}
		}
		cout << ans << "\n";
	}
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);

	int t;
	cin >> t;
	while (t--) {
		solve();
	}
}

詳細信息

Test #1:

score: 100
Accepted
time: 93ms
memory: 3936kb

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