This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |