답안 #773625

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
773625 2023-07-05T07:20:38 Z ParsaS LOSTIKS (INOI20_lostiks) C++17
59 / 100
2000 ms 514960 KB
// In the name of God
//#pragma GCC optimize("O2", "unroll-loops")
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
const int N = 1e6 + 5, lg = 21;
int n, c, s, t;
vector<int> adj[N];
vector<pair<int, int> > E;
int f[N], tin[N], tout[N], T, h[N];
int dis[lg][N];
ll dp[1 << lg][lg];
bool ok[1 << lg][lg];

void dfs(int v, int p) {
    tin[v] = ++T;
    for (auto u : adj[v]) {
        if (u != p) {
            h[u] = h[v] + 1;
            dfs(u, v);
        }
    }
    tout[v] = ++T;
}
void dfs2(int v, int p, int k) {
    for (auto u : adj[v]) {
        if (u != p) {
            dis[k][u] = dis[k][v] + 1;
            dfs2(u, v, k);
        }
    }
}
bool anc(int v, int u) {
    return tin[v] <= tin[u] && tout[v] >= tout[u];
}

void solve() {
    cin >> n >> s >> t;
    for (int i = 0; i < n - 1; i++) {
        int v, u, w; cin >> v >> u >> w;
        adj[v].pb(u), adj[u].pb(v);
        if (w)
            f[(int)E.size()] = w, E.pb(mp(v, u));
    }

    dfs(s, 0);
    tout[0] = ++T;

    c = (int)E.size();
    assert(c <= 20);
    memset(dis, 63, sizeof dis);

    for (int i = 0; i < c; i++) {
        if (tin[E[i].fi] > tin[E[i].se])
            swap(E[i].fi, E[i].se);
        queue<int> q;
        q.push(E[i].fi);
        dis[i][E[i].fi] = 0;
        while (!q.empty()) {
            int v = q.front();
            q.pop();
            for (auto u : adj[v]) {
                if (dis[i][u] > dis[i][v] + 1) {
                    dis[i][u] = dis[i][v] + 1;
                    q.push(u);
                }
            }
        }
    }
    E.pb(mp(t, t));
    f[c] = t;
    for (int mask = 0; mask < (1 << c); mask++) {
        for (int i = 0; i <= c; i++) {
            int v = E[i].fi;
            ok[mask][i] = true;
            for (int j = 0; j < c; j++) {
                ok[mask][i] &= (mask & (1 << j)) || (!anc(E[j].se, v) && !anc(E[j].se, f[i]));
            }
        }
    }
    memset(dp, 63, sizeof dp);
    for (int i = 0; i < c; i++) {
        if (ok[1 << i][i]) {
            dp[1 << i][i] = h[f[i]] + dis[i][f[i]];
        }
    }
    ll ans = 1e18;
    if (ok[0][c]) {
        ans = h[t];
    }
    for (int mask = 1; mask < (1 << c); mask++) {
        for (int i = 0; i < c; i++) {
            if (!(mask & (1 << i)))
                continue;

            int mask2 = mask ^ (1 << i);
            for (int j = 0; j < c; j++) {
                if (mask2 & (1 << j)) {
                    if (ok[mask2][i]) {
                        dp[mask][i] = min(dp[mask][i], dp[mask2][j] + dis[j][f[i]] + dis[i][f[i]]);
                    }
                }
            }

            if (ok[mask][c]) {
                ans = min(ans, dp[mask][i] + dis[i][t]);
            }
        }
    }

    if (ans < 1e18)
        cout << ans << '\n';
    else
        cout << -1 << '\n';
}

