Submission #1043642

#TimeUsernameProblemLanguageResultExecution timeMemory
1043642javotazLOSTIKS (INOI20_lostiks)C++17
59 / 100
2048 ms136728 KiB
// 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}); } } struct node { int u, h, mask, par; }; 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 bfs(int u, int rt) { node st; st.par = st.u = u, st.h = st.mask = 0; vector<node> q; q.pb(st); while (!q.empty()) { node tmp = q.back(); q.pp(); if (mrk[tmp.u]) f[rt][p[tmp.u]] = {(dis[rt][p[tmp.u]] = tmp.h), tmp.mask}; for (auto i: g[tmp.u]) if (i.F != tmp.par) { node tmp2; tmp2.u = i.F, tmp2.h = tmp.h + 1, tmp2.par = tmp.u; tmp2.mask = (i.S? (tmp.mask | (1 << pd[(e[tmp.u] < e[i.F])? i.F : tmp.u])) : tmp.mask); q.pb(tmp2); } } } 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++) bfs(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); } 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) { bool flag = false; for (int i = 0; i < c; ++i) if (((mask >> i) & 1) && (f[r[i]][l[i]].S | (mask ^ (1 << i))) != (mask ^ (1 << i))) flag = true; if (flag) continue; 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) 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]]; auto &ss = dp[x]; if (check(u, i, x)) { int z = x; for (; z; z ^= z & -z) y = min(y, s[l[__builtin_ctz(z)]] + ss[__builtin_ctz(z)]); } dp[mask][i] = y + s[l[i]]; } 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 (stderr)

Main.cpp: In function 'void solve()':
Main.cpp:78:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |  for (int i = 0; i < v.size(); i++)
      |                  ~~^~~~~~~~~~
Main.cpp:80:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   80 |  for (int i = 0; i < v.size(); i++)
      |                  ~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...