QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#308200#6559. A Tree and Two EdgesMizara#RE 0ms5664kbC++173.8kb2024-01-19 18:27:112024-01-19 18:27:11

Judging History

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

  • [2024-01-19 18:27:11]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:5664kb
  • [2024-01-19 18:27:11]
  • 提交

answer

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <math.h>
#include <utility>
const int OO = 0;
const int md = 998244353;
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<iii> viii;

struct dsu {
	int n;
	vector<int> p;
	dsu() {}
	dsu(int sz) {
		n = sz;
		p.resize(n);
		for (int i = 0; i < n; i++) p[i] = i;
	}
	int find(int x) {
		return x == p[x] ? x : p[x] = find(p[x]);
	}
	bool unite(int x, int y) {
		x = find(x), y = find(y);
		if (x == y) return false;
		if (rand() & 1) p[x] = y;
		else p[y] = x;
		return true;
	}
};

const int N = 50005, L = 16;

int n, q;
vector<vector<int>> g;
int dep[N];
int st[N], en[N];
int up[L][N];

vector<pair<int, int>> e;

int tim = 0;

void prep(int v, int prev, int d) {
	st[v] = tim++;
	dep[v] = d;
	up[0][v] = prev;
	for (const auto& i : g[v])
		if (i != prev)
			prep(i, v, d + 1);
	en[v] = tim++;
}

void prep_up() {
	for (int l = 1; l < L; l++) for (int i = 0; i < n; i++) {
		up[l][i] = up[l - 1][up[l - 1][i]];
	}
}

int jump(int u, int j) {
	if (j == 0) return u;
	for (int i = 0; i < L; i++) {
		if (j & (1 << i))
			u = up[i][u];
	}
	return u;
}

int lca(int u, int v) {
	if (u == v) return u;
	if (dep[u] < dep[v]) swap(u, v);
	u = jump(u, dep[u] - dep[v]);
	if (u == v) return u;
	for (int i = L - 1; i >= 0; i--) {
		if (up[i][u] != up[i][v]) {
			u = up[i][u];
			v = up[i][v];
		}
	}
	return up[u][0];
}

bool ancestor(int parent, int child) {
	return st[parent] <= st[child] && en[child] <= en[parent];
}

struct path {
	int u, v, l;
	path() {}
	path(int uu, int vv, int ll = -1) {
		u = uu;
		v = vv;
		l = ll;
	}
};

bool intersect(path& A, path& B) {
	if (A.l == -1)
		A.l = lca(A.u, A.v);
	if (B.l == -1)
		B.l = lca(B.u, B.v);
	if (dep[A.l] >= dep[B.l]) {
		// A.l is lower
		return ancestor(B.l, A.l) && (ancestor(A.l, B.u) || ancestor(A.l, B.v));
	}
	return ancestor(A.l, B.l) && (ancestor(B.l, A.u) || ancestor(B.l, A.v));
}

int f_one(int x, int y, int u, int v) {
	path xu(x, u), vy(v, y);
	if (intersect(xu, vy)) return 0;
	return 1;
}

int f_two(int x, int y, int u1, int v1, int u2, int v2) {
	path xu1(x, u1), v1u2(v1, u2), v2y(v2, y);
	if (intersect(xu1, v1u2) || intersect(xu1, v2y) || intersect(v1u2, v2y)) return 0;
	return 1;
}

void solve() {
	cin >> n >> q;
	dsu D(n);
	g.resize(n);
	for (int i = 0, u, v; i < n + 1; i++) {
		cin >> u >> v;
		--u, --v;
		if (D.unite(u, v)) {
			g[u].push_back(v);
			g[v].push_back(u);
		}
		else {
			e.emplace_back(u, v);
		}
	}
	prep(0, 0, 0);
	prep_up();
	while (q--) {
		int x, y;
		cin >> x >> y;
		--x, --y;
		int ans = 1; // none
		// only one
		ans += f_one(x, y, e[0].first, e[0].second);
		ans += f_one(x, y, e[0].second, e[0].first);
		ans += f_one(x, y, e[1].first, e[1].second);
		ans += f_one(x, y, e[1].second, e[1].first);
		// two
		ans += f_two(x, y, e[0].first, e[0].second, e[1].first, e[1].second);
		ans += f_two(x, y, e[0].first, e[0].second, e[1].second, e[1].first);
		ans += f_two(x, y, e[0].second, e[0].first, e[1].first, e[1].second);
		ans += f_two(x, y, e[0].second, e[0].first, e[1].second, e[1].first);
		swap(e[0], e[1]);
		ans += f_two(x, y, e[0].first, e[0].second, e[1].first, e[1].second);
		ans += f_two(x, y, e[0].first, e[0].second, e[1].second, e[1].first);
		ans += f_two(x, y, e[0].second, e[0].first, e[1].first, e[1].second);
		ans += f_two(x, y, e[0].second, e[0].first, e[1].second, e[1].first);
		swap(e[0], e[1]);
		cout << ans << '\n';
	}
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int tests = 1;
	//cin >> tests;
	while (tests--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 5660kb

input:

4 6
1 2
1 3
1 4
2 3
2 4
1 2
1 3
1 4
2 3
2 4
3 4

output:

3
3
3
3
3
4

result:

ok 6 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 5664kb

input:

6 4
1 2
1 3
1 6
2 3
3 4
3 5
4 5
1 2
1 3
1 4
1 6

output:

2
2
4
1

result:

ok 4 lines

Test #3:

score: -100
Runtime Error

input:

50000 50000
11561 23122
14261 28523
24407 48814
17947 35894
14803 29607
19557 39115
12415 24830
9583 19167
18397 36794
439 878
18040 36080
17150 34300
7922 15845
18938 37877
18625 37250
6459 12919
9818 19636
3579 7158
21323 42646
23882 47764
13603 27207
8353 16707
15907 31814
20935 41871
11686 23372...

output:


result: