Submission #786681

# Submission time Handle Problem Language Result Execution time Memory
786681 2023-07-18T11:13:29 Z qwerasdfzxcl JOI15_road_development (JOI15_road_development) C++17
100 / 100
605 ms 31796 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

struct DSU{
	int path[100100];
	void init(int n){
		for (int i=1;i<=n;i++) path[i] = i;
	}

	int find(int s){
		if (s==path[s]) return s;
		return path[s] = find(path[s]);
	}

	bool merge(int s, int v){
		s = find(s), v = find(v);
		if (s==v) return false;
		path[v] = s;
		return true;
	}
}dsu;

struct Seg{
	int tree[400400], lazy[400400];

	void init(int i, int l, int r){
		lazy[i] = 0;
		if (l==r){tree[i] = 1; return;}

		int m = (l+r)>>1;
		init(i<<1, l, m); init(i<<1|1, m+1, r);
		tree[i] = tree[i<<1] + tree[i<<1|1];
	}

	void propagate(int i, int l, int r){
		if (lazy[i]) tree[i] = 0;
		else return;

		if (l!=r){
			lazy[i<<1] = 1;
			lazy[i<<1|1] = 1;
		}

		lazy[i] = 0;
		return;
	}

	void update(int i, int l, int r, int s, int e){
		propagate(i, l, r);
		if (r<s || e<l) return;
		if (s<=l && r<=e){
			lazy[i] = 1;
			propagate(i, l, r);
			return;
		}

		int m = (l+r)>>1;
		update(i<<1, l, m, s, e); update(i<<1|1, m+1, r, s, e);
		tree[i] = tree[i<<1] + tree[i<<1|1];
	}

	int query(int i, int l, int r, int s, int e){
		propagate(i, l, r);
		if (r<s || e<l) return 0;
		if (s<=l && r<=e) return tree[i];

		int m = (l+r)>>1;
		return query(i<<1, l, m, s, e) + query(i<<1|1, m+1, r, s, e);
	}
}tree;

vector<int> adj[100100], G[100100];
int dep[100100], par[100100], top[100100], sz[100100], in[100100], out[100100], cnt, n;
int T[300300], X[300300], Y[300300];

void dfs0(int s, int pa = 0){
	par[s] = pa;
	dep[s] = dep[pa] + 1;
	sz[s] = 1;
	for (auto &v:adj[s]) if (v!=pa){
		G[s].push_back(v);
		dfs0(v, s);

		sz[s] += sz[v];
	}
}

void dfs1(int s){
	in[s] = ++cnt;

	if (!G[s].empty()) swap(G[s][0], *max_element(G[s].begin(), G[s].end(), [&](int x, int y){
		return sz[x] < sz[y];
	}));
	
	for (auto &v:G[s]){
		if (v==G[s][0]) top[v] = top[s];
		else top[v] = v;
		dfs1(v);
	}

	out[s] = cnt;
}

int query(int x, int y, int t){
	// printf("query %d %d / %d %d / %d %d\n", x, y, in[x], in[y], top[x], top[y]);
	int ret = 0;

	while(top[x]!=top[y]){
		if (dep[top[x]] > dep[top[y]]) swap(x, y);
		ret += tree.query(1, 1, n, in[top[y]], in[y]);
		if (t==1) tree.update(1, 1, n, in[top[y]], in[y]);
		y = par[top[y]];
	}

	if (dep[x] > dep[y]) swap(x, y);
	// printf("ok %d to %d\n", in[x]+1, in[y]);
	ret += tree.query(1, 1, n, in[x]+1, in[y]);
	if (t==1) tree.update(1, 1, n, in[x]+1, in[y]);

	return ret;
}

int main(){
	int q;
	scanf("%d %d", &n, &q);

	dsu.init(n);
	for (int i=1;i<=q;i++){
		scanf("%d %d %d", T+i, X+i, Y+i);
		if (T[i]==1){
			if (dsu.merge(X[i], Y[i])){
				adj[X[i]].push_back(Y[i]);
				adj[Y[i]].push_back(X[i]);
			}
		}
	}

	vector<int> root;
	for (int i=1;i<=n;i++) if (!par[i]){
		root.push_back(i);
		dfs0(i);
	}

	for (auto &r:root){
		top[r] = r;
		dfs1(r);
	}

	tree.init(1, 1, n);

	dsu.init(n);
	for (int i=1;i<=q;i++){
		if (dsu.find(X[i])!=dsu.find(Y[i])){
			if (T[i]==2){
				printf("-1\n");
			}
			else{
				dsu.merge(X[i], Y[i]);
			}
		}

		else{
			int ans = query(X[i], Y[i], T[i]);
			if (T[i]==2) printf("%d\n", ans);
		}
	}
}

Compilation message

road_development.cpp: In function 'int main()':
road_development.cpp:127:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  127 |  scanf("%d %d", &n, &q);
      |  ~~~~~^~~~~~~~~~~~~~~~~