int32_t main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    solve();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 277 ms 450752 KB Output is correct
2 Correct 178 ms 450640 KB Output is correct
3 Correct 224 ms 456584 KB Output is correct
4 Correct 251 ms 456536 KB Output is correct
5 Correct 193 ms 456656 KB Output is correct
6 Correct 201 ms 456620 KB Output is correct
7 Correct 208 ms 456664 KB Output is correct
8 Correct 201 ms 456612 KB Output is correct
9 Correct 201 ms 456888 KB Output is correct
10 Correct 207 ms 456716 KB Output is correct
11 Correct 199 ms 456684 KB Output is correct
12 Correct 192 ms 458188 KB Output is correct
13 Correct 188 ms 457872 KB Output is correct
14 Correct 187 ms 457504 KB Output is correct
15 Correct 190 ms 458332 KB Output is correct
16 Correct 186 ms 458892 KB Output is correct
17 Correct 185 ms 458952 KB Output is correct
18 Correct 188 ms 459280 KB Output is correct
19 Correct 198 ms 462560 KB Output is correct
20 Correct 195 ms 462372 KB Output is correct
21 Correct 189 ms 462196 KB Output is correct
22 Correct 146 ms 450720 KB Output is correct
23 Correct 144 ms 450668 KB Output is correct
24 Correct 143 ms 450652 KB Output is correct
25 Correct 149 ms 450688 KB Output is correct
26 Correct 148 ms 450728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 145 ms 450728 KB Output is correct
2 Correct 166 ms 450684 KB Output is correct
3 Correct 148 ms 450760 KB Output is correct
4 Correct 145 ms 450704 KB Output is correct
5 Correct 1351 ms 461928 KB Output is correct
6 Correct 1423 ms 461816 KB Output is correct
7 Correct 1323 ms 461920 KB Output is correct
8 Correct 1368 ms 461856 KB Output is correct
9 Correct 1370 ms 462008 KB Output is correct
10 Correct 1184 ms 461944 KB Output is correct
11 Correct 1192 ms 461948 KB Output is correct
12 Correct 1164 ms 461948 KB Output is correct
13 Correct 1208 ms 461948 KB Output is correct
14 Correct 1165 ms 461952 KB Output is correct
15 Correct 1289 ms 461940 KB Output is correct
16 Correct 1330 ms 462012 KB Output is correct
17 Correct 1330 ms 462136 KB Output is correct
18 Correct 1173 ms 462124 KB Output is correct
19 Correct 1130 ms 462076 KB Output is correct
20 Correct 1183 ms 461992 KB Output is correct
21 Correct 1112 ms 462116 KB Output is correct
22 Correct 1124 ms 462364 KB Output is correct
23 Correct 1107 ms 462404 KB Output is correct
24 Correct 1088 ms 462360 KB Output is correct
25 Correct 1722 ms 472396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 277 ms 450752 KB Output is correct
2 Correct 178 ms 450640 KB Output is correct
3 Correct 224 ms 456584 KB Output is correct
4 Correct 251 ms 456536 KB Output is correct
5 Correct 193 ms 456656 KB Output is correct
6 Correct 201 ms 456620 KB Output is correct
7 Correct 208 ms 456664 KB Output is correct
8 Correct 201 ms 456612 KB Output is correct
9 Correct 201 ms 456888 KB Output is correct
10 Correct 207 ms 456716 KB Output is correct
11 Correct 199 ms 456684 KB Output is correct
12 Correct 192 ms 458188 KB Output is correct
13 Correct 188 ms 457872 KB Output is correct
14 Correct 187 ms 457504 KB Output is correct
15 Correct 190 ms 458332 KB Output is correct
16 Correct 186 ms 458892 KB Output is correct
17 Correct 185 ms 458952 KB Output is correct
18 Correct 188 ms 459280 KB Output is correct
19 Correct 198 ms 462560 KB Output is correct
20 Correct 195 ms 462372 KB Output is correct
21 Correct 189 ms 462196 KB Output is correct
22 Correct 146 ms 450720 KB Output is correct
23 Correct 144 ms 450668 KB Output is correct
24 Correct 143 ms 450652 KB Output is correct
25 Correct 149 ms 450688 KB Output is correct
26 Correct 148 ms 450728 KB Output is correct
27 Correct 145 ms 450728 KB Output is correct
28 Correct 166 ms 450684 KB Output is correct
29 Correct 148 ms 450760 KB Output is correct
30 Correct 145 ms 450704 KB Output is correct
31 Correct 1351 ms 461928 KB Output is correct
32 Correct 1423 ms 461816 KB Output is correct
33 Correct 1323 ms 461920 KB Output is correct
34 Correct 1368 ms 461856 KB Output is correct
35 Correct 1370 ms 462008 KB Output is correct
36 Correct 1184 ms 461944 KB Output is correct
37 Correct 1192 ms 461948 KB Output is correct
38 Correct 1164 ms 461948 KB Output is correct
39 Correct 1208 ms 461948 KB Output is correct
40 Correct 1165 ms 461952 KB Output is correct
41 Correct 1289 ms 461940 KB Output is correct
42 Correct 1330 ms 462012 KB Output is correct
43 Correct 1330 ms 462136 KB Output is correct
44 Correct 1173 ms 462124 KB Output is correct
45 Correct 1130 ms 462076 KB Output is correct
46 Correct 1183 ms 461992 KB Output is correct
47 Correct 1112 ms 462116 KB Output is correct
48 Correct 1124 ms 462364 KB Output is correct
49 Correct 1107 ms 462404 KB Output is correct
50 Correct 1088 ms 462360 KB Output is correct
51 Correct 1722 ms 472396 KB Output is correct
52 Correct 1714 ms 514860 KB Output is correct
53 Execution timed out 2050 ms 514960 KB Time limit exceeded
54 Halted 0 ms 0 KB -