//Krzysztof Pieprzak #include <iostream> #include <fstream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cmath> #include <string> #include <cstring> #include <vector> #include <queue> #include <set> #include <list> #include <deque> #include <stack> #include <map> #include <ctime> #include <cassert> using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long ull; typedef unsigned int uint; typedef vector<int> vi; typedef vector<ll> vll; typedef pair<int,int> pii; typedef pair<long long, long long> pll; typedef vector<pii> vpii; typedef vector<pll> vpll; #define Size(x) (int)x.size() #define VAR(v,n) typeof(n)v = (n) #define FOR(i,a,b) for(VAR(i,a); i < (b); ++i) #define FORE(i,a,b) for(VAR(i,a); i <= (b); ++i) #define FORREV(i,a,b) for(VAR(i,b); i >= (a); --i) #define FORSTEP(i,a,b,step) for(VAR(i,a); i < (b); i += (step)) #define FOREACH(i,c) for(VAR(i,(c).begin()); i != (c).end(); ++i) #define ALL(x) x.begin(),x.end() #define CLEAR(t) memset(t, 0, sizeof(t)) #define F first #define S second #define MP make_pair #define PUB push_back #define POB pop_back #define pieprzu ios_base::sync_with_stdio(0); #define DEBUG if(0) const int INF = 1000000001; const double EPS = 10e-9; const int MAXN = 200005; const int MAXK = 500005; namespace LCA { const int MAX_N = MAXN; const int MAX_LOG_N = 18; vpii t[MAX_N]; bool vis[MAX_N]; int treeSize[MAX_N]; int preOrderId[MAX_N]; int vertexLvl[MAX_N]; pii ancestor[MAX_LOG_N + 1][MAX_N]; int dfs(int v, int &id, int lvl, pii father) { vis[v] = 1; preOrderId[v] = id++; treeSize[v] = 1; vertexLvl[v] = lvl; ancestor[0][v] = father; FOREACH (w, t[v]) { if (!vis[w->F]) { treeSize[v] += dfs(w->F, id, lvl + 1, MP(v,w->S)); } } return treeSize[v]; } inline void preLca(int n) { int id = 0; dfs(0, id, 0, MP(0,-1)); FOR (i, 1, MAX_LOG_N + 1) { FOR (j, 0, n) { ancestor[i][j] = ancestor[i - 1][ancestor[i - 1][j].F]; } } } inline bool isDescendant(int a, int b) { return preOrderId[a] >= preOrderId[b] && preOrderId[a] < preOrderId[b] + treeSize[b]; } inline int getEdge(int a, int b) { for (int i = MAX_LOG_N; i >= 0;) { if (ancestor[i][a].F != b && isDescendant(ancestor[i][a].F,b)) a = ancestor[i][a].F; else --i; } return ancestor[0][a].S; } inline pii getLcaEdges(int a, int b) { if (isDescendant(a, b)) return MP(getEdge(a,b),-1); if (isDescendant(b, a)) return MP(-1,getEdge(b,a)); for (int i = MAX_LOG_N; i >= 0;) { if (isDescendant(b, ancestor[i][a].F)) --i; else a = ancestor[i][a].F; } DEBUG printf("LCA(%d,%d) = %d\n", a, b, ancestor[0][a].F); return MP(ancestor[0][a].S,getEdge(b,ancestor[0][a].F)); } inline void clear(int n) { FOR (i, 0, n) { t[i].clear(); vis[i] = false; } } } int g[MAXN]; pii ab[MAXN]; pair<pii,pii> cd[MAXK]; bool out[MAXN]; inline int update(pii vs) { int res = min(g[vs.F],g[vs.S]); g[vs.F] -= res; g[vs.S] -= res; return res*2; } void rob() { int n,m,k; scanf("%d %d %d", &n, &m, &k); LCA::clear(n+1); FORE (i, 1, n) scanf("%d", &g[i]); int a,b; FOR (i, 0, m) { scanf("%d %d", &a, &b); LCA::t[a].PUB(MP(b,i)); LCA::t[b].PUB(MP(a,i)); ab[i] = MP(a,b); out[a] = 1; } FORE (i, 1, n) if (!out[i]) { LCA::t[0].PUB(MP(i,-1)); LCA::t[i].PUB(MP(0,-1)); } DEBUG FORE (i, 0, n) { printf("(%d): ", i); FOREACH (w, LCA::t[i]) printf("%d ", w->F); printf("\n"); } LCA::preLca(n+1); pii tmp; int kk = 0; FOR (i, 0, k) { scanf("%d %d", &a, &b); tmp = LCA::getLcaEdges(a,b); if (tmp.F != -1 || tmp.S != -1) cd[kk++] = MP(MP(max(tmp.F,tmp.S),i),MP(a,b)); } k = kk; sort(cd,cd+k); DEBUG { printf("cd: ");FOR (i, 0, k) printf("[(%d,%d),(%d,%d)] ", cd[i].F.F, cd[i].F.S, cd[i].S.F, cd[i].S.S);printf("\n"); } ll res = 0; FOR (i, 0, k) res += update(cd[i].S); printf("%lld\n", res); } int main() { int test = 1; //scanf("%d", &test); while (test--) rob(); return 0; }
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | //Krzysztof Pieprzak #include <iostream> #include <fstream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cmath> #include <string> #include <cstring> #include <vector> #include <queue> #include <set> #include <list> #include <deque> #include <stack> #include <map> #include <ctime> #include <cassert> using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long ull; typedef unsigned int uint; typedef vector<int> vi; typedef vector<ll> vll; typedef pair<int,int> pii; typedef pair<long long, long long> pll; typedef vector<pii> vpii; typedef vector<pll> vpll; #define Size(x) (int)x.size() #define VAR(v,n) typeof(n)v = (n) #define FOR(i,a,b) for(VAR(i,a); i < (b); ++i) #define FORE(i,a,b) for(VAR(i,a); i <= (b); ++i) #define FORREV(i,a,b) for(VAR(i,b); i >= (a); --i) #define FORSTEP(i,a,b,step) for(VAR(i,a); i < (b); i += (step)) #define FOREACH(i,c) for(VAR(i,(c).begin()); i != (c).end(); ++i) #define ALL(x) x.begin(),x.end() #define CLEAR(t) memset(t, 0, sizeof(t)) #define F first #define S second #define MP make_pair #define PUB push_back #define POB pop_back #define pieprzu ios_base::sync_with_stdio(0); #define DEBUG if(0) const int INF = 1000000001; const double EPS = 10e-9; const int MAXN = 200005; const int MAXK = 500005; namespace LCA { const int MAX_N = MAXN; const int MAX_LOG_N = 18; vpii t[MAX_N]; bool vis[MAX_N]; int treeSize[MAX_N]; int preOrderId[MAX_N]; int vertexLvl[MAX_N]; pii ancestor[MAX_LOG_N + 1][MAX_N]; int dfs(int v, int &id, int lvl, pii father) { vis[v] = 1; preOrderId[v] = id++; treeSize[v] = 1; vertexLvl[v] = lvl; ancestor[0][v] = father; FOREACH (w, t[v]) { if (!vis[w->F]) { treeSize[v] += dfs(w->F, id, lvl + 1, MP(v,w->S)); } } return treeSize[v]; } inline void preLca(int n) { int id = 0; dfs(0, id, 0, MP(0,-1)); FOR (i, 1, MAX_LOG_N + 1) { FOR (j, 0, n) { ancestor[i][j] = ancestor[i - 1][ancestor[i - 1][j].F]; } } } inline bool isDescendant(int a, int b) { return preOrderId[a] >= preOrderId[b] && preOrderId[a] < preOrderId[b] + treeSize[b]; } inline int getEdge(int a, int b) { for (int i = MAX_LOG_N; i >= 0;) { if (ancestor[i][a].F != b && isDescendant(ancestor[i][a].F,b)) a = ancestor[i][a].F; else --i; } return ancestor[0][a].S; } inline pii getLcaEdges(int a, int b) { if (isDescendant(a, b)) return MP(getEdge(a,b),-1); if (isDescendant(b, a)) return MP(-1,getEdge(b,a)); for (int i = MAX_LOG_N; i >= 0;) { if (isDescendant(b, ancestor[i][a].F)) --i; else a = ancestor[i][a].F; } DEBUG printf("LCA(%d,%d) = %d\n", a, b, ancestor[0][a].F); return MP(ancestor[0][a].S,getEdge(b,ancestor[0][a].F)); } inline void clear(int n) { FOR (i, 0, n) { t[i].clear(); vis[i] = false; } } } int g[MAXN]; pii ab[MAXN]; pair<pii,pii> cd[MAXK]; bool out[MAXN]; inline int update(pii vs) { int res = min(g[vs.F],g[vs.S]); g[vs.F] -= res; g[vs.S] -= res; return res*2; } void rob() { int n,m,k; scanf("%d %d %d", &n, &m, &k); LCA::clear(n+1); FORE (i, 1, n) scanf("%d", &g[i]); int a,b; FOR (i, 0, m) { scanf("%d %d", &a, &b); LCA::t[a].PUB(MP(b,i)); LCA::t[b].PUB(MP(a,i)); ab[i] = MP(a,b); out[a] = 1; } FORE (i, 1, n) if (!out[i]) { LCA::t[0].PUB(MP(i,-1)); LCA::t[i].PUB(MP(0,-1)); } DEBUG FORE (i, 0, n) { printf("(%d): ", i); FOREACH (w, LCA::t[i]) printf("%d ", w->F); printf("\n"); } LCA::preLca(n+1); pii tmp; int kk = 0; FOR (i, 0, k) { scanf("%d %d", &a, &b); tmp = LCA::getLcaEdges(a,b); if (tmp.F != -1 || tmp.S != -1) cd[kk++] = MP(MP(max(tmp.F,tmp.S),i),MP(a,b)); } k = kk; sort(cd,cd+k); DEBUG { printf("cd: ");FOR (i, 0, k) printf("[(%d,%d),(%d,%d)] ", cd[i].F.F, cd[i].F.S, cd[i].S.F, cd[i].S.S);printf("\n"); } ll res = 0; FOR (i, 0, k) res += update(cd[i].S); printf("%lld\n", res); } int main() { int test = 1; //scanf("%d", &test); while (test--) rob(); return 0; } |