#include <iostream> #include <cstdio> #include <string> #include <vector> #include <set> #include <map> #include <queue> #include <cmath> #include <algorithm> using namespace std; #define REP(i,a,b) for(int i=(a);i<=(b);++i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define SORT(X) sort(X.begin(),X.end()) #define fi first #define se second int Ile[200200]; int Rep[400200]; int Kor[400200]; int Nas[400200][20]; int Zac[400200]; int S1[400200]; int S2[400200]; vector<pii> V[400200]; ll ans; int L; int In[400200],Out[400200]; void dfs(int u){ In[u] = ++L; if(S1[u] != 0){ dfs(S1[u]); dfs(S2[u]); } Out[u] = ++L; } int Find(int a){ if(Rep[a] != a) Rep[a] = Find(Rep[a]); return Rep[a]; } int LCA(int x, int y){ for(int i = 19;i>=0;i--){ if(In[Nas[x][i]] < In[y] && Out[Nas[x][i]] > Out[y]) {} else x = Nas[x][i]; } if(In[Nas[x][0]] < In[y] && Out[Nas[x][0]] > Out[y]) return Nas[x][0]; else return -1; } void Rea(int x){ int u = Nas[x][0]; if(u == x) return; if(Zac[u]){ FOR(i,SZ(V[u])){ int m = min(Ile[V[u][i].fi],Ile[V[u][i].se]); ans += 2*m; Ile[V[u][i].fi] -= m; Ile[V[u][i].se] -= m; } Rea(u); } Zac[u] = 1; } int main () { int m,n,k; scanf("%d%d%d",&n,&m,&k); FORI(i,n+m+10) Rep[i]=i; FORI(i,n+m+10) Nas[i][0]=i; FORI(i,n) scanf("%d",&Ile[i]); int curr = n; FOR(i,m){ int a,b; scanf("%d%d",&a,&b); a = Find(a); b = Find(b); Nas[a][0] = curr+1; Nas[b][0] = curr+1; Kor[a] = 1; Kor[b] = 1; Rep[a] = curr+1; Rep[b] = curr+1; curr++; S1[curr] = a; S2[curr] = b; } FORI(j,19) FORI(i,curr) if(Kor[i] == 0) Nas[i][j]=i; else Nas[i][j] = Nas[Nas[i][j-1]][j-1]; FORI(i,curr) if(Kor[i] == 0) dfs(i); FOR(i,k){ int a,b; scanf("%d%d",&a,&b); int x = LCA(a,b); if(x == -1) continue; V[x].pb(mp(a,b)); } FORI(i,n) Rea(i); printf("%lld\n", ans); }
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 | #include <iostream> #include <cstdio> #include <string> #include <vector> #include <set> #include <map> #include <queue> #include <cmath> #include <algorithm> using namespace std; #define REP(i,a,b) for(int i=(a);i<=(b);++i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define SORT(X) sort(X.begin(),X.end()) #define fi first #define se second int Ile[200200]; int Rep[400200]; int Kor[400200]; int Nas[400200][20]; int Zac[400200]; int S1[400200]; int S2[400200]; vector<pii> V[400200]; ll ans; int L; int In[400200],Out[400200]; void dfs(int u){ In[u] = ++L; if(S1[u] != 0){ dfs(S1[u]); dfs(S2[u]); } Out[u] = ++L; } int Find(int a){ if(Rep[a] != a) Rep[a] = Find(Rep[a]); return Rep[a]; } int LCA(int x, int y){ for(int i = 19;i>=0;i--){ if(In[Nas[x][i]] < In[y] && Out[Nas[x][i]] > Out[y]) {} else x = Nas[x][i]; } if(In[Nas[x][0]] < In[y] && Out[Nas[x][0]] > Out[y]) return Nas[x][0]; else return -1; } void Rea(int x){ int u = Nas[x][0]; if(u == x) return; if(Zac[u]){ FOR(i,SZ(V[u])){ int m = min(Ile[V[u][i].fi],Ile[V[u][i].se]); ans += 2*m; Ile[V[u][i].fi] -= m; Ile[V[u][i].se] -= m; } Rea(u); } Zac[u] = 1; } int main () { int m,n,k; scanf("%d%d%d",&n,&m,&k); FORI(i,n+m+10) Rep[i]=i; FORI(i,n+m+10) Nas[i][0]=i; FORI(i,n) scanf("%d",&Ile[i]); int curr = n; FOR(i,m){ int a,b; scanf("%d%d",&a,&b); a = Find(a); b = Find(b); Nas[a][0] = curr+1; Nas[b][0] = curr+1; Kor[a] = 1; Kor[b] = 1; Rep[a] = curr+1; Rep[b] = curr+1; curr++; S1[curr] = a; S2[curr] = b; } FORI(j,19) FORI(i,curr) if(Kor[i] == 0) Nas[i][j]=i; else Nas[i][j] = Nas[Nas[i][j-1]][j-1]; FORI(i,curr) if(Kor[i] == 0) dfs(i); FOR(i,k){ int a,b; scanf("%d%d",&a,&b); int x = LCA(a,b); if(x == -1) continue; V[x].pb(mp(a,b)); } FORI(i,n) Rea(i); printf("%lld\n", ans); } |