답안 #591049

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
591049 2022-07-06T19:06:14 Z Lobo Designated Cities (JOI19_designated_cities) C++17
0 / 100
305 ms 35872 KB
#include<bits/stdc++.h>
using namespace std;

const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()

const int maxn = 2e5+10;

int n, q, up[maxn], dw[maxn], h[maxn], ans[maxn], tin[maxn], tout[maxn], timer;
pair<int,int> pai[maxn];
vector<int> g[maxn];
vector<pair<pair<int,int>,pair<int,int>>> edg;

void dfsh(int u, int ant) {
    tin[u] = ++timer;
    for(auto id : g[u]) {
        int v = edg[id].fr.fr^edg[id].fr.sc^u;
        if(v == ant) continue;
        if(v == edg[id].fr.fr) {
            swap(edg[id].fr.fr,edg[id].fr.sc);
            swap(edg[id].sc.fr,edg[id].sc.sc);
            //edg[i].sc.fr -> descendo
            //edg[i].sc.sc -> subindo
        }
        h[v] = h[u]+1;
        pai[v] = mp(u,id);
        dfsh(v,u);
    }
    tout[u] = timer;
}

void dfsdu(int u, int ant) {
    for(auto id : g[u]) {
        int v = edg[id].fr.fr^edg[id].fr.sc^u;
        if(v == ant) continue;
        dw[v] = dw[u]+edg[id].sc.fr;
        up[v] = up[u]+edg[id].sc.sc;
        dfsdu(v,u);
    }
}

void solve() {
    cin >> n;
    ans[0] = 0;
    for(int i = 0; i < n-1; i++) {
        int u,v,w1,w2;
        cin >> u >> v >> w1 >> w2;
        g[u].pb(i);
        g[v].pb(i);
        ans[0]+= w1+w2;
        edg.pb(mp(mp(u,v),mp(w1,w2)));
    }
    dfsh(1,0);

    for(int j = 1; j <= 2; j++) {
        pair<int,int> ans1 = mp(-inf,0);
        dfsdu(1,0);
        int tup = 0;
        for(int i = 0; i < n-1; i++) {
            tup+= edg[i].sc.sc;
        }
        for(int i = 1; i <= n; i++) {
            ans1 = max(ans1,mp(tup+dw[i]-up[i],i));
        }
        ans[j] = ans[j-1]-ans1.fr;
        int v = ans1.sc;
        // tira as arestas pegadas
        for(int i = 2; i <= n; i++) {
            //muda a aresta de i para pai[i].fr
            int id = pai[i].sc;
            if(tin[v] >= tin[i] && tout[v] <= tout[i]) {
                //se v é filho de i -> tira a aresta descendo
                edg[id].sc.fr = 0;
            }
            else {
                edg[id].sc.sc = 0;
            }
        }
    }
    cin >> q;
    
    cout << ans[2] << endl;
}

int32_t main() {
    ios::sync_with_stdio(false); cin.tie(0);

    // freopen("in.in", "r", stdin);
    // freopen("out.out", "w", stdout);
    int tt = 1;
    // cin >> tt;
    while(tt--) {
        solve();
    }

}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 4948 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 4948 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Incorrect 305 ms 35872 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 4948 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 4948 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 4948 KB Output isn't correct
2 Halted 0 ms 0 KB -