Submission #635719

# Submission time Handle Problem Language Result Execution time Memory
635719 2022-08-26T17:43:59 Z MohammadAghil LOSTIKS (INOI20_lostiks) C++17
0 / 100
15 ms 23904 KB
#include <bits/stdc++.h>
//#pragma GCC optimize ("Ofast,unroll-loops")
//#pragma GCC target ("avx2")
using namespace std;

typedef long long ll;
typedef pair<int, int> pp;

#define rep(i,l,r) for(int i = (l); i < (r); i++)
#define per(i,r,l) for(int i = (r); i >= (l); i--)
#define all(x) begin(x), end(x)
#define sz(x) (int)x.size()
#define pb push_back
#define ff first
#define ss second

void dbg(){
    cerr << endl;
}
template<typename Head, typename... Tail> void dbg(Head h, Tail... t){
    cerr << " " << h << ",";
    dbg(t...);
}
#define er(...) cerr << __LINE__ << " <" << #__VA_ARGS__ << ">:", dbg(__VA_ARGS__)

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void IOS(){
    cin.tie(0) -> sync_with_stdio(0);
    #ifndef ONLINE_JUDGE
        freopen("inp.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
    #endif
}

const ll mod = 1e9 + 7, maxn = 1e6 + 5, maxm = 20, inf = ll(1e9) + 5;

int val[maxn], ed[maxm], nxt[maxm], tmp, n;
vector<pp> adj[maxn];

void dfs(int r, int p){
    val[r] |= val[p];
    for(auto&[c, w]: adj[r]) if(c - p){
        if(w + 1){
            ed[tmp] = r;
            nxt[tmp] = w;
            val[c] = 1<<tmp;
            tmp++;
            dfs(c, r);
        } else{
            dfs(c, r);
        }
    }
}

int dist[maxm + 1][maxn], dq[maxn], st, en;

void dfs2(int r, int id){
//    vector<pp> stk;
//    for(auto&[c, w]: adj[r]) if(c - p){
//        dist[id][c] = dist[id][r] + 1;
//        dfs2(c, r);
//    }
    dq[st = 0]  = r, en = 1;
    fill(dist[id], dist[id] + n, -1);
    dist[id][r] = 0;
    while(st < en){
        int r = dq[st++];
        for(auto&[c, w]: adj[r]) if(dist[id][c] == -1){
            dist[id][c] = dist[id][r] + 1;
            dq[en++] = c;
        }
    }
}

int dp[1<<maxm][maxm];

int main(){ IOS();
    int s, t; cin >> n >> s >> t; s--, t--;
    int m = 0;
    rep(i,1,n){
        int u, v, w; cin >> u >> v >> w; u--, v--, w--;
        adj[u].pb({v, w});
        adj[v].pb({u, w});
        m += w != -1;
    }
    dfs(s, -1);
    rep(i,0,m){
        dfs2(ed[i], i);
    }
    dfs2(s, m);
    auto chk = [&](int msk, int r){
        return (val[r]&msk) == val[r];
    };
    per(i,(1<<m)-1,1){
        vector<int> is, nis;
        rep(e,0,m) {
            if(i>>e&1){
                if(chk(i, ed[e])) is.pb(e);
            }else{
                if(chk(i, nxt[e]) && chk(i, ed[e])) nis.pb(e);
            }
        }
        for(int e: is){
            dp[i][e] = inf;
            if(chk(i, t)){
                dp[i][e] = dist[e][t];
            }else{
                for(int x: nis){
                    dp[i][e] = min(dp[i][e], dp[i^(1<<x)][x] + dist[e][nxt[x]] + dist[x][nxt[x]]);
                }
            }

        }
    }
    int ans = inf;
    rep(i,0,m){
        if(chk(0, nxt[i]) && chk(0, ed[i])) ans = min(ans, dp[1<<i][i] + dist[m][nxt[i]] + dist[i][nxt[i]]);
    }
    if(chk(0, t)){
        ans = dist[m][t];
    }
    cout << (ans == inf? -1: ans) << '\n';
    return 0;
}

Compilation message

Main.cpp: In function 'void IOS()':
Main.cpp:31:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 |         freopen("inp.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:32:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |         freopen("out.txt", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 14 ms 23892 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 15 ms 23904 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 14 ms 23892 KB Output isn't correct
2 Halted 0 ms 0 KB -