답안 #915642

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
915642 2024-01-24T12:24:10 Z Trisanu_Das Training (IOI07_training) C++17
100 / 100
16 ms 4700 KB
#include <bits/stdc++.h>
using namespace std; 
 
struct edge{ int a, b, w, lca; };
 
const int mx = 1005;
 
int n, m, ans, dep[mx], tin[mx], tout[mx], cnt, par[mx], pos[mx], deg[mx], dp[mx][(1<<10)]; 
vector<int> adj[mx]; vector<edge> pth;
 
void dfs(int i){ 
    tin[i] = cnt++;
    for (int x : adj[i]) 
        if (x != par[i])
            dep[x] = dep[i]+1, par[x] = i, pos[x] = (1<<deg[i]++), dfs(x); 
    tout[i] = cnt-1;
}int LCA(int a, int b){
    while (!(tin[a] <= tin[b] and tout[b] <= tout[a])) a = par[a];
    return a;
}
 
int main(){
    cin >> n >> m; 
    for (int i = 0; i < m; i++){
        int a, b, w; cin >> a >> b >> w; ans += w;
        if (!w) adj[a].push_back(b), adj[b].push_back(a);
        pth.push_back({a, b, w});
    }dfs(1); for (auto &r : pth) r.lca = LCA(r.a, r.b);
    sort(pth.begin(), pth.end(), [](edge x, edge y){ return tin[x.lca] > tin[y.lca]; });
    for (auto r : pth) if (!(r.w and (dep[r.a]+dep[r.b])%2)){
        int sum = r.w, p1 = 0, p2 = 0;
        for (int x = r.a; x != r.lca; p1 = pos[x], x = par[x]) sum += dp[x][p1];
        for (int x = r.b; x != r.lca; p2 = pos[x], x = par[x]) sum += dp[x][p2];
        for (int msk = 0; msk < (1<<deg[r.lca]); msk++)
            if (!(msk&p1) and !(msk&p2))
                dp[r.lca][msk] = max(dp[r.lca][msk], dp[r.lca][(msk|p1|p2)]+sum);
    }cout<<ans-dp[1][0]<<endl;    
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 4444 KB Output is correct
2 Correct 7 ms 4700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 856 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1116 KB Output is correct
2 Correct 2 ms 856 KB Output is correct
3 Correct 3 ms 1628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2140 KB Output is correct
2 Correct 6 ms 1148 KB Output is correct
3 Correct 4 ms 1628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 604 KB Output is correct
2 Correct 3 ms 1628 KB Output is correct
3 Correct 16 ms 4444 KB Output is correct
4 Correct 4 ms 1624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 2140 KB Output is correct
2 Correct 14 ms 4556 KB Output is correct
3 Correct 7 ms 1628 KB Output is correct
4 Correct 5 ms 1372 KB Output is correct