Submission #1292911

#TimeUsernameProblemLanguageResultExecution timeMemory
1292911thdh__LOSTIKS (INOI20_lostiks)C++20
100 / 100
771 ms284800 KiB
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("avx,avx2,fma") #define ll long long #define pb push_back #define eb emplace_back #define pu push #define ins insert #define fi first #define se second #define all(a) a.begin(),a.end() #define bruh ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fu(x,a,b) for (auto x=a;x<=b;x++) #define fd(x,a,b) for (auto x=a;x>=b;x--) // #define int ll using namespace std; //mt19937 mt(chrono::steady_clock::now().time_since_epoch().count()); /* Competitive Programming notes that I need to study & fix my dumbass self: 1. Coding: - Always be sure to check the memory of arrays (maybe use vectors), for loops - Always try to maximize the memory if possible, even if you are going for subtasks - Do not exploit #define int long long, it will kill you 2. Stress: - Always try generating big testcases and try if they run 3. Time management: - Don't overcommit or undercommit, always spend a certain amount of time to think a problem, don't just look at it and say I'm fucked - Do not spend too much time coding brute-force solutions, they should be easily-codable solutions that don't take up too much time Time management schedule: Offline / LAH days (4 problems - 3h): 15' thinking of solution / idea 1. no idea: skip 2. yes idea: continue thinking for <= 15' + implementing: <= 20' + brute-force: <= 5' + test generator: <= 5' I hate offline because I am dumb */ typedef pair<int, int> ii; const int N = 1e6+5; const int M = 20+5; const int MAXM = (1<<20) + 5; const int B = 750; const int mod = 1e9+7; const int inf = 2e9; using cd = complex<double>; const long double PI = acos(-1); int power(int a,int b) {ll x = 1;if (a >= mod) a%=mod; while (b) {if (b & 1) x = x*a % mod;a = a*a % mod;b>>=1;}return x;} int n, m, s, t; vector<ii> adj[N]; vector<pair<ii, int>> e; vector<int> nodes; int needed[2*M][2*M], dist[2*M][2*M]; int dp[M][MAXM]; int up[N][M]; int h[N], par[N], xr[N]; void dfs(int u, int p) { par[u] = p; up[u][0] = p; for (int i = 1; i < M; i++) up[u][i] = up[up[u][i-1]][i-1]; for (auto i : adj[u]) { int v = i.fi, w = i.se; if (v == p) continue; xr[v] = xr[u]; if (w != -1) xr[v] ^= (1<<w); h[v] = h[u] + 1; dfs(v, u); } } int lca(int u, int v) { if (h[u] < h[v]) swap(u, v); int ou = u; int diff = h[u] - h[v]; for (int i = 0; i < M; i++) if (diff>>i & 1) u = up[u][i]; if (u == v) return u; for (int i = M-1; i >= 0; i--) if (up[u][i] != up[v][i]) u = up[u][i], v = up[v][i]; return up[u][0]; } bool can(int u, int id, int mask) { return (mask & needed[id][u]) == needed[id][u]; } void solve() { cin>>n>>s>>t; for (int i = 1; i < n; i++) { int u,v,w; cin>>u>>v>>w; if (w) e.pb({{u, v}, w}), adj[u].pb({v, e.size()-1}), adj[v].pb({u, e.size()-1}); else adj[u].pb({v, -1}), adj[v].pb({u, -1}); } m = e.size(); dfs(s, 0); for (int i = 0; i < m; i++) { int u = e[i].fi.fi, v = e[i].fi.se, w = e[i].se; if (h[u] > h[v]) swap(e[i].fi.fi, e[i].fi.se); } for (int i = 0; i < 2*m+2; i++) { int cur = (i < m ? e[i].fi.fi : (i == m ? s : (i == m+1 ? t : e[i-(m+2)].se))); for (int j = 0; j < 2*m+2; j++) { int nxt = (j < m ? e[j].fi.fi : (j == m ? s : (j == m+1 ? t : e[j-(m+2)].se))); int x = lca(cur, nxt); dist[i][j] = h[cur] + h[nxt] - 2 * h[x]; needed[i][j] = xr[cur] ^ xr[nxt]; } } for (int i = 0; i < (1<<m); i++) for (int j = 0; j < m+2; j++) dp[j][i] = inf; dp[m][0] = 0; for (int mask = 0; mask < (1<<m); mask++) { for (int i = 0; i < m+1; i++) { if (dp[i][mask] == inf) continue; for (int j = 0; j < m+2; j++) { if (j == m) continue; if (j < m) { if (mask>>j & 1) continue; // cout<<cur<<" "<<j+m+2<<" "<<w<<" "<<j<<" "<<mask<<endl; if (can(i, j+m+2, mask) && can(j+m+2, j, mask)) { // cout<<cur<<" "<<j+m+2<<" "<<w<<" "<<j<<" "<<mask<<endl; // cout<<dist[j+m+2][cur]<<" "<<dist[j][w]<<endl; dp[j][mask ^ (1<<j)] = min(dp[j][mask^(1<<j)], dp[i][mask] + dist[j+m+2][i] + dist[j][j+m+2]); // cout<<dp[j][mask ^ (1<<j)]<<endl; } } else if (j == m+1) { if (can(i, m+1, mask)) { // cout<<i<<" "<<cur<<" "<<mask<<" "<<dp[i][mask]<<endl; dp[j][mask] = min(dp[j][mask], dp[i][mask] + dist[m+1][i]); } } } } } // for (int i = 0; i < m+2; i++) // { // for (int j = 0; j < (1<<m); j++) // { // if (dp[i][j] == inf) cout<<-1<<" "; // else cout<<dp[i][j]<<" "; // } // cout<<endl; // } int ans = inf; for (int i = 0; i < (1<<m); i++) ans = min(ans, dp[m+1][i]); if (ans == inf) cout<<-1; else cout<<ans; } /* Go through the mistakes you usually make and revise your code, for god's sake... */ signed main() { bruh //freopen(".inp","r",stdin); //freopen(".out","w",stdout); int t = 1; // cin>>t; while (t--) { solve(); cout<<"\n"; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...