Submission #1114323

# Submission time Handle Problem Language Result Execution time Memory
1114323 2024-11-18T14:33:49 Z tintingyn21 Factories (JOI14_factories) C++17
Compilation error
0 ms 0 KB

// author : daohuyenchi



#ifdef LOCAL
    #include "D:\C++ Submit\debug.h"
#else
    #define debug(...)
#endif



#include <bits/stdc++.h>

using namespace std;

#define ull unsigned long long
#define db double
#define i32 int32_t  
#define i64 int64_t 
#define ll long long
// 
#define fi first
#define se second


//                                      #define int long long    // consider carefully


//
#define pii pair<int, int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define pil pair<int, ll> 
#define PAIR make_pair 
//                                      TIME IS LIMITED ...  
#define  rep(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define repd(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define repv(v, H) for(auto &v: H)
//                                      REFLECT ON THE PAST ...
#define RESET(c, x) memset(c, x, sizeof(c))
#define MASK(i) (1LL << (i))
#define BIT(mask, i) (((mask) >> (i)) & 1LL)
#define ONBIT(mask, i) ((mask) | (1LL << (i)))
#define OFFBIT(mask, i) ((mask) &~ (1LL << (i)))
#define COUNTBIT __builtin_popcountll
//                                      30 / 1 / 2024 ? love is zero... start from zero 
#define vi vector <int>
#define vll vector <ll>
#define lwb lower_bound
#define upb upper_bound
#define all(v) (v).begin(), (v).end()
#define special(H) (H).resize(distance(H.begin(), unique(all(H))))
//                                      
#define sp ' ' 
#define nl '\n'
#define EL {cerr << '\n';}
#define yes "YES"
#define no "NO"
#define Log2(n) (63 - __builtin_clzll(n))
#define left __left__
#define right __right__ 
#define lps(id) ((id) << 1)
#define rps(id) ((id) << 1 | 1)

//____________________________________________________________________


template <class X, class Y> bool maximize(X &a, const Y &b) {
    if(a < b) return a = b, true;
    return false;
}

template <class X, class Y> bool minimize(X &a, const Y &b) {
    if(a > b) return a = b, true;
    return false;
}

template <class... T>
void print(T&&... n) {
    using exp = int[];
    exp{0, (cerr << n << sp, 0)...};
    cerr << nl;
}

template <class T, class... C>
void assign(int n, T v, C&&... a) {
    using e = int[];
    e{(a.assign(n, v), 0)...};
}

template <class... C>
void resize(int n, C&&... a) {
    using e = int[];
    e{(a.resize(n), 0)...};
}

template <class T>
using vector2d = vector<vector<T>>;
template <class T>
using vector3d = vector<vector2d<T>>;

template <class T> int ssize(T &a) { return (int) a.size(); }


//____________________________________________________________________


mt19937 rng(chrono::steady_clock().now().time_since_epoch().count());

const int MOD      = 1000000007;
// const int MOD[2] = {1000000009, 998244353};

template<class X> void modmize(X &x, int cur_Mod = MOD) {
    if(x >= cur_Mod) x -= cur_Mod;
    if(x < 0) x += cur_Mod;
}

const long long oo = 1e18 + 7;
const long long LINF = 1e18 + 7;
const int IINF      = 2e9;
const int nmax     = 1e6 + 10;
const int MAX      = 2e5;
const int base     = 311;
const db eps       = 1e-6;
const int block    = 500;
static const double PI = acos(-1.0);

//____________________________________________________________________


int n, Q;
int a[nmax], b[nmax], wei[nmax];
int S, T, X[nmax], Y[nmax];
vector <pii> G[nmax];
int par[nmax][22];


struct LCA_Euler {
    int timer = 0;
    int ein[nmax], tour[nmax * 2], dep[nmax];
 
    int min_node(int u, int v) {
        return dep[u] <= dep[v] ? u : v;
    }
 
    void dfs(int u, int p) {
        ein[u] = ++timer;
        tour[timer] = u;
        dep[u] = dep[p] + 1;
        repv (e, G[u]) {
            int v = e.se;
            if (v == p) continue;
            dfs(v, u);
            tour[++timer] = u;
        }
    }
 
    int r_lca[nmax][22];
    void build_lca() {
 
        dfs(1, 0);
 
        rep (i, 1, timer) {
            r_lca[i][0] = tour[i];
        }
 
        rep (k, 1, 20) {
            for (int i = 1; i + (1 << k) - 1 <= timer; ++i) {
                r_lca[i][k] = min_node(r_lca[i][k - 1], r_lca[i + (1 << (k - 1))][k - 1]);
            }
        }
    }
 
    int get_lca(int u, int v) {
        int l = ein[u];
        int r = ein[v];
        if (l > r) swap (l, r);
        int k = Log2(r - l + 1);
        return min_node(r_lca[l][k], r_lca[r - (1 << k) + 1][k]);
    }
 
} lca;
 
ll pf[nmax];
void dfs(int u, int p) {
    repv (e, G[u]) {
        int v = e.se;
        int w = e.fi;
        if (v == p) continue;
        pf[v] = pf[u] + w;
        dfs(v, u);
    }
}
 
ll get_dist(int u, int v) {
    int anc = lca.get_lca(u, v);
    return pf[u] + pf[v] - 2ll * pf[anc];
}


int child[nmax];
bool del[nmax];
int dfs_sz(int u, int p) {
    child[u] = 1;
    repv (e, G[u]) {
        int v = e.se;
        int w = e.fi;
        if (del[v] || v == p) continue;
        child[u] += dfs_sz(v, u);
    }
    return child[u];
}

int Find_cent(int u, int p, int tar) {
    repv (e, G[u]) {
        int v = e.se;
        if (v == p || del[v] || child[v] <= tar) continue;
        return Find_cent(v, u, tar);
    }
    return u ;
}

void solve(int u, int pa) {

    int root = Find_cent(u, 0, dfs_sz(u, 0) / 2);
    del[root] = 1;
    par[root][0] = pa;

    repv (e, G[root]) {
        int v = e.se;
        if (del[v]) continue;
        solve(v, root);
    }
}

ll d[nmax];
void Init(int n, int a[], int b[], int wei[]) {

    rep (i, 0, n - 2) {
        ++a[i];
        ++b[i];
        G[a[i]].push_back({wei[i], b[i]});
        G[b[i]].push_back({wei[i], a[i]});
    }
    
    lca.build_lca();
    dfs(1, 0);
    solve(1, 0);

    rep (i, 1, n) {
        d[i] = LINF;
    }

}


void upd(int u) {
    int u0 = u;
    while (u != 0) {
        // debug(u);
        minimize(d[u], get_dist(u, u0));
        u = par[u][0];
    }   
}

void upd_init(int u) {
    while (u != 0) {
        d[u] = LINF;
        u = par[u][0];
    }
}
 
ll get(int u) {
    int u0 = u;
    ll ans = d[u];
    while (u != 0) {
        minimize(ans, d[u] + get_dist(u, u0));
        u = par[u][0];
    }
    return ans;
}

ll Query(int S, int X[], int T, int Y[]) {

    rep (i, 0, S - 1) ++X[i];
    rep (i, 0, T - 1) ++Y[i];

    rep (i, 0, S - 1) {
        upd(X[i]);
    }

    ll ans = LINF;
    rep (i, 0, T - 1) {
        minimize(ans, get(Y[i]));
    }

    rep (i, 0, S - 1) {
        upd_init(X[i]);
    }

    return ans;

}

void tintingyn() {
 
    cin >> n >> Q;
 
    rep (i, 0, n - 2) {
        cin >> a[i] >> b[i] >> wei[i];
        // print(a[i] + 1, b[i] + 1, wei[i]);
    }
 
    Init(n, a, b, wei);
 
    rep (_i, 0, Q - 1) {
        int S, T;
        cin >> S >> T;
        // debug(S, T);
        rep (i, 0, S - 1) cin >> X[i];
        rep (i, 0, T - 1) cin >> Y[i];
 
        // rep (i, 0, S - 1) {
        //     debug(X[i] + 1);
        // }
        // rep (i, 0, T - 1) {
        //     debug(Y[i] + 1);
        // }
 
        cout << Query(S, X, T, Y) << nl;
    }   
 
}
 
signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
 
    //________________________________________________________________
 
    #define TASK "3"
    if(fopen(TASK".inp", "r")) {
        freopen(TASK".inp", "r", stdin);
        freopen(TASK".out", "w", stdout);
    }
 
    //________________________________________________________________
    
    //  CODE FROM HERE ...! 
 
 
 
 
 
    int num_test = 1; 
    // cin >> num_test;
 
    while(num_test--) {
 
        tintingyn();
 
    }
 
 
    cerr << '\n' << "Time elapsed: " << (1.0 * clock() / CLOCKS_PER_SEC) << " s\n" << nl;
 
    return 0;
}

Compilation message

factories.cpp: In function 'int dfs_sz(int, int)':
factories.cpp:210:13: warning: unused variable 'w' [-Wunused-variable]
  210 |         int w = e.fi;
      |             ^
factories.cpp: In function 'int main()':
factories.cpp:347:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  347 |         freopen(TASK".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
factories.cpp:348:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  348 |         freopen(TASK".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/ld: /tmp/ccvPpgeQ.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/cc2jL86S.o:factories.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status