#include<iostream> #include<algorithm> #include<vector> using namespace std; #define ll long long #define ld long double #define vi vector<int> #define pb push_back #define pii pair<int,int> #define mp make_pair #define st first #define nd second #define mini(a,b) a=min(a,(b)) #define maxi(a,b) a=max(a,(b)) #define RE(i,n) for(int i=0,_n=(n);i<_n;++i) #define RI(i,n) for(int i=1,_n=(n);i<=_n;++i) const int inf=1e9+5, nax=400123, pot = (1 << 20); // jakie ma być pot? iurghwouwihfhg3rouhwfjpoh3vwrowuhfdushwgoruhfsjvdoisjr int n, ile_krokow, ile_par; int waga[nax]; int nr_v[nax], gr[nax]; vi w[nax]; int ojc[nax], prawy[nax], lewy[nax], h[nax]; int ile_wierz; int tr[2 * pot + 5]; int licznik; int odw[nax]; vector<pii > event[nax]; ll RES; void dfs(int x) { tr[pot + licznik] = x; odw[x] = licznik; licznik++; if(lewy[x] == 0) return; h[lewy[x]] = h[prawy[x]] = h[x] + 1; dfs(lewy[x]); tr[pot + licznik] = x; licznik++; dfs(prawy[x]); tr[pot + licznik] = x; licznik++; } int LCA(int a, int b) { a = odw[a] + pot; b = odw[b] + pot; if(a > b) swap(a, b); int res = tr[a]; if(h[tr[b]] < h[res]) res = tr[b]; while(a < b - 1) { if(a % 2 == 0 && h[tr[a+1]] < h[res]) res = tr[a+1]; if(b % 2 && h[tr[b-1]] < h[res]) res = tr[b-1]; a /= 2; b /= 2; } return res; } void res_dfs(int x) { if(lewy[x]) { res_dfs(lewy[x]); res_dfs(prawy[x]); } RE(i, event[x].size()) { int a = event[x][i].st, b = event[x][i].nd; int m = min(waga[a], waga[b]); RES += 2 * m; waga[a] -= m; waga[b] -= m; } } int main() { ios_base::sync_with_stdio(0); cin >> n >> ile_krokow >> ile_par; RI(i, n) cin >> waga[i]; RI(i, n) { gr[i] = i; w[i].pb(i); } ile_wierz = n; RE(_, ile_krokow) { int a, b; cin >> a >> b; a = gr[a]; b = gr[b]; if(w[a].size() > w[b].size()) swap(a, b); // 'a' wrzucamy do 'b' RE(i, w[a].size()) { w[b].pb( w[a][i] ); gr[ w[a][i] ] = b; } int v_a = nr_v[a], v_b = nr_v[b]; ile_wierz++; lewy[ile_wierz] = v_a; prawy[ile_wierz] = v_b; ojc[v_a] = ojc[v_b] = ile_wierz; nr_v[b] = ile_wierz; } RI(i, ile_wierz) if(ojc[i] == 0) dfs(i); h[0] = inf; for(int i = pot - 1; i > 0; --i) { if(h[tr[2 * i]] < h[tr[2 * i + 1]]) tr[i] = tr[2 * i]; else tr[i] = tr[2 * i + 1]; } RE(i, ile_par) { int a, b; cin >> a >> b; if(gr[a] == gr[b]) { int x = LCA(a, b); event[x].pb(mp(a,b)); } } RI(i, ile_wierz) if(ojc[i] == 0) res_dfs(i); cout << RES << "\n"; 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 | #include<iostream> #include<algorithm> #include<vector> using namespace std; #define ll long long #define ld long double #define vi vector<int> #define pb push_back #define pii pair<int,int> #define mp make_pair #define st first #define nd second #define mini(a,b) a=min(a,(b)) #define maxi(a,b) a=max(a,(b)) #define RE(i,n) for(int i=0,_n=(n);i<_n;++i) #define RI(i,n) for(int i=1,_n=(n);i<=_n;++i) const int inf=1e9+5, nax=400123, pot = (1 << 20); // jakie ma być pot? iurghwouwihfhg3rouhwfjpoh3vwrowuhfdushwgoruhfsjvdoisjr int n, ile_krokow, ile_par; int waga[nax]; int nr_v[nax], gr[nax]; vi w[nax]; int ojc[nax], prawy[nax], lewy[nax], h[nax]; int ile_wierz; int tr[2 * pot + 5]; int licznik; int odw[nax]; vector<pii > event[nax]; ll RES; void dfs(int x) { tr[pot + licznik] = x; odw[x] = licznik; licznik++; if(lewy[x] == 0) return; h[lewy[x]] = h[prawy[x]] = h[x] + 1; dfs(lewy[x]); tr[pot + licznik] = x; licznik++; dfs(prawy[x]); tr[pot + licznik] = x; licznik++; } int LCA(int a, int b) { a = odw[a] + pot; b = odw[b] + pot; if(a > b) swap(a, b); int res = tr[a]; if(h[tr[b]] < h[res]) res = tr[b]; while(a < b - 1) { if(a % 2 == 0 && h[tr[a+1]] < h[res]) res = tr[a+1]; if(b % 2 && h[tr[b-1]] < h[res]) res = tr[b-1]; a /= 2; b /= 2; } return res; } void res_dfs(int x) { if(lewy[x]) { res_dfs(lewy[x]); res_dfs(prawy[x]); } RE(i, event[x].size()) { int a = event[x][i].st, b = event[x][i].nd; int m = min(waga[a], waga[b]); RES += 2 * m; waga[a] -= m; waga[b] -= m; } } int main() { ios_base::sync_with_stdio(0); cin >> n >> ile_krokow >> ile_par; RI(i, n) cin >> waga[i]; RI(i, n) { gr[i] = i; w[i].pb(i); } ile_wierz = n; RE(_, ile_krokow) { int a, b; cin >> a >> b; a = gr[a]; b = gr[b]; if(w[a].size() > w[b].size()) swap(a, b); // 'a' wrzucamy do 'b' RE(i, w[a].size()) { w[b].pb( w[a][i] ); gr[ w[a][i] ] = b; } int v_a = nr_v[a], v_b = nr_v[b]; ile_wierz++; lewy[ile_wierz] = v_a; prawy[ile_wierz] = v_b; ojc[v_a] = ojc[v_b] = ile_wierz; nr_v[b] = ile_wierz; } RI(i, ile_wierz) if(ojc[i] == 0) dfs(i); h[0] = inf; for(int i = pot - 1; i > 0; --i) { if(h[tr[2 * i]] < h[tr[2 * i + 1]]) tr[i] = tr[2 * i]; else tr[i] = tr[2 * i + 1]; } RE(i, ile_par) { int a, b; cin >> a >> b; if(gr[a] == gr[b]) { int x = LCA(a, b); event[x].pb(mp(a,b)); } } RI(i, ile_wierz) if(ojc[i] == 0) res_dfs(i); cout << RES << "\n"; return 0; } |