Submission #633774

# Submission time Handle Problem Language Result Execution time Memory
633774 2022-08-23T08:33:02 Z NothingXD LOSTIKS (INOI20_lostiks) C++14
59 / 100
2000 ms 196348 KB
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
//typedef __uint128_t L;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	cerr << " " << H;
	debug_out(T...);
}

#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define MP(x, y) make_pair(x, y)
#define F first
#define S second

const int maxn = 1e6 + 10;
const int maxm = 20;
const int mask = (1 << 20);
const int inf = 1e9;

int n, m, S, T, edge[maxm][3], comp[maxn], cnt, sz, par[mask][maxm+1], h[maxm+1][maxn], dp[mask][maxm+1];
vector<int> g[maxn];
bool vis[maxn];

int first[maxn], to[maxn << 1], nxt[maxn << 1], ecnt;

void add(int u, int v){
	nxt[++ecnt] = first[u];
	to[ecnt] = v;
	first[u] = ecnt;
}

void dfs(int v, int *h, int p = -1){
	for (int i = first[v]; i; i = nxt[i]){
		int u = to[i];
		if (u != p){
			h[u] = h[v] + 1;
			dfs(u, h, v);
		}
	}
}

void getcomp(int v, int p = -1){
	vis[v] = true;
	comp[v] = cnt;
	for (auto u: g[v]) if (u != p) getcomp(u, v);
}

int getpar(int *par, int v){
	//debug(v, par[v]);
	return (par[v] < 0? v: par[v] = getpar(par, par[v]));
}

void merge(int *par, int u, int v){
	if ((u = getpar(par, u)) == (v = getpar(par, v))) return;
	if (par[u] > par[v]) swap(u, v);
	par[u] += par[v];
	par[v] = u;
}

