QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#242157#6559. A Tree and Two EdgesMovingUpWA 768ms11276kbC++143.0kb2023-11-06 23:45:302023-11-06 23:45:30

Judging History

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

  • [2023-11-06 23:45:30]
  • 评测
  • 测评结果:WA
  • 用时:768ms
  • 内存:11276kb
  • [2023-11-06 23:45:30]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;

const int LOG = 16;
int n, Q;
int dep[maxn], anc[maxn][LOG];
vector<int> adj[maxn];

void dfs(int v, int p = 0) {
	dep[v] = dep[p] + 1;
	anc[v][0] = p;
	for (int i = 1; i < LOG; i++) {
		if (anc[v][i - 1] > 0) {
			anc[v][i] = anc[anc[v][i - 1]][i - 1];
		}
	}

	for (int to : adj[v]) {
		if (to != p) {
			dfs(to, v);
		}
	}
}

int lca(int a, int b) {
	if (dep[a] > dep[b]) {
		swap(a, b);
	}
	for (int i = LOG - 1; i >= 0; i--) {
		if (dep[anc[b][i]] >= dep[a]) {
			b = anc[b][i];
		}
	}

	if (a == b) {
		return a;
	}

	for (int i = LOG - 1; i >= 0; i--) {
		if (anc[a][i] != anc[b][i]) {
			a = anc[a][i];
			b = anc[b][i];
		}
	}

	return anc[a][0];
}

bool contains(int a, int b, int x) {
	if (lca(a, x) == x && lca(x, b) == lca(a, b)) {
		return true;
	}
	if (lca(b, x) == x && lca(x, a) == lca(a, b)) {
		return true;
	}
	return false;
}

bool intersect(int a, int b, int c, int d) {
	return contains(a, b, c) || contains(a, b, d) || contains(c, d, a) || contains(c, d, b);
}

bool no_intersections(vector<pair<int, int>> edges) {
	vector<pair<int, int>> paths;
	for (int i = 1; i < edges.size(); i++) {
		paths.push_back({edges[i - 1].second, edges[i].first});
	}

	for (int i = 0; i < paths.size(); i++) {
		for (int j = 0; j < i; j++) {
			if (intersect(paths[i].first, paths[i].second, paths[j].first, paths[j].second)) {
				return false;
			}
		}
	}

	return true;
}

int par[maxn];
int root(int v) {
	return v == par[v] ? v : par[v] = root(par[v]);
}
bool unite(int a, int b) {
	a = root(a), b = root(b);
	if (a == b) {
		return false;
	}
	par[b] = a;
	return true;
}

int main()
{
	ios_base::sync_with_stdio(false), cin.tie(0);
	cin >> n >> Q;
	for (int i = 1; i <= n; i++) {
		par[i] = i;
	}
	vector<pair<int, int>> extra;
	for (int i = 0; i < n + 1; i++) {
		int a, b;
		cin >> a >> b;
		if (unite(a, b)) {
			adj[a].push_back(b);
			adj[b].push_back(a);
		} else {
			extra.push_back({a, b});
		}
	}

	dfs(1);

	for (int i = 0; i < Q; i++) {
		int a, b;
		cin >> a >> b;

		int ans = 0;
		vector<pair<int, int>> edges;
		edges.push_back({-1, a});
		for (int it = 0; it < 2; it++) {
			swap(extra[0], extra[1]);

			for (int w = it; w < 3; w++) {
				if (w == 1) {
					edges.push_back(extra[0]);
				} else if (w == 2) {
					edges.push_back({extra[0].second, extra[0].first});
				}

				for (int q = it; q < 3; q++) {
					if (q == 1) {
						edges.push_back({extra[1]});
					} else if (q == 2) {
						edges.push_back({extra[1].second, extra[1].first});
					}

					edges.push_back({b, -1});
					if (no_intersections(edges)) {
						ans++;
					}
					edges.pop_back();

					if (q != 0) {
						edges.pop_back();
					}
				}

				if (w != 0) {
					edges.pop_back();
				}
			}
		}

		cout << ans << "\n";
	}

	return 0;
}

详细

Test #1:

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

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: 1ms
memory: 6272kb

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
Wrong Answer
time: 768ms
memory: 11276kb

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:

13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
6
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
1...

result:

wrong answer 1st lines differ - expected: '4', found: '13'