Submission #635636

# Submission time Handle Problem Language Result Execution time Memory
635636 2022-08-26T14:23:38 Z NothingXD LOSTIKS (INOI20_lostiks) C++17
100 / 100
1949 ms 315624 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 lg = 20;
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], dis[maxm+1][maxm+1], h[maxn], pare[maxn][lg], dp[mask][maxm+1];
vector<int> g[maxn], G[maxm+1];
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 p = -1){
	pare[v][0] = p;
	for (int i = 1; i < lg; i++) if (pare[v][i-1] != -1) pare[v][i] = pare[pare[v][i-1]][i-1];
	for (int i = first[v]; i; i = nxt[i]){
		int u = to[i];
		if (u != p){
			h[u] = h[v] + 1;
			dfs(u, v);
		}
	}
}

int getpar(int v, int x){
	for (int i = 0; i < lg; i++){
		if ((x >> i) & 1) v = pare[v][i];
	}
	return v;
}

int lca(int u, int v){
	if (h[u] < h[v]) swap(u, v);
	u = getpar(u, h[u] - h[v]);
	if (u == v) return u;
	for(int i = lg - 1; ~i; i--){
		if (pare[v][i] != pare[u][i]){
			v = pare[v][i];
			u = pare[u][i];
		}
	}
	return pare[u][0];
}

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

void getpar(int v, int *par, int p = -1){
	vis[v] = true;
	par[v] = sz;
	for (auto u : G[v]) if (u != p) getpar(u, par, v);
}

int main(){
	//ios_base::sync_with_stdio(false); cin.tie(0);
	
	memset(pare, -1, sizeof pare);

	scanf("%d%d%d", &n, &S, &T);
	for (int i = 1; i < n; i++){
		int u, v, w; scanf("%d%d%d", &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);

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

	for (int i = 0; i <= m; i++){
		for (int j = 0; j <= m; j++){
			int v = (i == m? S: edge[i][0]);
			int u = (j == m? T: edge[j][0]);
			if (j != m){
				dis[i][j] = h[v] + h[u] + 2 * h[edge[j][2]] - 2 * h[lca(v, edge[j][2])] - 2 * h[lca(u, edge[j][2])];
			}
			else{
				dis[i][j] = h[v] + h[u] - 2 * h[lca(u, v)];
			}
		}
	}

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

	for (int i = 0; i < (1 << m); i++){
		for (int j = 0; j < m+1; j++){
			G[j].clear();
			vis[j] = false;
		}
		for (int j = 0; j < m; j++){
			if (!((i >> j) & 1)){
				G[comp[edge[j][0]]].push_back(comp[edge[j][1]]);
				G[comp[edge[j][1]]].push_back(comp[edge[j][0]]);
			}
		}
		sz = 0;
		for (int j = 0; j < m+1; j++){
			if (!vis[j]){
				sz++;
				getpar(j, par[i]);
			}
		}
	}

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

	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 (par[msk][comp[T]] == par[msk][comp[v]]){
				dp[msk][i] = dis[i][m];
				continue;
			}
			for (int tmp = msk; tmp; tmp ^= tmp & -tmp){
				int j = __builtin_ctz(tmp);
				if (par[msk][comp[edge[j][2]]] == par[msk][comp[v]] && par[msk][comp[edge[j][0]]] == par[msk][comp[v]]){
					dp[msk][i] = min(dp[msk][i], dp[msk^(1 << j)][j] + dis[i][j]);
				}
			}
		}
	}

	printf("%d\n", (dp[(1 << m) - 1][m] == inf? -1: dp[(1 << m) - 1][m]));



	return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:92:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   92 |  scanf("%d%d%d", &n, &S, &T);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:94:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   94 |   int u, v, w; scanf("%d%d%d", &u, &v, &w);
      |                ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 40 ms 102096 KB Output is correct
