답안 #1042027

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1042027 2024-08-02T12:37:17 Z Dan4Life 자매 도시 (APIO20_swap) C++17
30 / 100
159 ms 73024 KB
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
 
#define all(a) begin(a),end(a)
#define sz(a) (int)a.size()
#define pb push_back
 
const int mxN = (int)4e5+10;
const int mxLg = 20;
int n, m, newN, dfs_time;
int st[mxN], en[mxN], deg[mxN];
int p[mxN], line[mxN], good[mxN]; 
vector<array<int,3>> edges;
int jmp[mxLg][mxN], dep[mxN];
vector<int> adj[mxN];
 
int findSet(int i){return p[i]==i?i:p[i]=findSet(p[i]);}
bool isSameSet(int i, int j) { return findSet(i)==findSet(j); }
 
void unionSet(int i, int j){
	deg[i]++, deg[j]++;
	
	int x = findSet(i), y = findSet(j);
	
	if(x==y) line[x] = 0;
	if(!line[x] or !line[y] or deg[i]>=3 or deg[j]>=3 or good[x] or good[y])
		line[x]=line[y]=0, good[newN]=1;
	if(x==y) return;
	adj[newN].pb(y),adj[y].pb(newN);
	if(x!=y) adj[newN].pb(x),adj[x].pb(newN);
	
	p[y] = p[x] = p[newN] = newN; newN++;
}
 
void dfs(int s, int p){
	if(p!=-1) dep[s] = dep[p]+1;
	else dep[s] = 0;
	jmp[0][s] = p;
	st[s] = dfs_time++;
	for(auto u : adj[s]) 
		if(u!=p) dfs(u,s);
	en[s] = dfs_time;
}
 
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
	n = N, m = M; newN = N;
	for(int i = 0; i < N+M; i++) 
		p[i]=i,deg[i]=0,line[i]=1;
	for(int i = 0; i < M; i++)
		edges.pb({W[i],U[i],V[i]});
	sort(all(edges),[&](array<int,3> a, array<int,3> b){
		return a[0]<b[0];
	});
	
	memset(jmp,-1,sizeof(jmp));
	
	for(int i = 0; i < M; i++)
		unionSet(edges[i][1],edges[i][2]);
		
	for(int i = newN-1; i>=0; i--) 
		if(!dep[i]) dfs(i,-1);
		
	for(int i = 1; i < mxLg; i++)
		for(int j = 0; j < newN; j++)
			if(jmp[i-1][j]!=-1) jmp[i][j] = jmp[i-1][jmp[i-1][j]];
}
 
inline bool anc(int a, int b){
	return st[a]<=st[b] and en[a]>=en[b];
}
 
int lca(int a, int b){
	if(anc(a,b)) return a;
	if(anc(b,a)) return b;
	for(int i = mxLg-1; i>=0; i--)
		if(jmp[i][a]!=-1 and !anc(jmp[i][a],b))
			a = jmp[i][a];
	if(!anc(jmp[0][a],b)) return -1;
	return jmp[0][a];
}
 
int findNearestAnc(int s){
	if(s==-1 or good[s]) return s;
	for(int i = mxLg-1; i>=0; i--)
		if(jmp[i][s]!=-1 and !good[jmp[i][s]])
			s = jmp[i][s];
	if(jmp[0][s]==-1 or !good[jmp[0][s]]) return -1;
	return jmp[0][s];
}
 
