답안 #624394

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
624394 2022-08-08T07:11:18 Z radal Designated Cities (JOI19_designated_cities) C++17
0 / 100
219 ms 42328 KB
#include <bits/stdc++.h>
#pragma GCC target("sse,sse2,avx2")
#pragma GCC optimize("unroll-loops,O2")
#define rep(i,l,r) for (int i = l; i < r; i++)
#define repr(i,r,l) for (int i = r; i >= l; i--)
#define X first
#define Y second
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define endl '\n'
#define debug(x) cerr << #x << " : " << x << endl;
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pll;
constexpr int N = 2e5+10,mod = 998244353,inf = 1e9+10;
inline int mkay(int a,int b){
    if (a+b >= mod) return a+b-mod;
    if (a+b < 0) return a+b+mod;
    return a+b;
}
 
inline int poww(int a,int k){
    if (k < 0) return 0;
    int z = 1;
    while (k){
        if (k&1) z = 1ll*z*a%mod;
        a = 1ll*a*a%mod;
        k /= 2;
    } 
    return z; 
}

int par[N][20],h[N];
ll opt[N],d[N][2],up[N];
vector<pair<int,pll>> adj[N];
void pre(int v,int p){
    par[v][0] = p;
    for (auto u : adj[v]){
        if (u.X == p) continue;
        h[u.X] = h[v]+1;
        d[u.X][0] = d[v][0]+u.Y.X;
        d[u.X][1] = d[v][1]+u.Y.Y;
        pre(u.X,v);
        up[v] += up[u.X]+u.Y.Y;
    }
}
int main(){
    ios :: sync_with_stdio(0); cin.tie(0);
    memset(opt,63,sizeof opt);
    int n;
    cin >> n;
    ll sum = 0;
    rep(i,1,n){
        int u,v,a,b;
        cin >> u >> v >> a >> b;
        adj[u].pb({v,{a,b}});
        adj[v].pb({u,{b,a}});
        sum += a;
        sum += b;
    }
    if (n == 2){
        int q;
        cin >> q;
        while (q--){
            int t;
            cin >> t;
            if (t == 2){
                cout << 0 << endl;
                continue;
            }
            if (t == 1){
                cout << min(adj[1][0].Y.Y,adj[1][0].Y.X) << endl;
                continue;
            }
        }
        return 0;
    }
    int r = 1;
    rep(i,2,n+1) if (adj[i].size() > 1) r = i;
    pre(r,0);
    rep(j,1,20){
        rep(i,1,n+1){
            par[i][j] = par[par[i][j-1]][j-1];
        }
    }
    rep(i,1,n+1){
        opt[1] = min(opt[1],sum-up[r]-d[i][1]+d[i][0]);
    }
    cout << opt[1];
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6484 KB Output is correct
2 Incorrect 4 ms 6484 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6596 KB Output is correct
2 Incorrect 219 ms 42328 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6484 KB Output is correct
2 Incorrect 207 ms 42280 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6484 KB Output is correct
2 Incorrect 4 ms 6484 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6596 KB Output is correct
2 Incorrect 219 ms 42328 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6484 KB Output is correct
2 Incorrect 4 ms 6484 KB Output isn't correct
3 Halted 0 ms 0 KB -