답안 #781428

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
781428 2023-07-13T06:14:11 Z 박영우(#10014) 도로 개발 (JOI15_road_development) C++17
100 / 100
248 ms 39532 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define MAX 301010
#define MAXS 22
#define INF 1000000000000000001
#define bb ' '
#define ln '\n'
#define Ln '\n'
#define MOD 1000000007
vector<int> adj[MAX];
struct dsu {
	vector<int> p;
	int N;
	dsu(int N) :N(N) {
		p.resize(N + 1);
		int i;
		for (i = 1; i <= N; i++) p[i] = i;
	}
	int find(int a) {
		if (p[a] == a) return a;
		return p[a] = find(p[a]);
	}
	void uni(int a, int b) {
		a = find(a);
		b = find(b);
		p[b] = a;
	}
	int operator[](int x) { return find(x); }
};
struct fenwick {
	int N;
	vector<int> tree;
	fenwick(int N) :N(N) { tree.resize(N + 1); }
	void upd(int i, int x) { while (i <= N) { tree[i] += x, i += i & -i; } }
	int get(int i) { int ans = 0; while (i) { ans += tree[i]; i -= i & -i; } return ans; }
	void upd(int l, int r, int x) { upd(l, x), upd(r + 1, -x); }
};
int T[MAX];
int A[MAX];
int B[MAX];
int sp[MAX][MAXS];
int dep[MAX];
int vis[MAX];
pii range[MAX];
int prt[MAX];
int cnt;
void dfs(int x, int p = 0) {
	prt[x] = p;
	cnt++;
	range[x] = pii(cnt, cnt);
	vis[x] = 1;
	sp[x][0] = p;
	for (int i = 1; i < MAXS; i++) sp[x][i] = sp[sp[x][i - 1]][i - 1];
	for (auto v : adj[x]) if (v != p) {
		dep[v] = dep[x] + 1;
		dfs(v, x);
		range[x].second = max(range[x].second, range[v].second);
	}
}
int lca(int u, int v) {
	int i;
	if (dep[u] != dep[v]) {
		if (dep[u] > dep[v]) swap(u, v);
		int d = dep[v] - dep[u];
		for (i = 0; i < MAXS; i++) if (d >> i & 1) v = sp[v][i];
	}
	if (u == v) return u;
	for (i = MAXS - 1; i >= 0; i--) if (sp[u][i] != sp[v][i]) u = sp[u][i], v = sp[v][i];
	return sp[u][0];
}
signed main() {
	ios::sync_with_stdio(false), cin.tie(0);
	int N, Q;
	cin >> N >> Q;
	int i;
	dsu d = dsu(N);
	for (i = 1; i <= Q; i++) {
		cin >> T[i] >> A[i] >> B[i];
		if (T[i] == 1) {
			if (d[A[i]] != d[B[i]]) {
				d.uni(A[i], B[i]);
				adj[A[i]].push_back(B[i]);
				adj[B[i]].push_back(A[i]);
				T[i] = 3;
			}
		}
	}
	fenwick fen(N);
	for (i = 1; i <= N; i++) if (!vis[i]) dfs(i);
	for (i = 1; i <= N; i++) fen.upd(range[i].first, range[i].second, 1);
	dsu dd = d = dsu(N);
	for (i = 1; i <= Q; i++) {
		if (T[i] == 1) {
			int l = dd[lca(A[i], B[i])];
			for (auto v : { A[i], B[i] }) {
				v = dd[v];
				while (v != l) {
					int p = dd[prt[v]];
					fen.upd(range[v].first, range[v].second, -1);
					dd.uni(p, v);
					v = p;
				}
			}
		}
		if (T[i] == 2) {
			if (d[A[i]] != d[B[i]]) cout << -1 << ln;
			else cout << fen.get(range[A[i]].first) + fen.get(range[B[i]].first) - fen.get(range[lca(A[i], B[i])].first) * 2 << ln;
		}
		if (T[i] == 3) d.uni(A[i], B[i]);
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7636 KB Output is correct
2 Correct 4 ms 7636 KB Output is correct
3 Correct 5 ms 7636 KB Output is correct
4 Correct 5 ms 7716 KB Output is correct
5 Correct 5 ms 7636 KB Output is correct
6 Correct 4 ms 7636 KB Output is correct
7 Correct 5 ms 7636 KB Output is correct
8 Correct 5 ms 7636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 123 ms 26888 KB Output is correct
2 Correct 149 ms 26184 KB Output is correct
3 Correct 223 ms 32832 KB Output is correct
4 Correct 189 ms 37008 KB Output is correct
5 Correct 186 ms 34756 KB Output is correct
6 Correct 193 ms 34884 KB Output is correct
7 Correct 164 ms 30148 KB Output is correct
8 Correct 224 ms 34456 KB Output is correct
9 Correct 140 ms 29660 KB Output is correct
10 Correct 181 ms 33288 KB Output is correct
11 Correct 175 ms 35900 KB Output is correct
12 Correct 168 ms 39180 KB Output is correct
13 Correct 192 ms 39532 KB Output is correct
14 Correct 233 ms 35320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 131 ms 26580 KB Output is correct
2 Correct 156 ms 26172 KB Output is correct
3 Correct 206 ms 31296 KB Output is correct
4 Correct 219 ms 37936 KB Output is correct
5 Correct 175 ms 30196 KB Output is correct
6 Correct 229 ms 35004 KB Output is correct
7 Correct 144 ms 30464 KB Output is correct
8 Correct 184 ms 33972 KB Output is correct
9 Correct 192 ms 34508 KB Output is correct
10 Correct 232 ms 36252 KB Output is correct
11 Correct 131 ms 30076 KB Output is correct
12 Correct 191 ms 36540 KB Output is correct
13 Correct 198 ms 36832 KB Output is correct
14 Correct 160 ms 35780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 85 ms 25204 KB Output is correct
2 Correct 110 ms 24756 KB Output is correct
3 Correct 145 ms 29316 KB Output is correct
4 Correct 144 ms 33944 KB Output is correct
5 Correct 150 ms 29648 KB Output is correct
6 Correct 168 ms 36404 KB Output is correct
7 Correct 58 ms 24948 KB Output is correct
8 Correct 95 ms 30548 KB Output is correct
9 Correct 81 ms 33152 KB Output is correct
10 Correct 85 ms 27776 KB Output is correct
11 Correct 155 ms 33732 KB Output is correct
12 Correct 170 ms 34268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7636 KB Output is correct
2 Correct 4 ms 7636 KB Output is correct
3 Correct 5 ms 7636 KB Output is correct
4 Correct 5 ms 7716 KB Output is correct
5 Correct 5 ms 7636 KB Output is correct
6 Correct 4 ms 7636 KB Output is correct
7 Correct 5 ms 7636 KB Output is correct
8 Correct 5 ms 7636 KB Output is correct
9 Correct 123 ms 26888 KB Output is correct
10 Correct 149 ms 26184 KB Output is correct
11 Correct 223 ms 32832 KB Output is correct
12 Correct 189 ms 37008 KB Output is correct
13 Correct 186 ms 34756 KB Output is correct
14 Correct 193 ms 34884 KB Output is correct
15 Correct 164 ms 30148 KB Output is correct
16 Correct 224 ms 34456 KB Output is correct
17 Correct 140 ms 29660 KB Output is correct
18 Correct 181 ms 33288 KB Output is correct
19 Correct 175 ms 35900 KB Output is correct
20 Correct 168 ms 39180 KB Output is correct
21 Correct 192 ms 39532 KB Output is correct
22 Correct 233 ms 35320 KB Output is correct
23 Correct 131 ms 26580 KB Output is correct
24 Correct 156 ms 26172 KB Output is correct
25 Correct 206 ms 31296 KB Output is correct
26 Correct 219 ms 37936 KB Output is correct
27 Correct 175 ms 30196 KB Output is correct
28 Correct 229 ms 35004 KB Output is correct
29 Correct 144 ms 30464 KB Output is correct
30 Correct 184 ms 33972 KB Output is correct
31 Correct 192 ms 34508 KB Output is correct
32 Correct 232 ms 36252 KB Output is correct
33 Correct 131 ms 30076 KB Output is correct
34 Correct 191 ms 36540 KB Output is correct
35 Correct 198 ms 36832 KB Output is correct
36 Correct 160 ms 35780 KB Output is correct
37 Correct 85 ms 25204 KB Output is correct
38 Correct 110 ms 24756 KB Output is correct
39 Correct 145 ms 29316 KB Output is correct
40 Correct 144 ms 33944 KB Output is correct
41 Correct 150 ms 29648 KB Output is correct
42 Correct 168 ms 36404 KB Output is correct
43 Correct 58 ms 24948 KB Output is correct
44 Correct 95 ms 30548 KB Output is correct
45 Correct 81 ms 33152 KB Output is correct
46 Correct 85 ms 27776 KB Output is correct
47 Correct 155 ms 33732 KB Output is correct
48 Correct 170 ms 34268 KB Output is correct
49 Correct 131 ms 30388 KB Output is correct
50 Correct 155 ms 30208 KB Output is correct
51 Correct 200 ms 36496 KB Output is correct
52 Correct 217 ms 39332 KB Output is correct
53 Correct 116 ms 29252 KB Output is correct
54 Correct 113 ms 29252 KB Output is correct
55 Correct 170 ms 35076 KB Output is correct
56 Correct 173 ms 36888 KB Output is correct
57 Correct 195 ms 34184 KB Output is correct
58 Correct 191 ms 34208 KB Output is correct
59 Correct 248 ms 34784 KB Output is correct
60 Correct 190 ms 36108 KB Output is correct
61 Correct 169 ms 36036 KB Output is correct