Submission #792546

#TimeUsernameProblemLanguageResultExecution timeMemory
792546vjudge1Two Currencies (JOI23_currencies)C++14
38 / 100
749 ms93676 KiB
#include<bits/stdc++.h> #define ll long long #define fi first #define se second using namespace std ; const ll N = 1e5, NS = 2e3 ; bool flag1 ; ll n, m, q, ans, p[N + 1], c[N + 1], ind[N + 1], pw[2 * N + 1], dist1[N + 1], dist[N + 1] ; pair<ll, ll> mn[2 * N + 1][20] ; map<pair<ll, ll>, vector<ll>> mp ; vector<ll> now, tree[NS + 1] ; vector<pair<pair<ll, ll>, ll>> road ; vector<pair<ll, ll>> v1, v[N + 1] ; void dfs_perebor(ll city, ll last, ll t, ll x, ll y) { if(city == t) { ll kol = 0 ; sort(now.begin(), now.end()) ; for(ll i : now) { if(y >= i) y -= i, kol++ ; else break ; } ans = max(-1ll, x - ((ll)now.size() - kol)) ; return ; } for(ll i : tree[city]) { if(i == last) continue ; for(ll j : mp[{city, i}]) now.push_back(j) ; dfs_perebor(i, city, t, x, y) ; for(ll j : mp[{city, i}]) now.pop_back() ; } } void dfs_for_st(ll city, ll last) { ind[city] = v1.size() ; v1.push_back({dist1[city], city}) ; for(auto i : v[city]) { if(i.fi == last) continue ; dist[i.fi] = dist[city] + i.se ; dist1[i.fi] = dist1[city] + 1 ; dfs_for_st(i.fi, city) ; v1.push_back({dist1[city], city}) ; } } void build_sparce_table() { for(ll i = 2 ; i <= (ll)v1.size() ; i++) pw[i] = pw[i / 2] + 1 ; for(ll i = 0 ; i < (ll)v1.size() ; i++) mn[i][0] = v1[i] ; for(ll i = 1 ; i < 20 ; i++) for(ll j = 0 ; j <= (ll)v1.size() - (1 << i) ; j++) mn[j][i] = min(mn[j][i - 1], mn[j + (1 << (i - 1))][i - 1]) ; } ll get_lca(ll a, ll b) { a = ind[a] ; b = ind[b] ; if(a > b)swap(a, b) ; ll num = pw[b - a + 1] ; return min(mn[a][num], mn[b - (1 << num) + 1][num]).se ; } ll get_dist(ll a, ll b) { ll lc = get_lca(a, b) ; return dist[a] + dist[b] - dist[lc] * 2ll ; } signed main() { ios_base::sync_with_stdio( 0 ) ; cin.tie( 0 ) ; cout.tie( 0 ) ; cin >> n >> m >> q ; for(ll i = 1 ; i < n ; i++) { ll a, b ; cin >> a >> b ; if(n <= 2000 && m <= 2000 && q <= 2000) { tree[a].push_back(b) ; tree[b].push_back(a) ; } road.push_back({{a, b}, 0}) ; } for(ll i = 1 ; i <= m ; i++) { cin >> p[i] >> c[i] ; if(n <= 2000 && m <= 2000 && q <= 2000) { pair<ll, ll> pr = road[p[i] - 1].fi ; mp[pr].push_back(c[i]) ; mp[{pr.se, pr.fi}].push_back(c[i]) ; } if(c[i] != c[1]) flag1 = 1 ; } if(!flag1) { for(ll i = 1 ; i <= m ; i++) road[p[i] - 1].se += c[i] ; for(auto i : road) v[i.fi.fi].push_back({i.fi.se, i.se}), v[i.fi.se].push_back({i.fi.fi, i.se}) ; dfs_for_st(1, 0) ; build_sparce_table() ; while(q--) { ll s, t, x, y ; cin >> s >> t >> x >> y ; ll ds = get_dist(s, t) ; ds /= c[1] ; y /= c[1] ; ds -= min(ds, y) ; cout << max(-1ll, x - ds) << '\n' ; } return 0 ; } if(n <= 2000 && m <= 2000 && q <= 2000) { while(q--) { ll s, t, x, y ; cin >> s >> t >> x >> y ; dfs_perebor(s, 0, t, x, y) ; cout << ans << '\n' ; } return 0 ; } return 0 ; }

Compilation message (stderr)

currencies.cpp: In function 'void dfs_perebor(long long int, long long int, long long int, long long int, long long int)':
currencies.cpp:37:16: warning: unused variable 'j' [-Wunused-variable]
   37 |         for(ll j : mp[{city, i}])
      |                ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...