2 Correct 43 ms 102068 KB Output is correct
3 Correct 123 ms 109648 KB Output is correct
4 Correct 104 ms 109668 KB Output is correct
5 Correct 101 ms 109644 KB Output is correct
6 Correct 98 ms 109572 KB Output is correct
7 Correct 101 ms 109652 KB Output is correct
8 Correct 102 ms 109672 KB Output is correct
9 Correct 102 ms 109848 KB Output is correct
10 Correct 98 ms 109744 KB Output is correct
11 Correct 98 ms 109724 KB Output is correct
12 Correct 113 ms 111272 KB Output is correct
13 Correct 99 ms 110932 KB Output is correct
14 Correct 99 ms 110612 KB Output is correct
15 Correct 99 ms 111364 KB Output is correct
16 Correct 114 ms 111896 KB Output is correct
17 Correct 125 ms 111996 KB Output is correct
18 Correct 111 ms 112280 KB Output is correct
19 Correct 110 ms 115556 KB Output is correct
20 Correct 117 ms 115400 KB Output is correct
21 Correct 141 ms 115324 KB Output is correct
22 Correct 41 ms 102060 KB Output is correct
23 Correct 41 ms 102112 KB Output is correct
24 Correct 41 ms 102108 KB Output is correct
25 Correct 47 ms 102064 KB Output is correct
26 Correct 42 ms 102032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 102088 KB Output is correct
2 Correct 41 ms 102012 KB Output is correct
3 Correct 41 ms 102192 KB Output is correct
4 Correct 43 ms 102256 KB Output is correct
5 Correct 717 ms 188732 KB Output is correct
6 Correct 668 ms 188752 KB Output is correct
7 Correct 711 ms 188840 KB Output is correct
8 Correct 672 ms 188700 KB Output is correct
9 Correct 706 ms 188808 KB Output is correct
10 Correct 545 ms 188816 KB Output is correct
11 Correct 594 ms 188816 KB Output is correct
12 Correct 542 ms 188732 KB Output is correct
13 Correct 533 ms 188748 KB Output is correct
14 Correct 559 ms 188728 KB Output is correct
15 Correct 728 ms 188772 KB Output is correct
16 Correct 645 ms 188828 KB Output is correct
17 Correct 736 ms 188784 KB Output is correct
18 Correct 571 ms 188924 KB Output is correct
19 Correct 527 ms 188856 KB Output is correct
20 Correct 566 ms 189064 KB Output is correct
21 Correct 530 ms 188932 KB Output is correct
22 Correct 541 ms 189160 KB Output is correct
23 Correct 552 ms 189104 KB Output is correct
24 Correct 543 ms 189344 KB Output is correct
25 Correct 1026 ms 274488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 102096 KB Output is correct
2 Correct 43 ms 102068 KB Output is correct
3 Correct 123 ms 109648 KB Output is correct
4 Correct 104 ms 109668 KB Output is correct
5 Correct 101 ms 109644 KB Output is correct
6 Correct 98 ms 109572 KB Output is correct
7 Correct 101 ms 109652 KB Output is correct
8 Correct 102 ms 109672 KB Output is correct
9 Correct 102 ms 109848 KB Output is correct
10 Correct 98 ms 109744 KB Output is correct
11 Correct 98 ms 109724 KB Output is correct
12 Correct 113 ms 111272 KB Output is correct
13 Correct 99 ms 110932 KB Output is correct
14 Correct 99 ms 110612 KB Output is correct
15 Correct 99 ms 111364 KB Output is correct
16 Correct 114 ms 111896 KB Output is correct
17 Correct 125 ms 111996 KB Output is correct
18 Correct 111 ms 112280 KB Output is correct
19 Correct 110 ms 115556 KB Output is correct
20 Correct 117 ms 115400 KB Output is correct
21 Correct 141 ms 115324 KB Output is correct
22 Correct 41 ms 102060 KB Output is correct
23 Correct 41 ms 102112 KB Output is correct
24 Correct 41 ms 102108 KB Output is correct
25 Correct 47 ms 102064 KB Output is correct
26 Correct 42 ms 102032 KB Output is correct
27 Correct 39 ms 102088 KB Output is correct
28 Correct 41 ms 102012 KB Output is correct
29 Correct 41 ms 102192 KB Output is correct
30 Correct 43 ms 102256 KB Output is correct
31 Correct 717 ms 188732 KB Output is correct
32 Correct 668 ms 188752 KB Output is correct
33 Correct 711 ms 188840 KB Output is correct
34 Correct 672 ms 188700 KB Output is correct
35 Correct 706 ms 188808 KB Output is correct
36 Correct 545 ms 188816 KB Output is correct
37 Correct 594 ms 188816 KB Output is correct
38 Correct 542 ms 188732 KB Output is correct
39 Correct 533 ms 188748 KB Output is correct
40 Correct 559 ms 188728 KB Output is correct
41 Correct 728 ms 188772 KB Output is correct
42 Correct 645 ms 188828 KB Output is correct
43 Correct 736 ms 188784 KB Output is correct
44 Correct 571 ms 188924 KB Output is correct
45 Correct 527 ms 188856 KB Output is correct
46 Correct 566 ms 189064 KB Output is correct
47 Correct 530 ms 188932 KB Output is correct
48 Correct 541 ms 189160 KB Output is correct
49 Correct 552 ms 189104 KB Output is correct
50 Correct 543 ms 189344 KB Output is correct
51 Correct 1026 ms 274488 KB Output is correct
52 Correct 1316 ms 170004 KB Output is correct
53 Correct 1303 ms 170236 KB Output is correct
54 Correct 1285 ms 168276 KB Output is correct
55 Correct 1298 ms 167344 KB Output is correct
56 Correct 1307 ms 168372 KB Output is correct
57 Correct 1355 ms 169720 KB Output is correct
58 Correct 41 ms 102080 KB Output is correct
59 Correct 41 ms 102152 KB Output is correct
60 Correct 45 ms 102064 KB Output is correct
61 Correct 1387 ms 169632 KB Output is correct
62 Correct 1452 ms 188736 KB Output is correct
63 Correct 1397 ms 182904 KB Output is correct
64 Correct 116 ms 111352 KB Output is correct
65 Correct 145 ms 111528 KB Output is correct
66 Correct 1335 ms 174780 KB Output is correct
67 Correct 1387 ms 174452 KB Output is correct
68 Correct 1734 ms 256964 KB Output is correct
69 Correct 1737 ms 257320 KB Output is correct
70 Correct 1749 ms 256780 KB Output is correct
71 Correct 1684 ms 256708 KB Output is correct
72 Correct 1676 ms 256836 KB Output is correct
73 Correct 1590 ms 258200 KB Output is correct
74 Correct 1707 ms 258776 KB Output is correct
75 Correct 1622 ms 258364 KB Output is correct
76 Correct 1626 ms 259540 KB Output is correct
77 Correct 1605 ms 258324 KB Output is correct
78 Correct 1889 ms 264556 KB Output is correct
79 Correct 1862 ms 263280 KB Output is correct
80 Correct 1936 ms 262352 KB Output is correct
81 Correct 1925 ms 275416 KB Output is correct
82 Correct 1851 ms 277016 KB Output is correct
83 Correct 1868 ms 276548 KB Output is correct
84 Correct 1902 ms 285340 KB Output is correct
85 Correct 1861 ms 315624 KB Output is correct
86 Correct 1879 ms 314672 KB Output is correct
87 Correct 1949 ms 315488 KB Output is correct