Submission #295172

# Submission time Handle Problem Language Result Execution time Memory
295172 2020-09-09T14:06:03 Z 송준혁(#5805) JOI15_road_development (JOI15_road_development) C++14
100 / 100
511 ms 25208 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define INF (1ll<<62)
#define MOD 1'000'000'007
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;

int N, Q, ans;
int qu[303030], U[303030], V[303030];
int Pa[101010], P[101010][20], D[101010];
int I[101010], O[101010], num;
int nx[101010], T[101010];
bool chk[101010];
vector<int> adj[101010];

int rt(int u){
	if (u == Pa[u]) return u;
	return Pa[u] = rt(Pa[u]);
}

void upd(int k, int v){while(k<=N){T[k]+=v, k+=k&-k;}}
int f(int k){int r=0; while(k){r+=T[k], k^=k&-k;} return r;}

void dfs(int u, int p){
	I[u] = ++num, upd(num, 1);
	D[u] = D[p]+1, P[u][0] = nx[u] = p, chk[u] = true;
	for (int i=1; i<=18; i++) P[u][i] = P[P[u][i-1]][i-1];
	for (int v : adj[u]) if (v != p) dfs(v, u);
	O[u] = num, upd(num+1, -1);
}

int lca(int u, int v){
	if (D[u] > D[v]) swap(u, v);
	for (int i=18; i>=0; i--) if (D[u] <= D[v] - (1<<i)) v = P[v][i];
	if (u == v) return u;
	for (int i=18; i>=0; i--) if (P[u][i] != P[v][i]) u = P[u][i], v = P[v][i];
	return P[u][0];
}

int up(int u, int r){
	if (D[u] <= D[r]) return u;
	if (chk[u]){
		upd(I[u], -1), upd(O[u]+1, 1);
		chk[u]=false;
	}
	return nx[u] = up(nx[u], r);
}

int main(){
	scanf("%d %d", &N, &Q);
	for (int i=1; i<=N; i++) Pa[i] = i;
	for (int i=1; i<=Q; i++){
		scanf("%d %d %d", &qu[i], &U[i], &V[i]);
		if (qu[i] == 1 && rt(U[i]) != rt(V[i])){
			adj[U[i]].pb(V[i]), adj[V[i]].pb(U[i]);
			Pa[rt(U[i])] = rt(V[i]);
		}
	}
	for (int i=1; i<=N; i++) if (!D[i]) dfs(i, 0);
	for (int i=1; i<=N; i++) Pa[i] = i;
	for (int i=1; i<=Q; i++){
		if (rt(U[i]) != rt(V[i])){
			if (qu[i] == 1) Pa[rt(U[i])] = rt(V[i]);
			else puts("-1");
		}
		else{
			if (qu[i] == 2) printf("%d\n", f(I[U[i]])+f(I[V[i]])-2*f(I[lca(U[i], V[i])]));
			else{
				up(U[i], lca(U[i], V[i]));
				up(V[i], lca(U[i], V[i]));
			}
		}
	}
	return 0;
}

Compilation message

road_development.cpp: In function 'int main()':
road_development.cpp:53:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   53 |  scanf("%d %d", &N, &Q);
      |  ~~~~~^~~~~~~~~~~~~~~~~
road_development.cpp:56:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   56 |   scanf("%d %d %d", &qu[i], &U[i], &V[i]);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2944 KB Output is correct
2 Correct 4 ms 2944 KB Output is correct
3 Correct 4 ms 2944 KB Output is correct
4 Correct 4 ms 2944 KB Output is correct
5 Correct 4 ms 2944 KB Output is correct
6 Correct 4 ms 2944 KB Output is correct
7 Correct 4 ms 2944 KB Output is correct
8 Correct 4 ms 2944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 295 ms 20868 KB Output is correct
2 Correct 346 ms 20472 KB Output is correct
3 Correct 460 ms 23544 KB Output is correct
4 Correct 493 ms 23544 KB Output is correct
5 Correct 438 ms 22392 KB Output is correct
6 Correct 420 ms 22520 KB Output is correct
7 Correct 323 ms 20472 KB Output is correct
8 Correct 446 ms 22464 KB Output is correct
9 Correct 304 ms 20088 KB Output is correct
10 Correct 418 ms 21880 KB Output is correct
11 Correct 404 ms 23160 KB Output is correct
12 Correct 371 ms 24696 KB Output is correct
13 Correct 407 ms 25208 KB Output is correct
14 Correct 427 ms 22904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 310 ms 20696 KB Output is correct
2 Correct 359 ms 20344 KB Output is correct
3 Correct 448 ms 22784 KB Output is correct
4 Correct 423 ms 23928 KB Output is correct
5 Correct 284 ms 20216 KB Output is correct
6 Correct 486 ms 22776 KB Output is correct
7 Correct 243 ms 20188 KB Output is correct
8 Correct 441 ms 22008 KB Output is correct
9 Correct 363 ms 22136 KB Output is correct
10 Correct 443 ms 23672 KB Output is correct
11 Correct 271 ms 20276 KB Output is correct
12 Correct 435 ms 23420 KB Output is correct
13 Correct 425 ms 23976 KB Output is correct
14 Correct 392 ms 23288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 205 ms 19184 KB Output is correct
2 Correct 249 ms 18808 KB Output is correct
3 Correct 350 ms 20984 KB Output is correct
4 Correct 384 ms 22136 KB Output is correct
5 Correct 363 ms 19832 KB Output is correct
6 Correct 478 ms 23160 KB Output is correct
7 Correct 130 ms 17528 KB Output is correct
8 Correct 165 ms 20216 KB Output is correct
9 Correct 185 ms 21496 KB Output is correct
10 Correct 185 ms 18912 KB Output is correct
11 Correct 451 ms 21752 KB Output is correct
12 Correct 453 ms 21968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2944 KB Output is correct
2 Correct 4 ms 2944 KB Output is correct
3 Correct 4 ms 2944 KB Output is correct
4 Correct 4 ms 2944 KB Output is correct
5 Correct 4 ms 2944 KB Output is correct
6 Correct 4 ms 2944 KB Output is correct
7 Correct 4 ms 2944 KB Output is correct
8 Correct 4 ms 2944 KB Output is correct
9 Correct 295 ms 20868 KB Output is correct
10 Correct 346 ms 20472 KB Output is correct
11 Correct 460 ms 23544 KB Output is correct
12 Correct 493 ms 23544 KB Output is correct
13 Correct 438 ms 22392 KB Output is correct
14 Correct 420 ms 22520 KB Output is correct
15 Correct 323 ms 20472 KB Output is correct
16 Correct 446 ms 22464 KB Output is correct
17 Correct 304 ms 20088 KB Output is correct
18 Correct 418 ms 21880 KB Output is correct
19 Correct 404 ms 23160 KB Output is correct
20 Correct 371 ms 24696 KB Output is correct
21 Correct 407 ms 25208 KB Output is correct
22 Correct 427 ms 22904 KB Output is correct
23 Correct 310 ms 20696 KB Output is correct
24 Correct 359 ms 20344 KB Output is correct
25 Correct 448 ms 22784 KB Output is correct
26 Correct 423 ms 23928 KB Output is correct
27 Correct 284 ms 20216 KB Output is correct
28 Correct 486 ms 22776 KB Output is correct
29 Correct 243 ms 20188 KB Output is correct
30 Correct 441 ms 22008 KB Output is correct
31 Correct 363 ms 22136 KB Output is correct
32 Correct 443 ms 23672 KB Output is correct
33 Correct 271 ms 20276 KB Output is correct
34 Correct 435 ms 23420 KB Output is correct
35 Correct 425 ms 23976 KB Output is correct
36 Correct 392 ms 23288 KB Output is correct
37 Correct 205 ms 19184 KB Output is correct
38 Correct 249 ms 18808 KB Output is correct
39 Correct 350 ms 20984 KB Output is correct
40 Correct 384 ms 22136 KB Output is correct
41 Correct 363 ms 19832 KB Output is correct
42 Correct 478 ms 23160 KB Output is correct
43 Correct 130 ms 17528 KB Output is correct
44 Correct 165 ms 20216 KB Output is correct
45 Correct 185 ms 21496 KB Output is correct
46 Correct 185 ms 18912 KB Output is correct
47 Correct 451 ms 21752 KB Output is correct
48 Correct 453 ms 21968 KB Output is correct
49 Correct 324 ms 20388 KB Output is correct
50 Correct 322 ms 20088 KB Output is correct
51 Correct 511 ms 23252 KB Output is correct
52 Correct 485 ms 24524 KB Output is correct
53 Correct 281 ms 19824 KB Output is correct
54 Correct 241 ms 19884 KB Output is correct
55 Correct 461 ms 22580 KB Output is correct
56 Correct 450 ms 23560 KB Output is correct
57 Correct 477 ms 22248 KB Output is correct
58 Correct 456 ms 22336 KB Output is correct
59 Correct 432 ms 22376 KB Output is correct
60 Correct 383 ms 23024 KB Output is correct
61 Correct 434 ms 23292 KB Output is correct