Submission #1279179

#TimeUsernameProblemLanguageResultExecution timeMemory
1279179InvMODLOSTIKS (INOI20_lostiks)C++17
100 / 100
1066 ms200020 KiB
#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define eb emplace_back

#define vi vector<int>
#define pi pair<int,int>
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())

template<class T> using upq = priority_queue<T, vector<T>, greater<T>>;
template<class T> int lwrbound(const vector<T>& a, const T& b, const int s = 0){return int(lower_bound(s + all(a), b) - a.begin());}
template<class T> int uprbound(const vector<T>& a, const T& b, const int s = 0){return int(upper_bound(s + all(a), b) - a.begin());}

#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define ROF(i, a, b) for(int i = (a); i >= (b); i--)
#define sumof(x) accumulate(all(x), 0ll)
#define dbg(x) "[" << #x " = " << (x) << "]"
#define el "\n"

using ll = long long;
using ld = long double;

template<class T> bool ckmx(T& a, const T b){return (a < b ? a = b, true : false);}
template<class T> bool ckmn(T& a, const T b){return (a > b ? a = b, true : false);}

const int N = 1e6 + 5, B = 21, INF = 1e9;

int n, m, S, T, valB[N], Chain, timerDFS;

int id[N], sz[N], par[N], h[N], head[N];

int target[B], dp[(1 << B)][B], tin[N], tout[N];

vector<pi> adj[N]; vi key, nodeh;

void dfs1(int x, int p){
    sz[x] = 1;
    for(pi e : adj[x])if(e.fi != p){
        int v = e.fi;
        par[v] = x, h[v] = h[x] + 1;
        valB[v] = (valB[x] ^ e.se);
        dfs1(v, x);
        sz[x] += sz[v];
    }
}

void dfs2(int x, int p){
    if(!head[Chain]) head[Chain] = x;
    id[x] = Chain, tin[x] = ++timerDFS; int nxt = 0;
    for(pi e : adj[x])if(e.fi != p && sz[e.fi] > sz[nxt]) nxt = e.fi;
    if(nxt) dfs2(nxt, x);
    for(pi e : adj[x])if(e.fi != p && e.fi != nxt) ++Chain, dfs2(e.fi, x);
    tout[x] = timerDFS;
}

int LCA(int u, int v){
    while(id[u] != id[v]){
        if(id[u] < id[v]) swap(u, v);
        u = par[head[id[u]]];
    }
    return h[u] < h[v] ? u : v;
}

int gdist(int u, int v){
    return h[u] + h[v] - 2 * h[LCA(u, v)];
}

bool can(int u, int v, int mask){
    int chk = (valB[u] ^ valB[v]);
    return ((chk & mask) == chk);
}

bool inSub(int u, int v){
    return tin[u] <= tin[v] && tout[v] <= tout[u];
}

void Main()
{
    cin >> n >> S >> T;

    FOR(i, 1, n - 1){
        int u,v,c; cin >> u >> v >> c;

        int w = 0;
        if(c){
            w = (1 << sz(key));
            key.eb(c), nodeh.eb(u), nodeh.eb(v);
        }
        adj[u].eb(v, w), adj[v].eb(u, w);
    }
    dfs1(1, -1), dfs2(1, -1);

    if(can(S, T, 0)){
        cout << gdist(S, T) << el;
        return;
    }

    m = sz(key);
    FOR(i, 0, m - 1){
        int u = nodeh[(i << 1)], v = nodeh[(i << 1) | 1];
        if(h[u] > h[v]) swap(u, v);
        target[i] = (inSub(v, S) ? v : u);
    }

    FOR(mask, 0, (1 << m) - 1) FOR(i, 0, m - 1){
        dp[mask][i] = INF;
    }

    FOR(i, 0, m - 1){
        if(can(S, key[i], 0) && can(key[i], target[i], 0)){
            dp[(1 << i)][i] = gdist(S, key[i]) + gdist(key[i], target[i]);
        }
    }

    int ans = INF;
    FOR(mask, 0, (1 << m) - 1){
        FOR(i, 0, m - 1) if(dp[mask][i] != INF){
            FOR(j, 0, m - 1){
                if(mask >> j & 1) continue;

                if(can(target[i], key[j], mask) && can(key[j], target[j], mask)){
                    ckmn(dp[mask | (1 << j)][j], dp[mask][i] + gdist(target[i], key[j])
                                                             + gdist(key[j], target[j]));
                }
            }

            if(can(target[i], T, mask)){
                ckmn(ans, dp[mask][i] + gdist(target[i], T));
            }
        }
    }
    cout << (ans == INF ? -1 : ans) << el;
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    #define name "InvMOD"
    if(fopen(name".INP", "r")){
        freopen(name".INP", "r", stdin);
        freopen(name".OUT", "w", stdout);
    }

    int t = 1; while(t--) Main();
    return 0;
}

Compilation message (stderr)

Main.cpp: In function 'int32_t main()':
Main.cpp:148:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  148 |         freopen(name".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:149:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  149 |         freopen(name".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...