Submission #1043582

# Submission time Handle Problem Language Result Execution time Memory
1043582 2024-08-04T11:44:31 Z javotaz LOSTIKS (INOI20_lostiks) C++17
59 / 100
2000 ms 136800 KB
// In the Name of Allah
 
#include<bits/stdc++.h>
using namespace std;
 
#pragma GCC optimize("Ofast,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,avx,avx2,sse4.2,popcnt,tune=native")
 
typedef long long ll;
 
#define F first
#define S second
#define pii pair<int, int>
#define pb push_back
#define pp pop_back
#define all(x) x.begin(), x.end()
 
const int N = 1e6 + 12, M = 20, inf = 1e9 + 7;
vector<pii> g[N];
int n, s, t, par[N], dp[(1 << M) + 4][M], p[N], kd[N], pd[N], e[N], l[N], r[N], dis[2 * M + 2][2 * M + 2];
pii f[2 * M + 2][2 * M + 2];
bool mrk[N];
vector<int> v, d;
 
void ip() {
	cin >> n >> s >> t;
	for (int i = 1; i < n; i++) {
		int u, v, w;
		cin >> u >> v >> w;
		g[u].pb({v, w});
		g[v].pb({u, w});
	}
}
 
void dfs(int u) {
	for (auto i: g[u])
		if (i.F != par[u]) { 
			par[i.F] = u;
			e[i.F] = e[u] + 1;
			if (i.S) {
				pd[i.F] = d.size(), d.pb(i.F), kd[i.F] = i.S;
				v.pb(u), v.pb(i.S);
			}
			dfs(i.F);
		}
}
 
void dfs2(int u, int rt, int h = 0, int mask = 0, int par = -1) {
	if (mrk[u])
		f[rt][p[u]] = {h, mask}, dis[rt][p[u]] = h;
	for (auto i: g[u])
		if (i.F != par)
			dfs2(i.F, rt, h + 1, (i.S? (mask | (1 << pd[(e[u] < e[i.F])? i.F : u])) : mask), u);
}
 
void solve() {
	par[s] = s;
	dfs(s);
	v.pb(s), v.pb(t);
	sort(all(v));
	v.resize(unique(all(v)) - v.begin());
	for (int i = 0; i < v.size(); i++) 
		p[v[i]] = i, mrk[v[i]] = true;
	for (int i = 0; i < v.size(); i++)
		dfs2(v[i], i);
	memset(dp, 63, sizeof dp);
}

bool check(int v, int u, int x) {
	return ((f[l[v]][r[u]].S | x) == x) && ((f[r[u]][l[u]].S | x) == x);
}

int32_t main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	ip(), solve();
	int c = d.size();
	int ans = inf;
	for (int i = 0; i < c; i++)
		l[i] = p[par[d[i]]], r[i] = p[kd[d[i]]];
	for (int mask = 1; mask < (1 << c); ++mask) 
		for (int i = 0; i < c; ++i) 
			if ((mask >> i) & 1) {
				int x = (mask ^ (1 << i));
				if (!x) {
					if ((f[p[s]][r[i]].S | x) == x && (f[r[i]][l[i]].S | x) == x)
						dp[mask][i] = f[p[s]][r[i]].F + f[r[i]][l[i]].F;
				}
				else {
					int u = __builtin_ctz(x), y = inf;
					auto &s = dis[r[i]];
					if (check(u, i, x))
						for (int j = 0; j < c; ++j)
							if ((x >> j) & 1 && dp[x][j] < inf)
								y = ((y < s[l[j]] + dp[x][j])? y : s[l[j]] + dp[x][j]);
					dp[mask][i] = y + f[r[i]][l[i]].F;
				}
				if ((f[l[i]][p[t]].S | mask) == mask)
					ans = min(ans, dp[mask][i] + f[l[i]][p[t]].F);
			}
	if (f[p[s]][p[t]].S == 0)
		ans = f[p[s]][p[t]].F;
	cout << ((ans >= inf)? -1 : ans) << '\n';
	return 0;
}

Compilation message

Main.cpp: In function 'void solve()':
Main.cpp:62:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |  for (int i = 0; i < v.size(); i++)
      |                  ~~^~~~~~~~~~
Main.cpp:64:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |  for (int i = 0; i < v.size(); i++)
      |                  ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 14 ms 119644 KB Output is correct