int getMinimumFuelCapacity(int X, int Y) {
	int Z = findNearestAnc(lca(X,Y));
	return Z==-1?-1:edges[Z-n][0];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 51840 KB Output is correct
2 Correct 6 ms 51804 KB Output is correct
3 Correct 6 ms 51804 KB Output is correct
4 Correct 6 ms 51804 KB Output is correct
5 Correct 6 ms 51804 KB Output is correct
6 Correct 7 ms 51804 KB Output is correct
7 Correct 6 ms 51956 KB Output is correct
8 Correct 7 ms 51804 KB Output is correct
9 Correct 56 ms 60616 KB Output is correct
10 Correct 56 ms 62664 KB Output is correct
11 Correct 54 ms 62464 KB Output is correct
12 Correct 59 ms 63172 KB Output is correct
13 Correct 51 ms 65480 KB Output is correct
14 Correct 52 ms 60928 KB Output is correct
15 Correct 102 ms 66404 KB Output is correct
16 Correct 103 ms 67188 KB Output is correct
17 Correct 102 ms 67888 KB Output is correct
18 Correct 159 ms 70004 KB Output is correct
19 Incorrect 46 ms 57616 KB Output isn't correct
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 51840 KB Output is correct
2 Correct 6 ms 51804 KB Output is correct
3 Correct 127 ms 71268 KB Output is correct
4 Correct 126 ms 72496 KB Output is correct
5 Correct 129 ms 72284 KB Output is correct
6 Correct 121 ms 72744 KB Output is correct
7 Correct 133 ms 73024 KB Output is correct
8 Correct 137 ms 72304 KB Output is correct
9 Correct 124 ms 72696 KB Output is correct
10 Correct 130 ms 72152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 51840 KB Output is correct
2 Correct 6 ms 51804 KB Output is correct
3 Correct 6 ms 51804 KB Output is correct
4 Correct 6 ms 51804 KB Output is correct
5 Correct 6 ms 51804 KB Output is correct
6 Correct 7 ms 51804 KB Output is correct
7 Correct 6 ms 51956 KB Output is correct
8 Correct 7 ms 51804 KB Output is correct
9 Incorrect 5 ms 51804 KB Output isn't correct
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 51804 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 51840 KB Output is correct
2 Correct 6 ms 51804 KB Output is correct
3 Correct 6 ms 51804 KB Output is correct
4 Correct 6 ms 51804 KB Output is correct
5 Correct 6 ms 51804 KB Output is correct
6 Correct 7 ms 51804 KB Output is correct
7 Correct 6 ms 51956 KB Output is correct
8 Correct 7 ms 51804 KB Output is correct
9 Correct 56 ms 60616 KB Output is correct
10 Correct 56 ms 62664 KB Output is correct
11 Correct 54 ms 62464 KB Output is correct
12 Correct 59 ms 63172 KB Output is correct
13 Correct 51 ms 65480 KB Output is correct
14 Correct 52 ms 60928 KB Output is correct
15 Correct 102 ms 66404 KB Output is correct
16 Correct 103 ms 67188 KB Output is correct
17 Correct 102 ms 67888 KB Output is correct
18 Correct 159 ms 70004 KB Output is correct
19 Correct 127 ms 71268 KB Output is correct
20 Correct 126 ms 72496 KB Output is correct
21 Correct 129 ms 72284 KB Output is correct
22 Correct 121 ms 72744 KB Output is correct
23 Correct 133 ms 73024 KB Output is correct
24 Correct 137 ms 72304 KB Output is correct
25 Correct 124 ms 72696 KB Output is correct
26 Correct 130 ms 72152 KB Output is correct
27 Correct 7 ms 51800 KB Output is correct
28 Correct 7 ms 51852 KB Output is correct
29 Correct 6 ms 52020 KB Output is correct
30 Correct 6 ms 51800 KB Output is correct
31 Correct 6 ms 51804 KB Output is correct
32 Correct 6 ms 51804 KB Output is correct
33 Correct 6 ms 51804 KB Output is correct
34 Correct 6 ms 51804 KB Output is correct
35 Correct 7 ms 51804 KB Output is correct
36 Correct 12 ms 53484 KB Output is correct
37 Correct 62 ms 62944 KB Output is correct
38 Correct 58 ms 62916 KB Output is correct
39 Correct 55 ms 62920 KB Output is correct
40 Correct 58 ms 62660 KB Output is correct
41 Correct 55 ms 62408 KB Output is correct
42 Correct 61 ms 61896 KB Output is correct
43 Correct 55 ms 62916 KB Output is correct
44 Correct 56 ms 62920 KB Output is correct
45 Correct 51 ms 65988 KB Output is correct
46 Correct 63 ms 62920 KB Output is correct
47 Correct 16 ms 53788 KB Output is correct
48 Correct 108 ms 68320 KB Output is correct
49 Correct 103 ms 68400 KB Output is correct
50 Correct 103 ms 68204 KB Output is correct
51 Correct 101 ms 68152 KB Output is correct
52 Correct 100 ms 67708 KB Output is correct
53 Correct 84 ms 65464 KB Output is correct
54 Correct 101 ms 69220 KB Output is correct
55 Correct 102 ms 68400 KB Output is correct
56 Correct 126 ms 70840 KB Output is correct
57 Correct 114 ms 69212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 51804 KB Output isn't correct
2 Halted 0 ms 0 KB -