road_development.cpp:131:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  131 |   scanf("%d %d %d", T+i, X+i, Y+i);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5076 KB Output is correct
2 Correct 5 ms 5120 KB Output is correct
3 Correct 4 ms 5204 KB Output is correct
4 Correct 4 ms 5256 KB Output is correct
5 Correct 5 ms 5076 KB Output is correct
6 Correct 4 ms 5204 KB Output is correct
7 Correct 5 ms 5076 KB Output is correct
8 Correct 4 ms 5204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 231 ms 17500 KB Output is correct
2 Correct 541 ms 22412 KB Output is correct
3 Correct 236 ms 28720 KB Output is correct
4 Correct 229 ms 29520 KB Output is correct
5 Correct 254 ms 27032 KB Output is correct
6 Correct 223 ms 27132 KB Output is correct
7 Correct 410 ms 22540 KB Output is correct
8 Correct 204 ms 26700 KB Output is correct
9 Correct 365 ms 21936 KB Output is correct
10 Correct 191 ms 25680 KB Output is correct
11 Correct 200 ms 28052 KB Output is correct
12 Correct 191 ms 31364 KB Output is correct
13 Correct 167 ms 31796 KB Output is correct
14 Correct 198 ms 27592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 233 ms 17568 KB Output is correct
2 Correct 378 ms 22504 KB Output is correct
3 Correct 263 ms 27412 KB Output is correct
4 Correct 221 ms 30432 KB Output is correct
5 Correct 224 ms 22428 KB Output is correct
6 Correct 239 ms 27464 KB Output is correct
7 Correct 208 ms 21552 KB Output is correct
8 Correct 255 ms 26432 KB Output is correct
9 Correct 223 ms 27536 KB Output is correct
10 Correct 215 ms 28312 KB Output is correct
11 Correct 193 ms 21244 KB Output is correct
12 Correct 233 ms 28604 KB Output is correct
13 Correct 210 ms 28940 KB Output is correct
14 Correct 173 ms 27876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 160 ms 16208 KB Output is correct
2 Correct 360 ms 19788 KB Output is correct
3 Correct 186 ms 24268 KB Output is correct
4 Correct 173 ms 26592 KB Output is correct
5 Correct 605 ms 21920 KB Output is correct
6 Correct 275 ms 28424 KB Output is correct
7 Correct 67 ms 17172 KB Output is correct
8 Correct 63 ms 22536 KB Output is correct
9 Correct 73 ms 25548 KB Output is correct
10 Correct 121 ms 18892 KB Output is correct
11 Correct 221 ms 26592 KB Output is correct
12 Correct 285 ms 26572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5076 KB Output is correct
2 Correct 5 ms 5120 KB Output is correct
3 Correct 4 ms 5204 KB Output is correct
4 Correct 4 ms 5256 KB Output is correct
5 Correct 5 ms 5076 KB Output is correct
6 Correct 4 ms 5204 KB Output is correct
7 Correct 5 ms 5076 KB Output is correct
8 Correct 4 ms 5204 KB Output is correct
9 Correct 231 ms 17500 KB Output is correct
10 Correct 541 ms 22412 KB Output is correct
11 Correct 236 ms 28720 KB Output is correct
12 Correct 229 ms 29520 KB Output is correct
13 Correct 254 ms 27032 KB Output is correct
14 Correct 223 ms 27132 KB Output is correct
15 Correct 410 ms 22540 KB Output is correct
16 Correct 204 ms 26700 KB Output is correct
17 Correct 365 ms 21936 KB Output is correct
18 Correct 191 ms 25680 KB Output is correct
19 Correct 200 ms 28052 KB Output is correct
20 Correct 191 ms 31364 KB Output is correct
21 Correct 167 ms 31796 KB Output is correct
22 Correct 198 ms 27592 KB Output is correct
23 Correct 233 ms 17568 KB Output is correct
24 Correct 378 ms 22504 KB Output is correct
25 Correct 263 ms 27412 KB Output is correct
26 Correct 221 ms 30432 KB Output is correct
27 Correct 224 ms 22428 KB Output is correct
28 Correct 239 ms 27464 KB Output is correct
29 Correct 208 ms 21552 KB Output is correct
30 Correct 255 ms 26432 KB Output is correct
31 Correct 223 ms 27536 KB Output is correct
32 Correct 215 ms 28312 KB Output is correct
33 Correct 193 ms 21244 KB Output is correct
34 Correct 233 ms 28604 KB Output is correct
35 Correct 210 ms 28940 KB Output is correct
36 Correct 173 ms 27876 KB Output is correct
37 Correct 160 ms 16208 KB Output is correct
38 Correct 360 ms 19788 KB Output is correct
39 Correct 186 ms 24268 KB Output is correct
40 Correct 173 ms 26592 KB Output is correct
41 Correct 605 ms 21920 KB Output is correct
42 Correct 275 ms 28424 KB Output is correct
43 Correct 67 ms 17172 KB Output is correct
44 Correct 63 ms 22536 KB Output is correct
45 Correct 73 ms 25548 KB Output is correct
46 Correct 121 ms 18892 KB Output is correct
47 Correct 221 ms 26592 KB Output is correct
48 Correct 285 ms 26572 KB Output is correct
49 Correct 235 ms 21572 KB Output is correct
50 Correct 450 ms 22408 KB Output is correct
51 Correct 260 ms 28492 KB Output is correct
52 Correct 236 ms 31644 KB Output is correct
53 Correct 218 ms 20332 KB Output is correct
54 Correct 187 ms 21536 KB Output is correct
55 Correct 249 ms 27264 KB Output is correct
56 Correct 180 ms 29304 KB Output is correct
57 Correct 210 ms 26592 KB Output is correct
58 Correct 196 ms 26564 KB Output is correct
59 Correct 180 ms 27596 KB Output is correct
60 Correct 164 ms 28680 KB Output is correct
61 Correct 185 ms 28064 KB Output is correct