답안 #635450

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
635450 2022-08-26T09:29:02 Z S2speed LOSTIKS (INOI20_lostiks) C++17
100 / 100
1545 ms 247468 KB
#include<bits/stdc++.h>
 
using namespace std;
 
#pragma GCC optimize ("Ofast")
 
#define sze(x) (int)(x.size())
typedef long long ll;
typedef pair<ll , ll> pll;
typedef pair<int , int> pii;
typedef pair<pii , int> piii;
 
const ll maxn = (1 << 20) + 17 , inf = 2e8;
 
int n , s , t;
pii adj[maxn << 1];
vector<piii> ed;
int dg[maxn] , st[maxn] , ft[maxn] , x[maxn];
int dis[2][maxn];
int bfs[maxn] , sz = 0;
int g[maxn] , k[22] , d[22] , e[22] , f[22][22] , dp[maxn][22] , ind[22];
int jad[maxn][20];

void rBFS(int r , int h){
	sz = 0;
	dis[h][r] = 0;
	g[r] = 0;
	bfs[sz++] = r;
	int x = 0;
	while(x < sz){
		int v = bfs[x++];
		for(int e = st[v] ; e < ft[v] ; e++){
			pii p = adj[e];
			int i = p.first , t = p.second;
			if(dis[h][i] < dis[h][v] + 1) continue;
			jad[i][0] = v;
			for(int j = 1 ; j < 20 ; j++){
				if(jad[i][j - 1] == -1) continue;
				jad[i][j] = jad[jad[i][j - 1]][j - 1];
			}
			g[i] = g[v];
			if(t != -1){
				g[i] ^= (1 << t);
				d[t] = v;
			}
			dis[h][i] = dis[h][v] + 1;
			bfs[sz++] = i;
		}
	}
	return;
}

int find_jad(int v , int d){
	d = dis[0][v] - d;
	for(int j = 0 ; j < 20 ; j++){
		if(d & (1 << j)) v = jad[v][j];
	}
	return v;
}

int lca(int v , int u){
	if(dis[0][v] > dis[0][u]) swap(v , u);
	u = find_jad(u , dis[0][v]);
	if(v == u) return v;
	for(int j = 19 ; ~j ; j--){
		if(jad[v][j] != jad[u][j]){
			v = jad[v][j];
			u = jad[u][j];
		}
	}
	return jad[v][0];
}

int dist(int v , int u){
	int l = lca(v , u);
	return dis[0][v] + dis[0][u] - (dis[0][l] << 1);
}
 
void BFS(int r , int h){
	sz = 0;
	dis[h][r] = 0;
	bfs[sz++] = r;
	int x = 0;
	while(x < sz){
		int v = bfs[x++];
		for(int e = st[v] ; e < ft[v] ; e++){
			pii p = adj[e];
			int i = p.first;
			if(dis[h][i] < dis[h][v] + 1) continue;
			dis[h][i] = dis[h][v] + 1;
			bfs[sz++] = i;
		}
	}
	return;
}
 
