#include <map> #include <cstdio> #include <vector> #include <algorithm> using namespace std; int n, m, k; vector<int> G; vector<pair<int,int> > S; vector<pair<int,int> > R; struct Node{ int size; int parent; int preorder; int supervisor; vector<int> childs; }; vector<Node> Tree; void init(){ Tree.resize(n); for(int i=0; i<n; ++i){ Tree[i].size = 1; Tree[i].parent = -1; Tree[i].supervisor = i; } } void merge(int a, int b){ Node node; int node_id = Tree.size(); int sup_a = Tree[a].supervisor; int sup_b = Tree[b].supervisor; // Ustawianie wierzcholka node.parent = node.supervisor = -1; node.size = Tree[sup_a].size + Tree[sup_b].size + 1; node.childs.push_back(sup_a); Tree[sup_a].parent = node_id; node.childs.push_back(sup_b); Tree[sup_b].parent = node_id; Tree[b].supervisor = node_id; Tree.push_back(node); } void complete_tree(){ if (m==n-1) return; Node node; node.parent = node.supervisor = -1; node.size = 1; for(int i=0; i<Tree.size(); ++i) if (Tree[i].parent==-1){ node.childs.push_back(i); Tree[i].parent = Tree.size(); node.size += Tree[i].size; } Tree.push_back(node); } int cnt; void dfs(int v){ Tree[v].preorder = cnt; ++cnt; for(int i=0; i<Tree[v].childs.size(); ++i) dfs(Tree[v].childs[i]); } void calculate_preorder(){cnt = 0; dfs(Tree.size()-1);} bool predecessor(int u, int v){ if (Tree[u].preorder < Tree[v].preorder) return false; return Tree[u].preorder < Tree[v].preorder + Tree[v].size; } vector<vector<int> > p; void calculate_p(){ p.resize(1, vector<int>(Tree.size())); for(int i=0; i<Tree.size(); ++i) if (Tree[i].parent == -1) p[0][i]=i; else p[0][i]=Tree[i].parent; bool cont; do{ cont = false; vector<int> row(Tree.size()); for(int i=0; i<Tree.size(); ++i){ row[i] = p.back()[p.back()[i]]; if (row[i] != Tree.size()-1) cont = true; } p.push_back(row); } while(cont); } int LCA(int u, int v){ if (predecessor(u, v)) return v; if (predecessor(v, u)) return u; int i = u; int j = p.size()-1; while(j>=0){ if (predecessor(v, p[j][i])) --j; else i = p[j][i]; } return p[0][i]; } int main(){ // Wczytywanie danych scanf("%d %d %d", &n, &m, &k); G.resize(n); S.resize(m); R.resize(k); for(int i=0; i<n; ++i) scanf("%d", &G[i]); for(int i=0; i<m; ++i) scanf("%d %d", &S[i].first, &S[i].second); for(int i=0; i<k; ++i){ scanf("%d %d", &R[i].first, &R[i].second); --R[i].first, --R[i].second; } // Tworzenie drzewa i wszystkiego innego init(); for(int i=0; i<m; ++i) merge(S[i].first-1, S[i].second-1); complete_tree(); calculate_preorder(); calculate_p(); // Liczenie wyniku long long result = 0; map<int, vector<pair<int,int> > > M; for(int i=0; i<k; ++i) M[LCA(R[i].first, R[i].second)].push_back(R[i]); if (m<n-1) M[Tree.size()-1].clear(); for(map<int, vector<pair<int,int> > >::iterator it=M.begin(); it!=M.end(); ++it){ vector<pair<int,int> > &V = it->second; for(int i=0; i<V.size(); ++i){ int a = V[i].first; int b = V[i].second; int x = min(G[a], G[b]); result += x; result += x; G[a] -= x; G[b] -= x; } } // Wypisywanie wyniku printf("%lld\n", result); }
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 | #include <map> #include <cstdio> #include <vector> #include <algorithm> using namespace std; int n, m, k; vector<int> G; vector<pair<int,int> > S; vector<pair<int,int> > R; struct Node{ int size; int parent; int preorder; int supervisor; vector<int> childs; }; vector<Node> Tree; void init(){ Tree.resize(n); for(int i=0; i<n; ++i){ Tree[i].size = 1; Tree[i].parent = -1; Tree[i].supervisor = i; } } void merge(int a, int b){ Node node; int node_id = Tree.size(); int sup_a = Tree[a].supervisor; int sup_b = Tree[b].supervisor; // Ustawianie wierzcholka node.parent = node.supervisor = -1; node.size = Tree[sup_a].size + Tree[sup_b].size + 1; node.childs.push_back(sup_a); Tree[sup_a].parent = node_id; node.childs.push_back(sup_b); Tree[sup_b].parent = node_id; Tree[b].supervisor = node_id; Tree.push_back(node); } void complete_tree(){ if (m==n-1) return; Node node; node.parent = node.supervisor = -1; node.size = 1; for(int i=0; i<Tree.size(); ++i) if (Tree[i].parent==-1){ node.childs.push_back(i); Tree[i].parent = Tree.size(); node.size += Tree[i].size; } Tree.push_back(node); } int cnt; void dfs(int v){ Tree[v].preorder = cnt; ++cnt; for(int i=0; i<Tree[v].childs.size(); ++i) dfs(Tree[v].childs[i]); } void calculate_preorder(){cnt = 0; dfs(Tree.size()-1);} bool predecessor(int u, int v){ if (Tree[u].preorder < Tree[v].preorder) return false; return Tree[u].preorder < Tree[v].preorder + Tree[v].size; } vector<vector<int> > p; void calculate_p(){ p.resize(1, vector<int>(Tree.size())); for(int i=0; i<Tree.size(); ++i) if (Tree[i].parent == -1) p[0][i]=i; else p[0][i]=Tree[i].parent; bool cont; do{ cont = false; vector<int> row(Tree.size()); for(int i=0; i<Tree.size(); ++i){ row[i] = p.back()[p.back()[i]]; if (row[i] != Tree.size()-1) cont = true; } p.push_back(row); } while(cont); } int LCA(int u, int v){ if (predecessor(u, v)) return v; if (predecessor(v, u)) return u; int i = u; int j = p.size()-1; while(j>=0){ if (predecessor(v, p[j][i])) --j; else i = p[j][i]; } return p[0][i]; } int main(){ // Wczytywanie danych scanf("%d %d %d", &n, &m, &k); G.resize(n); S.resize(m); R.resize(k); for(int i=0; i<n; ++i) scanf("%d", &G[i]); for(int i=0; i<m; ++i) scanf("%d %d", &S[i].first, &S[i].second); for(int i=0; i<k; ++i){ scanf("%d %d", &R[i].first, &R[i].second); --R[i].first, --R[i].second; } // Tworzenie drzewa i wszystkiego innego init(); for(int i=0; i<m; ++i) merge(S[i].first-1, S[i].second-1); complete_tree(); calculate_preorder(); calculate_p(); // Liczenie wyniku long long result = 0; map<int, vector<pair<int,int> > > M; for(int i=0; i<k; ++i) M[LCA(R[i].first, R[i].second)].push_back(R[i]); if (m<n-1) M[Tree.size()-1].clear(); for(map<int, vector<pair<int,int> > >::iterator it=M.begin(); it!=M.end(); ++it){ vector<pair<int,int> > &V = it->second; for(int i=0; i<V.size(); ++i){ int a = V[i].first; int b = V[i].second; int x = min(G[a], G[b]); result += x; result += x; G[a] -= x; G[b] -= x; } } // Wypisywanie wyniku printf("%lld\n", result); } |