2 Correct 15 ms 119644 KB Output is correct
3 Correct 67 ms 128380 KB Output is correct
4 Correct 65 ms 128336 KB Output is correct
5 Correct 67 ms 128340 KB Output is correct
6 Correct 62 ms 128344 KB Output is correct
7 Correct 64 ms 128344 KB Output is correct
8 Correct 65 ms 128340 KB Output is correct
9 Correct 62 ms 128528 KB Output is correct
10 Correct 68 ms 128336 KB Output is correct
11 Correct 65 ms 128340 KB Output is correct
12 Correct 67 ms 130412 KB Output is correct
13 Correct 68 ms 131412 KB Output is correct
14 Correct 64 ms 130132 KB Output is correct
15 Correct 64 ms 130388 KB Output is correct
16 Correct 67 ms 131664 KB Output is correct
17 Correct 64 ms 131408 KB Output is correct
18 Correct 70 ms 131920 KB Output is correct
19 Correct 72 ms 136628 KB Output is correct
20 Correct 72 ms 136800 KB Output is correct
21 Correct 76 ms 136788 KB Output is correct
22 Correct 15 ms 119900 KB Output is correct
23 Correct 12 ms 119900 KB Output is correct
24 Correct 16 ms 120148 KB Output is correct
25 Correct 14 ms 119908 KB Output is correct
26 Correct 14 ms 119900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 119640 KB Output is correct
2 Correct 14 ms 119644 KB Output is correct
3 Correct 15 ms 119836 KB Output is correct
4 Correct 17 ms 119796 KB Output is correct
5 Correct 91 ms 120180 KB Output is correct
6 Correct 78 ms 120180 KB Output is correct
7 Correct 93 ms 120156 KB Output is correct
8 Correct 91 ms 120156 KB Output is correct
9 Correct 87 ms 120204 KB Output is correct
10 Correct 75 ms 120156 KB Output is correct
11 Correct 85 ms 120204 KB Output is correct
12 Correct 76 ms 120196 KB Output is correct
13 Correct 69 ms 120192 KB Output is correct
14 Correct 73 ms 120156 KB Output is correct
15 Correct 77 ms 120168 KB Output is correct
16 Correct 96 ms 120260 KB Output is correct
17 Correct 88 ms 120228 KB Output is correct
18 Correct 73 ms 120368 KB Output is correct
19 Correct 85 ms 120360 KB Output is correct
20 Correct 78 ms 120368 KB Output is correct
21 Correct 77 ms 120412 KB Output is correct
22 Correct 70 ms 120780 KB Output is correct
23 Correct 71 ms 120760 KB Output is correct
24 Correct 79 ms 120780 KB Output is correct
25 Correct 539 ms 119872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 119644 KB Output is correct
2 Correct 15 ms 119644 KB Output is correct
3 Correct 67 ms 128380 KB Output is correct
4 Correct 65 ms 128336 KB Output is correct
5 Correct 67 ms 128340 KB Output is correct
6 Correct 62 ms 128344 KB Output is correct
7 Correct 64 ms 128344 KB Output is correct
8 Correct 65 ms 128340 KB Output is correct
9 Correct 62 ms 128528 KB Output is correct
10 Correct 68 ms 128336 KB Output is correct
11 Correct 65 ms 128340 KB Output is correct
12 Correct 67 ms 130412 KB Output is correct
13 Correct 68 ms 131412 KB Output is correct
14 Correct 64 ms 130132 KB Output is correct
15 Correct 64 ms 130388 KB Output is correct
16 Correct 67 ms 131664 KB Output is correct
17 Correct 64 ms 131408 KB Output is correct
18 Correct 70 ms 131920 KB Output is correct
19 Correct 72 ms 136628 KB Output is correct
20 Correct 72 ms 136800 KB Output is correct
21 Correct 76 ms 136788 KB Output is correct
22 Correct 15 ms 119900 KB Output is correct
23 Correct 12 ms 119900 KB Output is correct
24 Correct 16 ms 120148 KB Output is correct
25 Correct 14 ms 119908 KB Output is correct
26 Correct 14 ms 119900 KB Output is correct
27 Correct 15 ms 119640 KB Output is correct
28 Correct 14 ms 119644 KB Output is correct
29 Correct 15 ms 119836 KB Output is correct
30 Correct 17 ms 119796 KB Output is correct
31 Correct 91 ms 120180 KB Output is correct
32 Correct 78 ms 120180 KB Output is correct
33 Correct 93 ms 120156 KB Output is correct
34 Correct 91 ms 120156 KB Output is correct
35 Correct 87 ms 120204 KB Output is correct
36 Correct 75 ms 120156 KB Output is correct
37 Correct 85 ms 120204 KB Output is correct
38 Correct 76 ms 120196 KB Output is correct
39 Correct 69 ms 120192 KB Output is correct
40 Correct 73 ms 120156 KB Output is correct
41 Correct 77 ms 120168 KB Output is correct
42 Correct 96 ms 120260 KB Output is correct
43 Correct 88 ms 120228 KB Output is correct
44 Correct 73 ms 120368 KB Output is correct
45 Correct 85 ms 120360 KB Output is correct
46 Correct 78 ms 120368 KB Output is correct
47 Correct 77 ms 120412 KB Output is correct
48 Correct 70 ms 120780 KB Output is correct
49 Correct 71 ms 120760 KB Output is correct
50 Correct 79 ms 120780 KB Output is correct
51 Correct 539 ms 119872 KB Output is correct
52 Execution timed out 2074 ms 92392 KB Time limit exceeded
53 Halted 0 ms 0 KB -