int main(){
	ios_base::sync_with_stdio(false); cin.tie(0);

	cin >> n >> S >> T;
	for (int i = 1; i < n; i++){
		int u, v, w; cin >> u >> v >> w;
		add(u, v);
		add(v, u);
		if (w == 0){
			g[u].push_back(v);
			g[v].push_back(u);
		}
		else{
			edge[m][0] = u;
			edge[m][1] = v;
			edge[m][2] = w;
			m++;
		}
	}

	dfs(S, h[m]);

	for (int i = 0; i < m; i++){
		if (h[m][edge[i][0]] > h[m][edge[i][1]]) swap(edge[i][0], edge[i][1]);
		dfs(edge[i][0], h[i]);
	}

	assert(m <= 20);

	for (int i = 1; i <= n; i++){
		if (!vis[i]){
			getcomp(i);
			cnt++;
		}
	}

	for (int i = (1 << m) - 1; ~i; i--){
		if (i == (1 << m) - 1){
			for (int j = 0; j <= m; j++) par[i][j] = -1;
			continue;
		}
		int tmp = __builtin_ctz(((1 << m) - 1) ^ i);
		for (int j = 0; j <= m; j++){
			par[i][j] = par[i^(1 << tmp)][j];
		}
		merge(par[i], comp[edge[tmp][0]], comp[edge[tmp][1]]);
	}

	for (int i = 0; i <= m; i++){
		dp[0][i] = h[i][T];
	}

	for (int msk = 1; msk < (1 << m); msk++){
		for (int i = 0; i <= m; i++){
			int v = (i == m? S: edge[i][0]);
			dp[msk][i] = inf;
			if (getpar(par[msk], comp[T]) == getpar(par[msk], comp[v])){
				dp[msk][i] = h[i][T];
				continue;
			}
			for (int tmp = msk; tmp; tmp ^= tmp & -tmp){
				int j = __builtin_ctz(tmp);
				if (getpar(par[msk], comp[edge[j][2]]) == getpar(par[msk], comp[v]) &&
					   	getpar(par[msk], comp[edge[j][0]]) == getpar(par[msk], comp[v])){
					dp[msk][i] = min(dp[msk][i], dp[msk^(1 << j)][j] + h[i][edge[j][2]] + h[j][edge[j][2]]);
				}
			}
		}
	}

	cout << (dp[(1 << m) - 1][m] == inf? -1: dp[(1 << m) - 1][m]) << '\n';



	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23764 KB Output is correct
2 Correct 15 ms 23764 KB Output is correct
3 Correct 117 ms 32828 KB Output is correct
4 Correct 103 ms 32908 KB Output is correct
5 Correct 76 ms 32972 KB Output is correct
6 Correct 112 ms 32864 KB Output is correct
7 Correct 94 ms 33004 KB Output is correct
8 Correct 81 ms 32888 KB Output is correct
9 Correct 77 ms 33184 KB Output is correct
10 Correct 73 ms 32996 KB Output is correct
11 Correct 79 ms 32888 KB Output is correct
12 Correct 74 ms 34816 KB Output is correct
13 Correct 70 ms 35084 KB Output is correct
14 Correct 79 ms 34940 KB Output is correct
15 Correct 69 ms 35020 KB Output is correct
16 Correct 68 ms 35760 KB Output is correct
17 Correct 67 ms 35704 KB Output is correct
18 Correct 80 ms 36356 KB Output is correct
19 Correct 72 ms 40244 KB Output is correct
20 Correct 70 ms 40092 KB Output is correct
21 Correct 70 ms 39904 KB Output is correct
22 Correct 13 ms 23900 KB Output is correct
23 Correct 15 ms 23956 KB Output is correct
24 Correct 14 ms 23844 KB Output is correct
25 Correct 13 ms 23972 KB Output is correct
26 Correct 13 ms 23884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 13 ms 23880 KB Output is correct
3 Correct 14 ms 24024 KB Output is correct
4 Correct 15 ms 24020 KB Output is correct
5 Correct 1177 ms 111144 KB Output is correct
6 Correct 1124 ms 111052 KB Output is correct
7 Correct 1147 ms 111180 KB Output is correct
8 Correct 1109 ms 111160 KB Output is correct
9 Correct 1179 ms 111136 KB Output is correct
10 Correct 1022 ms 111060 KB Output is correct
11 Correct 1000 ms 111116 KB Output is correct
12 Correct 1008 ms 111128 KB Output is correct
13 Correct 970 ms 111092 KB Output is correct
14 Correct 1022 ms 111128 KB Output is correct
15 Correct 1227 ms 111088 KB Output is correct
16 Correct 982 ms 111176 KB Output is correct
17 Correct 1181 ms 111208 KB Output is correct
18 Correct 1054 ms 111352 KB Output is correct
19 Correct 942 ms 111272 KB Output is correct
20 Correct 1014 ms 111284 KB Output is correct
21 Correct 977 ms 111296 KB Output is correct
22 Correct 986 ms 111856 KB Output is correct
23 Correct 970 ms 111756 KB Output is correct
24 Correct 949 ms 111548 KB Output is correct
25 Correct 911 ms 196348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23764 KB Output is correct
2 Correct 15 ms 23764 KB Output is correct
3 Correct 117 ms 32828 KB Output is correct
4 Correct 103 ms 32908 KB Output is correct
5 Correct 76 ms 32972 KB Output is correct
6 Correct 112 ms 32864 KB Output is correct
7 Correct 94 ms 33004 KB Output is correct
8 Correct 81 ms 32888 KB Output is correct
9 Correct 77 ms 33184 KB Output is correct
10 Correct 73 ms 32996 KB Output is correct
11 Correct 79 ms 32888 KB Output is correct
12 Correct 74 ms 34816 KB Output is correct
13 Correct 70 ms 35084 KB Output is correct
14 Correct 79 ms 34940 KB Output is correct
15 Correct 69 ms 35020 KB Output is correct
16 Correct 68 ms 35760 KB Output is correct
17 Correct 67 ms 35704 KB Output is correct
18 Correct 80 ms 36356 KB Output is correct
19 Correct 72 ms 40244 KB Output is correct
20 Correct 70 ms 40092 KB Output is correct
21 Correct 70 ms 39904 KB Output is correct
22 Correct 13 ms 23900 KB Output is correct
23 Correct 15 ms 23956 KB Output is correct
24 Correct 14 ms 23844 KB Output is correct
25 Correct 13 ms 23972 KB Output is correct
26 Correct 13 ms 23884 KB Output is correct
27 Correct 13 ms 23764 KB Output is correct
28 Correct 13 ms 23880 KB Output is correct
29 Correct 14 ms 24024 KB Output is correct
30 Correct 15 ms 24020 KB Output is correct
31 Correct 1177 ms 111144 KB Output is correct
32 Correct 1124 ms 111052 KB Output is correct
33 Correct 1147 ms 111180 KB Output is correct
34 Correct 1109 ms 111160 KB Output is correct
35 Correct 1179 ms 111136 KB Output is correct
36 Correct 1022 ms 111060 KB Output is correct
37 Correct 1000 ms 111116 KB Output is correct
38 Correct 1008 ms 111128 KB Output is correct
39 Correct 970 ms 111092 KB Output is correct
40 Correct 1022 ms 111128 KB Output is correct
41 Correct 1227 ms 111088 KB Output is correct
42 Correct 982 ms 111176 KB Output is correct
43 Correct 1181 ms 111208 KB Output is correct
44 Correct 1054 ms 111352 KB Output is correct
45 Correct 942 ms 111272 KB Output is correct
46 Correct 1014 ms 111284 KB Output is correct
47 Correct 977 ms 111296 KB Output is correct
48 Correct 986 ms 111856 KB Output is correct
49 Correct 970 ms 111756 KB Output is correct
50 Correct 949 ms 111548 KB Output is correct
51 Correct 911 ms 196348 KB Output is correct
52 Execution timed out 2094 ms 121620 KB Time limit exceeded
53 Halted 0 ms 0 KB -