Submission #635734

# Submission time Handle Problem Language Result Execution time Memory
635734 2022-08-26T17:57:16 Z MohammadAghil LOSTIKS (INOI20_lostiks) C++17
59 / 100
2000 ms 121992 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, tmp_vl[maxm][maxm];

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[maxm][1<<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);

    rep(i,0,m){
        rep(j,0,m){
            tmp_vl[i][j] = dist[i][nxt[j]] + dist[j][nxt[j]];
        }
    }

    auto chk = [&](int msk, int r){
        return (val[r]&msk) == val[r];
    };
    vector<int> is(m), nis(m);
    int ptr_is = 0, ptr_nis = 0;
    per(i,(1<<m)-1,1){
        ptr_is = ptr_nis = 0;
        rep(e,0,m) {
            if(i>>e&1){
                if(chk(i, ed[e])) is[ptr_is++] = e;
            }else{
                if(chk(i, nxt[e]) && chk(i, ed[e])) nis[ptr_nis++] = e;
            }
        }
        rep(x,0,ptr_is){
            int e = is[x];
            dp[e][i] = inf;
            if(chk(i, t)){
                dp[e][i] = dist[e][t];
            }else{
                rep(j,0,ptr_nis){
                    int x = nis[j];
                    dp[e][i] = min(dp[e][i], dp[x][i^(1<<x)] + tmp_vl[e][x]);
                }
            }
        }
    }
    int ans = inf;
    rep(i,0,m){
        if(chk(0, nxt[i]) && chk(0, ed[i])) ans = min(ans, dp[i][1<<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;
}
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23860 KB Output is correct
2 Correct 15 ms 23872 KB Output is correct
3 Correct 81 ms 31700 KB Output is correct
4 Correct 75 ms 31732 KB Output is correct
5 Correct 82 ms 31760 KB Output is correct
6 Correct 85 ms 31692 KB Output is correct
7 Correct 89 ms 31820 KB Output is correct
8 Correct 88 ms 31792 KB Output is correct
9 Correct 85 ms 31956 KB Output is correct
10 Correct 74 ms 31820 KB Output is correct
11 Correct 78 ms 31824 KB Output is correct
12 Correct 90 ms 33252 KB Output is correct
13 Correct 81 ms 32832 KB Output is correct
14 Correct 90 ms 32460 KB Output is correct
15 Correct 89 ms 33268 KB Output is correct
16 Correct 71 ms 34060 KB Output is correct
17 Correct 89 ms 34060 KB Output is correct
18 Correct 92 ms 34608 KB Output is correct
19 Correct 72 ms 38536 KB Output is correct
20 Correct 75 ms 38476 KB Output is correct
21 Correct 81 ms 38220 KB Output is correct
22 Correct 15 ms 23892 KB Output is correct
23 Correct 13 ms 23892 KB Output is correct
24 Correct 15 ms 23832 KB Output is correct
25 Correct 13 ms 23972 KB Output is correct
26 Correct 14 ms 23952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23788 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 15 ms 24020 KB Output is correct
4 Correct 12 ms 24020 KB Output is correct
5 Correct 92 ms 52904 KB Output is correct
6 Correct 87 ms 51648 KB Output is correct
7 Correct 114 ms 52924 KB Output is correct
8 Correct 95 ms 54604 KB Output is correct
9 Correct 93 ms 51768 KB Output is correct
10 Correct 78 ms 47504 KB Output is correct
11 Correct 82 ms 47432 KB Output is correct
12 Correct 78 ms 47524 KB Output is correct
13 Correct 74 ms 47504 KB Output is correct
14 Correct 72 ms 47528 KB Output is correct
15 Correct 89 ms 53324 KB Output is correct
16 Correct 101 ms 52984 KB Output is correct
17 Correct 89 ms 53700 KB Output is correct
18 Correct 77 ms 47676 KB Output is correct
19 Correct 89 ms 47632 KB Output is correct
20 Correct 85 ms 47600 KB Output is correct
21 Correct 74 ms 47620 KB Output is correct
22 Correct 71 ms 47996 KB Output is correct
23 Correct 74 ms 47908 KB Output is correct
24 Correct 79 ms 47952 KB Output is correct
25 Correct 282 ms 89764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23860 KB Output is correct
2 Correct 15 ms 23872 KB Output is correct
3 Correct 81 ms 31700 KB Output is correct
4 Correct 75 ms 31732 KB Output is correct
5 Correct 82 ms 31760 KB Output is correct
6 Correct 85 ms 31692 KB Output is correct
7 Correct 89 ms 31820 KB Output is correct
8 Correct 88 ms 31792 KB Output is correct
9 Correct 85 ms 31956 KB Output is correct
10 Correct 74 ms 31820 KB Output is correct
11 Correct 78 ms 31824 KB Output is correct
12 Correct 90 ms 33252 KB Output is correct
13 Correct 81 ms 32832 KB Output is correct
14 Correct 90 ms 32460 KB Output is correct
15 Correct 89 ms 33268 KB Output is correct
16 Correct 71 ms 34060 KB Output is correct
17 Correct 89 ms 34060 KB Output is correct
18 Correct 92 ms 34608 KB Output is correct
19 Correct 72 ms 38536 KB Output is correct
20 Correct 75 ms 38476 KB Output is correct
21 Correct 81 ms 38220 KB Output is correct
22 Correct 15 ms 23892 KB Output is correct
23 Correct 13 ms 23892 KB Output is correct
24 Correct 15 ms 23832 KB Output is correct
25 Correct 13 ms 23972 KB Output is correct
26 Correct 14 ms 23952 KB Output is correct
27 Correct 13 ms 23788 KB Output is correct
28 Correct 12 ms 23764 KB Output is correct
29 Correct 15 ms 24020 KB Output is correct
30 Correct 12 ms 24020 KB Output is correct
31 Correct 92 ms 52904 KB Output is correct
32 Correct 87 ms 51648 KB Output is correct
33 Correct 114 ms 52924 KB Output is correct
34 Correct 95 ms 54604 KB Output is correct
35 Correct 93 ms 51768 KB Output is correct
36 Correct 78 ms 47504 KB Output is correct
37 Correct 82 ms 47432 KB Output is correct
38 Correct 78 ms 47524 KB Output is correct
39 Correct 74 ms 47504 KB Output is correct
40 Correct 72 ms 47528 KB Output is correct
41 Correct 89 ms 53324 KB Output is correct
42 Correct 101 ms 52984 KB Output is correct
43 Correct 89 ms 53700 KB Output is correct
44 Correct 77 ms 47676 KB Output is correct
45 Correct 89 ms 47632 KB Output is correct
46 Correct 85 ms 47600 KB Output is correct
47 Correct 74 ms 47620 KB Output is correct
48 Correct 71 ms 47996 KB Output is correct
49 Correct 74 ms 47908 KB Output is correct
50 Correct 79 ms 47952 KB Output is correct
51 Correct 282 ms 89764 KB Output is correct
52 Correct 1812 ms 110068 KB Output is correct
53 Execution timed out 2082 ms 121992 KB Time limit exceeded
54 Halted 0 ms 0 KB -