int main(){
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	memset(jad , -1 , sizeof(jad));
	memset(dp , 63 , sizeof(dp));
	memset(dis , 63 , sizeof(dis));
	cin>>n>>s>>t; s--; t--;
	int m = 0;
	for(int i = 1 ; i < n ; i++){
		int v , u , w;
		cin>>v>>u>>w; v--; u--; w--;
		dg[v]++; dg[u]++;
		if(w != -1){
			k[m] = w;
			ed.push_back({{v , u} , m});
			m++;
		} else {
			ed.push_back({{v , u} , -1});
		}
	}
	x[0] = st[0] = 0; ft[0] = dg[0];
	for(int i = 1 ; i < n ; i++){
		x[i] = st[i] = ft[i - 1]; ft[i] = st[i] + dg[i];
	}
	for(auto p : ed){
		int v = p.first.first , u = p.first.second , w = p.second;
		adj[x[v]++] = {u , w}; adj[x[u]++] = {v , w};
	}
	rBFS(s , 0);
	if(g[t] == 0){
		cout<<dis[0][t]<<'\n';
		return 0;
	}
	BFS(t , 1);
	for(int j = 0 ; j < m ; j++){
		e[j] = g[k[j]] | g[d[j]];
		if(e[j] == 0){
			dp[(1 << j)][j] = dis[0][k[j]] + dist(d[j] , k[j]);
		}
	}
	for(int i = 0 ; i < m ; i++){
		for(int j = 0 ; j < m ; j++){
			f[i][j] = dist(d[i] , k[j]) + dist(d[j] , k[j]);
		}
	}
	int lm = (1 << m);
	for(int mask = 3 ; mask < lm ; mask++){
		int bp = 0;
		for(int j = 0 ; j < m ; j++){
			if(mask & (1 << j)) ind[bp++] = j;
		}
		if(bp == 1) continue;
		for(int jj = 0 ; jj < bp ; jj++){
			int j = ind[jj];
			if((e[j] & mask) != e[j]) continue;
			int h = inf , msk = mask ^ (1 << j);
			for(int ii = 0 ; ii < bp ; ii++){
				int i = ind[ii];
				if(i == j) continue;
				h = min(h , dp[msk][i] + f[i][j]);
			}
			dp[mask][j] = h;
		}
	}
	int ans = inf;
	for(int mask = 1 ; mask < lm ; mask++){
		if((g[t] & mask) != g[t]) continue;
		int h = inf;
		for(int i = 0 ; i < m ; i++){
			if(!(mask & (1 << i))) continue;
//			cout<<mask<<' '<<i<<'\n';
			h = min(h , dp[mask][i] + dis[1][d[i]]);
		}
		ans = min(ans , h);
	}
	cout<<(ans == inf ? -1 : ans)<<'\n';
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 72 ms 180892 KB Output is correct
2 Correct 68 ms 180864 KB Output is correct
3 Correct 111 ms 186560 KB Output is correct
4 Correct 159 ms 186596 KB Output is correct
5 Correct 121 ms 186656 KB Output is correct
6 Correct 147 ms 186640 KB Output is correct
7 Correct 117 ms 186576 KB Output is correct
8 Correct 135 ms 186580 KB Output is correct
9 Correct 142 ms 186696 KB Output is correct
10 Correct 149 ms 186680 KB Output is correct
11 Correct 137 ms 186688 KB Output is correct
12 Correct 152 ms 186676 KB Output is correct
13 Correct 180 ms 186672 KB Output is correct
14 Correct 147 ms 186684 KB Output is correct
15 Correct 128 ms 186580 KB Output is correct
16 Correct 133 ms 186760 KB Output is correct
17 Correct 138 ms 186768 KB Output is correct
18 Correct 129 ms 186864 KB Output is correct
19 Correct 140 ms 186708 KB Output is correct
20 Correct 135 ms 186772 KB Output is correct
21 Correct 138 ms 186680 KB Output is correct
22 Correct 74 ms 180952 KB Output is correct
23 Correct 70 ms 180824 KB Output is correct
24 Correct 71 ms 180904 KB Output is correct
25 Correct 77 ms 180920 KB Output is correct
26 Correct 78 ms 180952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 180828 KB Output is correct
2 Correct 83 ms 180964 KB Output is correct
3 Correct 74 ms 180940 KB Output is correct
4 Correct 75 ms 180928 KB Output is correct
5 Correct 157 ms 181372 KB Output is correct
6 Correct 137 ms 181480 KB Output is correct
7 Correct 163 ms 181372 KB Output is correct
8 Correct 146 ms 181480 KB Output is correct
9 Correct 156 ms 181448 KB Output is correct
10 Correct 144 ms 181436 KB Output is correct
11 Correct 119 ms 181468 KB Output is correct
12 Correct 133 ms 181468 KB Output is correct
13 Correct 126 ms 181404 KB Output is correct
14 Correct 120 ms 181492 KB Output is correct
15 Correct 157 ms 181480 KB Output is correct
16 Correct 155 ms 181468 KB Output is correct
17 Correct 162 ms 181364 KB Output is correct
18 Correct 119 ms 181472 KB Output is correct
19 Correct 131 ms 181484 KB Output is correct
20 Correct 123 ms 181416 KB Output is correct
21 Correct 121 ms 181436 KB Output is correct
22 Correct 119 ms 181412 KB Output is correct
23 Correct 134 ms 181484 KB Output is correct
24 Correct 122 ms 181484 KB Output is correct
25 Correct 422 ms 180944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 72 ms 180892 KB Output is correct
2 Correct 68 ms 180864 KB Output is correct
3 Correct 111 ms 186560 KB Output is correct
4 Correct 159 ms 186596 KB Output is correct
5 Correct 121 ms 186656 KB Output is correct
6 Correct 147 ms 186640 KB Output is correct
7 Correct 117 ms 186576 KB Output is correct
8 Correct 135 ms 186580 KB Output is correct
9 Correct 142 ms 186696 KB Output is correct
10 Correct 149 ms 186680 KB Output is correct
11 Correct 137 ms 186688 KB Output is correct
12 Correct 152 ms 186676 KB Output is correct
13 Correct 180 ms 186672 KB Output is correct
14 Correct 147 ms 186684 KB Output is correct
15 Correct 128 ms 186580 KB Output is correct
16 Correct 133 ms 186760 KB Output is correct
17 Correct 138 ms 186768 KB Output is correct
18 Correct 129 ms 186864 KB Output is correct
19 Correct 140 ms 186708 KB Output is correct
20 Correct 135 ms 186772 KB Output is correct
21 Correct 138 ms 186680 KB Output is correct
22 Correct 74 ms 180952 KB Output is correct
23 Correct 70 ms 180824 KB Output is correct
24 Correct 71 ms 180904 KB Output is correct
25 Correct 77 ms 180920 KB Output is correct
26 Correct 78 ms 180952 KB Output is correct
27 Correct 71 ms 180828 KB Output is correct
28 Correct 83 ms 180964 KB Output is correct
29 Correct 74 ms 180940 KB Output is correct
30 Correct 75 ms 180928 KB Output is correct
31 Correct 157 ms 181372 KB Output is correct
32 Correct 137 ms 181480 KB Output is correct
33 Correct 163 ms 181372 KB Output is correct
34 Correct 146 ms 181480 KB Output is correct
35 Correct 156 ms 181448 KB Output is correct
36 Correct 144 ms 181436 KB Output is correct
37 Correct 119 ms 181468 KB Output is correct
38 Correct 133 ms 181468 KB Output is correct
39 Correct 126 ms 181404 KB Output is correct
40 Correct 120 ms 181492 KB Output is correct
41 Correct 157 ms 181480 KB Output is correct
42 Correct 155 ms 181468 KB Output is correct
43 Correct 162 ms 181364 KB Output is correct
44 Correct 119 ms 181472 KB Output is correct
45 Correct 131 ms 181484 KB Output is correct
46 Correct 123 ms 181416 KB Output is correct
47 Correct 121 ms 181436 KB Output is correct
48 Correct 119 ms 181412 KB Output is correct
49 Correct 134 ms 181484 KB Output is correct
50 Correct 122 ms 181484 KB Output is correct
51 Correct 422 ms 180944 KB Output is correct
52 Correct 862 ms 232420 KB Output is correct
53 Correct 848 ms 232140 KB Output is correct
54 Correct 852 ms 232108 KB Output is correct
55 Correct 861 ms 232092 KB Output is correct
56 Correct 974 ms 232056 KB Output is correct
57 Correct 953 ms 232020 KB Output is correct
58 Correct 74 ms 180880 KB Output is correct
59 Correct 73 ms 180872 KB Output is correct
60 Correct 74 ms 180852 KB Output is correct
61 Correct 875 ms 232284 KB Output is correct
62 Correct 857 ms 231968 KB Output is correct
63 Correct 850 ms 247384 KB Output is correct
64 Correct 122 ms 187452 KB Output is correct
65 Correct 140 ms 187428 KB Output is correct
66 Correct 881 ms 247468 KB Output is correct
67 Correct 857 ms 247332 KB Output is correct
68 Correct 940 ms 247452 KB Output is correct
69 Correct 912 ms 247300 KB Output is correct
70 Correct 935 ms 247396 KB Output is correct
71 Correct 932 ms 247340 KB Output is correct
72 Correct 909 ms 247420 KB Output is correct
73 Correct 906 ms 247356 KB Output is correct
74 Correct 879 ms 247376 KB Output is correct
75 Correct 911 ms 247372 KB Output is correct
76 Correct 907 ms 247424 KB Output is correct
77 Correct 868 ms 247328 KB Output is correct
78 Correct 1393 ms 247320 KB Output is correct
79 Correct 1312 ms 247380 KB Output is correct
80 Correct 1297 ms 247372 KB Output is correct
81 Correct 1233 ms 247352 KB Output is correct
82 Correct 1464 ms 247360 KB Output is correct
83 Correct 1258 ms 247312 KB Output is correct
84 Correct 1371 ms 247300 KB Output is correct
85 Correct 1545 ms 247368 KB Output is correct
86 Correct 1414 ms 247308 KB Output is correct
87 Correct 1418 ms 247384 KB Output is correct