Submission #999538

# Submission time Handle Problem Language Result Execution time Memory
999538 2024-06-15T17:44:00 Z aykhn LOSTIKS (INOI20_lostiks) C++17
100 / 100
1309 ms 469584 KB
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define inf 0x3F3F3F3F3F3F3F3F

struct DSU
{
	vector<int> e;
	void init(int n)
	{
		e.assign(n + 1, -1);
	}
	int get(int x)
	{
		if (e[x] < 0) return x;
		return e[x] = get(e[x]);
	}
	void unite(int x, int y)
	{
		x = get(x), y = get(y);
		if (x == y) return;
		if (e[x] > e[y]) swap(x, y);
		e[x] += e[y];
		e[y] = x;
	}
};

const int MXN = 1e6 + 5;
const int LOG = 20;

int n, S, T;
vector<array<int, 2>> adj[MXN];
int p[LOG][MXN], dep[MXN], in[MXN], out[MXN], tim;
int b[MXN], need[MXN];
int dp[20][(1LL << 20)];
vector<array<int, 3>> m;

void dfs(int a)
{
	in[a] = ++tim;
	for (array<int, 2> &v : adj[a])
	{
		if (v[0] == p[0][a]) continue;
		p[0][v[0]] = a;
		dep[v[0]] = dep[a] + 1;
		need[v[0]] = need[a];
		if (v[1])
		{
			int ind = lower_bound(m.begin(), m.end(), array<int, 3> {a, v[0], v[1]}) - m.begin();
			need[v[0]] = need[a] | (1LL << ind);
		}
		dfs(v[0]);
	}
	out[a] = tim;
}

int anc(int u, int v)
{
	return in[u] <= in[v] && out[v] <= out[u];
}
int LCA(int u, int v)
{
	if (anc(u, v)) return u;
	if (anc(v, u)) return v;
	for (int i = LOG - 1; i >= 0; i--)
	{
		if (anc(p[i][u], v)) continue;
		u = p[i][u];
	}
	return p[0][u];
}
int DIST(int u, int v)
{
	return dep[u] + dep[v] - 2 * dep[LCA(u, v)];
}

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> S >> T;
	DSU dsu;
	dsu.init(n);
	for (int i = 1; i < n; i++)
	{
		int u, v, w;
		cin >> u >> v >> w;
		adj[u].push_back({v, w});
		adj[v].push_back({u, w});
		if (w) m.push_back({u, v, w});
		if (!w) dsu.unite(u, v);
	}
	sort(m.begin(), m.end());
	p[0][S] = S;
	dfs(S);
	for (array<int, 3> &i : m) if (p[0][i[0]] == i[1]) swap(i[0], i[1]);
	dfs(S);
	for (int i = 1; i < LOG; i++) for (int j = 1; j <= n; j++) p[i][j] = p[i - 1][p[i - 1][j]];
	for (int i = 0; i < m.size(); i++) for (int j = 0; j < (1LL << (int)m.size()); j++) dp[i][j] = inf;
	for (int i = 0; i < m.size(); i++)
	{
		b[i] = (need[m[i][0]] ^ need[m[i][2]] ^ need[LCA(m[i][0], m[i][2])]);
	}
	if (dsu.get(S) == dsu.get(T)) 
	{
		cout << DIST(S, T) << '\n';
		return 0;
	}
	for (int msk = 1; msk < (1LL << (int)(m.size())); msk++)
	{
		if (__builtin_popcount(msk) == 1)
		for (int i = 0; i < m.size(); i++)
		{
			if (!b[i] && (msk >> i & 1)) 
			{
				dp[i][msk] = DIST(S, m[i][2]) + DIST(m[i][2], m[i][0]);
			}
		}
		for (int i = 0; i < m.size(); i++)
		{
			if (dp[i][msk] >= inf) continue;
			for (int j = 0; j < m.size(); j++)
			{
				if ((msk >> j & 1)) continue;
				if (((msk ^ b[j]) & b[j])) continue;
				dp[j][msk | (1LL << j)] = min(dp[j][msk | (1LL << j)], dp[i][msk] + DIST(m[i][0], m[j][2]) + DIST(m[j][2], m[j][0]));
			}
		}
	}
	int res = inf;
	for (int msk = 1; msk < (1LL << (int)m.size()); msk++)
	{
		for (int i = 0; i < m.size(); i++)
		{
			if (dp[i][msk] >= inf) continue;
			if (((msk ^ need[T]) & need[T])) continue;
			res = min(res, dp[i][msk] + DIST(m[i][0], T));
 		}
	}
	cout << (res >= inf ? -1 : res) << '\n';
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:101:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  101 |  for (int i = 0; i < m.size(); i++) for (int j = 0; j < (1LL << (int)m.size()); j++) dp[i][j] = inf;
      |                  ~~^~~~~~~~~~
