Submission #376202

# Submission time Handle Problem Language Result Execution time Memory
376202 2021-03-11T04:29:46 Z wiwiho Designated Cities (JOI19_designated_cities) C++14
0 / 100
1 ms 492 KB
#include <bits/stdc++.h>
 
#define eb emplace_back
#define printv(a, b) { \
    for(auto pv : a) b << pv << " "; \
    b << "\n"; \
}
#define mp make_pair
#define F first
#define S second
 
using namespace std;

typedef long long ll;
using pll = pair<ll, ll>;

struct edge{
    int a, b, c, d;
    ll get(int v){
        if(v == b) return c;
        else return d;
    }
    int ano(int v){
        return a ^ b ^ v;
    }
};

vector<vector<edge>> g;
vector<ll> dp, ans;

void dfs(int now, int p){
    //cerr << "dfs " << now << "\n";
    for(edge i : g[now]){
        int v = i.ano(now);
        if(v == p) continue;
        dfs(v, now);
        dp[now] += i.get(v) + dp[v];
    }
}

void dfs2(int now, int p){
    for(edge i : g[now]){
        int v = i.ano(now);
        if(v == p){
            //cerr << "dfs2 " << now << " " << p << " "  << ans[p] << " " << i.get(now) << " " << i.get(p) << "\n";
            ans[now] = ans[p] - i.get(now) + i.get(p);
            continue;
        }
    }
    for(edge i : g[now]){
        int v = i.ano(now);
        if(v != p) dfs2(v, now);
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
 
    int n;
    cin >> n;

    assert(n <= 16);
    
    g.resize(n + 1);
    dp.resize(n + 1);
    ans.resize(n + 1);

    for(int i = 0; i < n - 1; i++){
        int u, v, c, d;
        cin >> u >> v >> c >> d;
        g[u].eb(edge({u, v, c, d}));
        g[v].eb(edge({u, v, c, d}));
    }

    dfs(1, 1);
    ans[1] = dp[1];
    dfs2(1, 1);

    ll mn = 1LL << 60;
    for(int i = 1; i <= n; i++) mn = min(mn, ans[i]);
    //printv(dp, cerr);
    //printv(ans, cerr);

    int q;
    cin >> q;

    while(q--){
        int e;
        cin >> e;
        cout << mn << "\n";
    }
 
    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 364 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Runtime error 1 ms 492 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 364 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 364 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Runtime error 1 ms 492 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 364 KB Output isn't correct
2 Halted 0 ms 0 KB -