1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| #include "swap.h" #include <bits/stdc++.h> #include <bits/extc++.h> #define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define iter(a) a.begin(), a.end() #define riter(a) a.rbegin(), a.rend() #define lsort(a) sort(iter(a)) #define gsort(a) sort(riter(a)) #define pb(a) push_back(a) #define eb(a) emplace_back(a) #define pf(a) push_front(a) #define pob pop_back() #define pof pop_front() #define mp(a, b) make_pair(a, b) #define F first #define S second #define mt make_tuple #define gt(t, i) get<i>(t) #define iceil(a, b) ((a + b - 1) / b) #define tomax(a, b) (a = max(a, b)) #define printv(a, b) {bool pvaspace=false; \ for(auto pva : a){ \ if(pvaspace) b << " "; pvaspace=true;\ b << pva;\ }\ b << "\n";}
using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef unsigned long long ull; typedef long double ld; using pii = pair<int, int>; using pll = pair<ll, ll>; using pdd = pair<ld, ld>; using tiii = tuple<int, int, int>; const ll MOD = 1000000007; const ll MAX = 2147483647; template<typename A, typename B> ostream& operator<<(ostream& o, pair<A, B> p){ return o << '(' << p.F << ',' << p.S << ')'; } vector<int> dsu, one, three; vector<vector<int>> anc, g; vector<int> ans, in, out;
int findDSU(int a){ if(dsu[a] != a) dsu[a] = findDSU(dsu[a]); return dsu[a]; } void unionDSU(int a, int b){ a = findDSU(a); b = findDSU(b); if(a == b) return; dsu[b] = a; one[a] += one[b]; three[a] += three[b]; } int dfsts = 0; void dfs(int now, int p){ in[now] = dfsts++; if(ans[now] == -1) ans[now] = ans[p]; for(int i : g[now]) dfs(i, now); out[now] = dfsts++; } bool isAnc(int a, int b){ return in[a] <= in[b] && out[a] >= out[b]; } int lca(int a, int b){ if(isAnc(a, b)) return a; for(int i = 19; i >= 0; i--){ if(!isAnc(anc[a][i], b)) a = anc[a][i]; } return anc[a][0]; } void init(int N, int M, std::vector<int> U, std::vector<int> V, std::vector<int> W) { vector<pair<int, pii>> e(M); for(int i = 0; i < M; i++){ e[i] = mp(W[i], mp(U[i], V[i])); } lsort(e); dsu.resize(N + M); for(int i = 0; i < N + M; i++) dsu[i] = i; one.resize(N + M); three.resize(N + M); int ts = N; anc.resize(N + M, vector<int>(20)); for(int i = 0; i < N + M; i++) anc[i][0] = i; ans.resize(N + M, -1); g.resize(N + M); in.resize(N + M); out.resize(N + M); vector<int> deg(N); for(auto i : e){ int w = i.F, u = i.S.F, v = i.S.S; if(deg[u] == 1) one[findDSU(u)]--; if(deg[v] == 1) one[findDSU(v)]--; deg[u]++; deg[v]++; if(deg[u] == 1) one[findDSU(u)]++; else if(deg[u] == 3) three[findDSU(u)]++; if(deg[v] == 1) one[findDSU(v)]++; else if(deg[v] == 3) three[findDSU(v)]++; u = findDSU(u); v = findDSU(v); int dv = ts++; if(u == v){ unionDSU(dv, u); anc[u][0] = dv; g[dv].eb(u); } else{ unionDSU(dv, u); unionDSU(dv, v); anc[u][0] = anc[v][0] = dv; g[dv].eb(u); g[dv].eb(v); } if(!one[dv] || three[dv]) ans[dv] = w; }
for(int i = 0; i < N + M; i++){ if(anc[i][0] == i) dfs(i, i); } for(int i = 1; i < 20; i++){ for(int j = 0; j < N + M; j++){ anc[j][i] = anc[anc[j][i - 1]][i - 1]; } } } int getMinimumFuelCapacity(int X, int Y) { if(findDSU(X) != findDSU(Y)) return -1; return ans[lca(X, Y)]; }
|