Main.cpp:102:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |  for (int i = 0; i < m.size(); i++)
      |                  ~~^~~~~~~~~~
Main.cpp:114:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  114 |   for (int i = 0; i < m.size(); i++)
      |                   ~~^~~~~~~~~~
Main.cpp:121:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |   for (int i = 0; i < m.size(); i++)
      |                   ~~^~~~~~~~~~
Main.cpp:124:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  124 |    for (int j = 0; j < m.size(); j++)
      |                    ~~^~~~~~~~~~
Main.cpp:135:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  135 |   for (int i = 0; i < m.size(); i++)
      |                   ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 71516 KB Output is correct
2 Correct 6 ms 28764 KB Output is correct
3 Correct 77 ms 73056 KB Output is correct
4 Correct 58 ms 50260 KB Output is correct
5 Correct 66 ms 50516 KB Output is correct
6 Correct 59 ms 73556 KB Output is correct
7 Correct 52 ms 110196 KB Output is correct
8 Correct 65 ms 110164 KB Output is correct
9 Correct 76 ms 72724 KB Output is correct
10 Correct 61 ms 110160 KB Output is correct
11 Correct 53 ms 69200 KB Output is correct
12 Correct 52 ms 71508 KB Output is correct
13 Correct 53 ms 69020 KB Output is correct
14 Correct 64 ms 111212 KB Output is correct
15 Correct 54 ms 76420 KB Output is correct
16 Correct 82 ms 65620 KB Output is correct
17 Correct 53 ms 67008 KB Output is correct
18 Correct 49 ms 67664 KB Output is correct
19 Correct 52 ms 69200 KB Output is correct
20 Correct 53 ms 70480 KB Output is correct
21 Correct 53 ms 68436 KB Output is correct
22 Correct 7 ms 36956 KB Output is correct
23 Correct 6 ms 34908 KB Output is correct
24 Correct 6 ms 34908 KB Output is correct
25 Correct 7 ms 37052 KB Output is correct
26 Correct 6 ms 34904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 71516 KB Output is correct
2 Correct 8 ms 57436 KB Output is correct
3 Correct 8 ms 57392 KB Output is correct
4 Correct 9 ms 43100 KB Output is correct
5 Correct 91 ms 122388 KB Output is correct
6 Correct 88 ms 120400 KB Output is correct
7 Correct 91 ms 120528 KB Output is correct
8 Correct 95 ms 114516 KB Output is correct
9 Correct 89 ms 114512 KB Output is correct
10 Correct 85 ms 114672 KB Output is correct
11 Correct 84 ms 114512 KB Output is correct
12 Correct 81 ms 158800 KB Output is correct
13 Correct 91 ms 134484 KB Output is correct
14 Correct 88 ms 124500 KB Output is correct
15 Correct 88 ms 122452 KB Output is correct
16 Correct 85 ms 122432 KB Output is correct
17 Correct 82 ms 116572 KB Output is correct
18 Correct 83 ms 118804 KB Output is correct
19 Correct 85 ms 118864 KB Output is correct
20 Correct 85 ms 118784 KB Output is correct
21 Correct 92 ms 116944 KB Output is correct
22 Correct 94 ms 161360 KB Output is correct
23 Correct 87 ms 137052 KB Output is correct
24 Correct 85 ms 123192 KB Output is correct
25 Correct 1309 ms 205064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 71516 KB Output is correct
2 Correct 6 ms 28764 KB Output is correct
3 Correct 77 ms 73056 KB Output is correct
4 Correct 58 ms 50260 KB Output is correct
5 Correct 66 ms 50516 KB Output is correct
6 Correct 59 ms 73556 KB Output is correct
7 Correct 52 ms 110196 KB Output is correct
8 Correct 65 ms 110164 KB Output is correct
9 Correct 76 ms 72724 KB Output is correct
10 Correct 61 ms 110160 KB Output is correct
11 Correct 53 ms 69200 KB Output is correct
12 Correct 52 ms 71508 KB Output is correct
13 Correct 53 ms 69020 KB Output is correct
14 Correct 64 ms 111212 KB Output is correct
15 Correct 54 ms 76420 KB Output is correct
16 Correct 82 ms 65620 KB Output is correct
17 Correct 53 ms 67008 KB Output is correct
18 Correct 49 ms 67664 KB Output is correct
19 Correct 52 ms 69200 KB Output is correct
20 Correct 53 ms 70480 KB Output is correct
21 Correct 53 ms 68436 KB Output is correct
22 Correct 7 ms 36956 KB Output is correct
23 Correct 6 ms 34908 KB Output is correct
24 Correct 6 ms 34908 KB Output is correct
25 Correct 7 ms 37052 KB Output is correct
26 Correct 6 ms 34904 KB Output is correct
27 Correct 10 ms 71516 KB Output is correct
28 Correct 8 ms 57436 KB Output is correct
29 Correct 8 ms 57392 KB Output is correct
30 Correct 9 ms 43100 KB Output is correct
31 Correct 91 ms 122388 KB Output is correct
32 Correct 88 ms 120400 KB Output is correct
33 Correct 91 ms 120528 KB Output is correct
34 Correct 95 ms 114516 KB Output is correct
35 Correct 89 ms 114512 KB Output is correct
36 Correct 85 ms 114672 KB Output is correct
37 Correct 84 ms 114512 KB Output is correct
38 Correct 81 ms 158800 KB Output is correct
39 Correct 91 ms 134484 KB Output is correct
40 Correct 88 ms 124500 KB Output is correct
41 Correct 88 ms 122452 KB Output is correct
42 Correct 85 ms 122432 KB Output is correct
43 Correct 82 ms 116572 KB Output is correct
44 Correct 83 ms 118804 KB Output is correct
45 Correct 85 ms 118864 KB Output is correct
46 Correct 85 ms 118784 KB Output is correct
47 Correct 92 ms 116944 KB Output is correct
48 Correct 94 ms 161360 KB Output is correct
49 Correct 87 ms 137052 KB Output is correct
50 Correct 85 ms 123192 KB Output is correct
51 Correct 1309 ms 205064 KB Output is correct
52 Correct 1030 ms 294044 KB Output is correct
53 Correct 1058 ms 294332 KB Output is correct
54 Correct 1044 ms 291648 KB Output is correct
55 Correct 1026 ms 292788 KB Output is correct
56 Correct 1027 ms 294268 KB Output is correct
57 Correct 1000 ms 296288 KB Output is correct
58 Correct 9 ms 75864 KB Output is correct
59 Correct 10 ms 84056 KB Output is correct
60 Correct 9 ms 79964 KB Output is correct
61 Correct 951 ms 296164 KB Output is correct
62 Correct 984 ms 309956 KB Output is correct
63 Correct 946 ms 300628 KB Output is correct
64 Correct 58 ms 116816 KB Output is correct
65 Correct 60 ms 117864 KB Output is correct
66 Correct 951 ms 296880 KB Output is correct
67 Correct 951 ms 296404 KB Output is correct
68 Correct 853 ms 368120 KB Output is correct
69 Correct 804 ms 368952 KB Output is correct
70 Correct 843 ms 367956 KB Output is correct
71 Correct 891 ms 367240 KB Output is correct
72 Correct 855 ms 367952 KB Output is correct
73 Correct 869 ms 370068 KB Output is correct
74 Correct 867 ms 371032 KB Output is correct
75 Correct 851 ms 370708 KB Output is correct
76 Correct 854 ms 372080 KB Output is correct
77 Correct 847 ms 370372 KB Output is correct
78 Correct 804 ms 377808 KB Output is correct
79 Correct 820 ms 377936 KB Output is correct
80 Correct 807 ms 375888 KB Output is correct
81 Correct 816 ms 398932 KB Output is correct
82 Correct 830 ms 401236 KB Output is correct
83 Correct 790 ms 400468 KB Output is correct
84 Correct 865 ms 416084 KB Output is correct
85 Correct 863 ms 469584 KB Output is correct
86 Correct 853 ms 468016 KB Output is correct
87 Correct 858 ms 468196 KB Output is correct