Submission #1292903

#TimeUsernameProblemLanguageResultExecution timeMemory
1292903thdh__LOSTIKS (INOI20_lostiks)C++20
59 / 100
2101 ms181344 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][N], dist[2*M][N];
int dp[M][MAXM];

int h[N], par[N];

void dfs(int u, int p) {
	par[u] = p;
	for (auto i : adj[u]) 
	{
		int v = i.fi, w = i.se;
		if (v == p) continue;
		h[v] = h[u] + 1;
		dfs(v, u);
	}
}

void dfn(int u, int p, int id) 
{
	// cout<<u<<" "<<needed[id][u]<<endl;
	for (auto i : adj[u]) 
	{
		int v = i.fi, w = i.se;
		if (v == p) continue;
		needed[id][v] = needed[id][u];
		// cout<<v<<" "<<w<<endl;
		if (w != -1) needed[id][v] |= (1<<w);
		dist[id][v] = dist[id][u] + 1;
		dfn(v, u, id);
	}
} 

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;
		// cout<<i<<" ";
		if (h[u] < h[v]) dfn(u, 0, i);
		else dfn(v, 0, i), swap(e[i].fi.fi, e[i].fi.se);
		nodes.pb(e[i].fi.fi);
		dfn(w, 0, i+m+2);
	}
	// cout<<t<<endl;
	dfn(s, 0, m); 
	dfn(t, 0, m+1);
	// cout<<needed[m+1][4]<<endl;
	// cout<<can(4, 4, 6)<<endl;
	// return;
	nodes.pb(s); nodes.pb(t);
	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;
			int cur = (i < m ? e[i].fi.fi : (i == m ? s : t));
			for (int j = 0; j < m+2; j++) {
				if (j == m) continue;
				int nxt = (j < m ? e[j].fi.fi : (j == m ? s : t));
				if (j < m) 
				{
					if (mask>>j & 1) continue;
					int w = e[j].se;
					// cout<<cur<<" "<<j+m+2<<" "<<w<<" "<<j<<" "<<mask<<endl; 
					if (can(cur, j+m+2, mask) && can(w, 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][cur] + dist[j][w]);
						// cout<<dp[j][mask ^ (1<<j)]<<endl;
					}
				} else if (j == m+1)
				{
					if (can(cur, m+1, mask)) 
					{
						// cout<<i<<" "<<cur<<" "<<mask<<" "<<dp[i][mask]<<endl;
						dp[j][mask] = min(dp[j][mask], dp[i][mask] + dist[m+1][cur]);
					}
				}
			}
		}
	}
	// 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...