QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#387277#5102. Dungeon Crawlerucup-team1209#WA 1ms6656kbC++202.0kb2024-04-12 11:41:042024-04-12 11:41:05

Judging History

你现在查看的是最新测评结果

  • [2024-04-12 11:41:05]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:6656kb
  • [2024-04-12 11:41:04]
  • 提交

answer

#include <bits/stdc++.h>
#define cs const
#define pb push_back
using namespace std;
using ll = long long ; 
using pi = pair <int, int> ; 
cs int N = 2e3 + 5; 

void cmx(ll & x, ll y) {
    if(x < y) x = y; 
}

int n, q; 
vector <pi> e[N];

ll d[N][N]; 
int fir[N][N];
int lca[N][N];
int LCA(int rt, int u, int v) {
    return lca[rt][u] ^ lca[u][v] ^ lca[rt][v];
}

void dfs(int u, int f, ll * d, int * fir) { 
    for(auto [v, w] : e[u]) if(v != f) {
        d[v] = d[u] + w; 
        if(f) fir[v] = fir[u];
        else fir[v] = v; 
        dfs(v, u, d, fir); 
    }
}
int fa[N];
void dfs1(int u, int f) {
    fa[u] = f; 
    for(auto [v, w] : e[u]) if(v != f) {
        dfs1(v, u);
    }
}
int work(int i, int j) {
    if(lca[i][j]) return lca[i][j];
    if(i == j) return lca[i][j] = i; 
    if(d[1][i] > d[1][j]) {
        return lca[i][j] = work(fa[i], j);
    }
    else {
        return lca[i][j] = work(i, fa[j]);
    }
}

int main() {
    #ifdef zqj
    freopen("1.in","r",stdin);
    #endif
    ios :: sync_with_stdio(0), cin.tie(0);
    cin >> n >> q;
    ll sum = 0; 
    for(int i = 1; i < n; i++) {
        int u, v, w;
        cin >> u >> v >> w; 
        sum += w; 
        e[u].pb({v, w});
        e[v].pb({u, w});
    }
    sum = sum * 2; 

    for(int i = 1; i <= n; i++) {
        dfs(i, 0, d[i], fir[i]); 
    }
    dfs1(1, 0);
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            work(i, j);
    
    for(int i = 0; i < q; i++) {
        int s, k, t; 
        cin >> s >> k >> t; 
        int p = LCA(s, k, t);
        if(p == t) {
            cout << "impossible\n";
            continue; 
        }
        int q = 0; 
        if(p != k) {
            q = fir[p][k];
        }
        ll ans = 0; 
        for(int j = 1; j <= n; j++) 
            if(!q || (d[s][q] + d[q][j] != d[s][j])) cmx(ans, d[s][j]);
        cout << sum - ans << '\n';
    }

    return 0; 
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 5988kb

input:

22 5
1 2 7
2 3 5
3 4 8
3 6 6
3 7 3
2 5 6
5 8 2
8 9 11
2 10 16
1 11 12
11 12 4
11 13 9
1 14 25
14 15 4
15 16 5
15 17 6
15 18 1
15 19 8
14 20 7
20 21 9
20 22 17
1 19 9
1 9 19
1 8 9
1 9 8
2 22 11

output:

316
293
293
impossible
314

result:

ok 5 lines

Test #2:

score: -100
Wrong Answer
time: 1ms
memory: 6656kb

input:

100 100
1 2 289384
2 3 930887
2 4 692778
4 5 636916
4 6 747794
4 7 238336
4 8 885387
8 9 760493
8 10 516650
8 11 641422
8 12 202363
8 13 490028
8 14 368691
8 15 520060
8 16 897764
16 17 513927
16 18 180541
16 19 383427
16 20 89173
16 21 455737
16 22 5212
16 23 595369
16 24 702568
16 25 956430
16 26 ...

output:

103526917
103484292
106288816
104379596
104405611
104775512
106850550
105291604
103838430
106026228
107121708
104175650
105894571
104509242
103971939
105376499
105223283
104153426
105082245
106829056
104130613
104800548
106846555
104138329
103769253
106879186
104044745
104385328
106973740
105460460
...

result:

wrong answer 7th lines differ - expected: '105434682', found: '106850550'