#include<iostream> #include<vector> #include<utility> #include<cassert> using namespace std; typedef pair<int,int> PII; typedef vector<int> VI; typedef vector<PII> VII; typedef long long LL; const int MAXN = (1<<18)+1; const int MAXK = (1<<19)+1; const int WHITE = 1; const int GRAY = 2; const int BLACK = 3; struct Node; struct Query { Node* a; Node* b; Node* result; Query(Node* aa=nullptr, Node* bb=nullptr) : a(aa), b(bb), result(nullptr) {} Node* other(Node* n) { if (n == a) return b; return a; } } _queries[MAXK]; int queryCount = 0; struct Node { Node* left; Node* right; Node* parent; VII reactions; int color; vector<Query*> queries; int idx; Node(int i=-1, Node* l=nullptr, Node* r=nullptr) : left(l), right(r), parent(nullptr), reactions(), color(WHITE), queries(), idx(i) {} Node* getRepr() { if (parent != this) { parent = parent->getRepr(); } return parent; } void lcaVisit() { color = GRAY; parent = this; if (left != nullptr) { left->lcaVisit(); left->parent = this; } if (right != nullptr) { right->lcaVisit(); right->parent = this; } for (Query* q : queries) { Node* other = q->other(this); if (other->color != WHITE) { q->result = other->getRepr(); // q->result->reactions.push_back(PII(q->a->idx, q->b->idx)); } } color = BLACK; } void whiteVisit() { color = WHITE; if (left != nullptr) { left->whiteVisit(); } if (right != nullptr) { right->whiteVisit(); } } LL sedimentVisit(int* g) { LL result = 0; if (left != nullptr) { result += left->sedimentVisit(g); } if (right != nullptr) { result += right->sedimentVisit(g); } for (PII r : reactions) { int s = min(g[r.first], g[r.second]); // printf("reaction: %d %d: %d\n", r.first, r.second, s); g[r.first] -= s; g[r.second] -= s; result += 2 * s; } return result; } }; void newQuery(Node* a, Node* b) { _queries[queryCount] = Query(a, b); a->queries.push_back(&_queries[queryCount]); b->queries.push_back(&_queries[queryCount]); queryCount++; } Node _nodes[2*MAXN]; int nodeCount = 0; Node* newNode(Node* l, Node* r) { _nodes[nodeCount] = Node(nodeCount, l, r); return &_nodes[nodeCount++]; } Node* nodes[MAXN]; void join(int a, int b) { nodes[b] = newNode(nodes[a], nodes[b]); nodes[a] = nullptr; } void addQuery(int c, int d) { newQuery(&_nodes[c], &_nodes[d]); } int g[MAXN]; int main() { int n, m, k; assert(3 == scanf("%d%d%d", &n, &m, &k)); nodeCount = n; for (int i = 0; i < n; ++i) { nodes[i] = &_nodes[i]; nodes[i]->idx = i; assert(1 == scanf("%d", &g[i])); } for (int i = 0; i < m; ++i) { int a, b; assert(2 == scanf("%d%d", &a, &b)); a--, b--; join(a, b); } for (int i = 0; i < k; ++i) { int c, d; assert(2 == scanf("%d%d", &c, &d)); c--, d--; addQuery(c, d); } LL result = 0; for (int i = 0; i < n; ++i) { if (nodes[i] != nullptr) { nodes[i]->lcaVisit(); nodes[i]->whiteVisit(); } } for (int i = 0; i < k; ++i) { Query* q = &_queries[i]; // printf("lca %d %d: %d\n", q->a->idx, q->b->idx, q->result->idx); if (q->result != nullptr) { q->result->reactions.push_back(PII(q->a->idx, q->b->idx)); } } for (int i = 0; i < n; ++i) { if (nodes[i] != nullptr) { result += nodes[i]->sedimentVisit(g); } } printf("%lld\n", result); 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 | #include<iostream> #include<vector> #include<utility> #include<cassert> using namespace std; typedef pair<int,int> PII; typedef vector<int> VI; typedef vector<PII> VII; typedef long long LL; const int MAXN = (1<<18)+1; const int MAXK = (1<<19)+1; const int WHITE = 1; const int GRAY = 2; const int BLACK = 3; struct Node; struct Query { Node* a; Node* b; Node* result; Query(Node* aa=nullptr, Node* bb=nullptr) : a(aa), b(bb), result(nullptr) {} Node* other(Node* n) { if (n == a) return b; return a; } } _queries[MAXK]; int queryCount = 0; struct Node { Node* left; Node* right; Node* parent; VII reactions; int color; vector<Query*> queries; int idx; Node(int i=-1, Node* l=nullptr, Node* r=nullptr) : left(l), right(r), parent(nullptr), reactions(), color(WHITE), queries(), idx(i) {} Node* getRepr() { if (parent != this) { parent = parent->getRepr(); } return parent; } void lcaVisit() { color = GRAY; parent = this; if (left != nullptr) { left->lcaVisit(); left->parent = this; } if (right != nullptr) { right->lcaVisit(); right->parent = this; } for (Query* q : queries) { Node* other = q->other(this); if (other->color != WHITE) { q->result = other->getRepr(); // q->result->reactions.push_back(PII(q->a->idx, q->b->idx)); } } color = BLACK; } void whiteVisit() { color = WHITE; if (left != nullptr) { left->whiteVisit(); } if (right != nullptr) { right->whiteVisit(); } } LL sedimentVisit(int* g) { LL result = 0; if (left != nullptr) { result += left->sedimentVisit(g); } if (right != nullptr) { result += right->sedimentVisit(g); } for (PII r : reactions) { int s = min(g[r.first], g[r.second]); // printf("reaction: %d %d: %d\n", r.first, r.second, s); g[r.first] -= s; g[r.second] -= s; result += 2 * s; } return result; } }; void newQuery(Node* a, Node* b) { _queries[queryCount] = Query(a, b); a->queries.push_back(&_queries[queryCount]); b->queries.push_back(&_queries[queryCount]); queryCount++; } Node _nodes[2*MAXN]; int nodeCount = 0; Node* newNode(Node* l, Node* r) { _nodes[nodeCount] = Node(nodeCount, l, r); return &_nodes[nodeCount++]; } Node* nodes[MAXN]; void join(int a, int b) { nodes[b] = newNode(nodes[a], nodes[b]); nodes[a] = nullptr; } void addQuery(int c, int d) { newQuery(&_nodes[c], &_nodes[d]); } int g[MAXN]; int main() { int n, m, k; assert(3 == scanf("%d%d%d", &n, &m, &k)); nodeCount = n; for (int i = 0; i < n; ++i) { nodes[i] = &_nodes[i]; nodes[i]->idx = i; assert(1 == scanf("%d", &g[i])); } for (int i = 0; i < m; ++i) { int a, b; assert(2 == scanf("%d%d", &a, &b)); a--, b--; join(a, b); } for (int i = 0; i < k; ++i) { int c, d; assert(2 == scanf("%d%d", &c, &d)); c--, d--; addQuery(c, d); } LL result = 0; for (int i = 0; i < n; ++i) { if (nodes[i] != nullptr) { nodes[i]->lcaVisit(); nodes[i]->whiteVisit(); } } for (int i = 0; i < k; ++i) { Query* q = &_queries[i]; // printf("lca %d %d: %d\n", q->a->idx, q->b->idx, q->result->idx); if (q->result != nullptr) { q->result->reactions.push_back(PII(q->a->idx, q->b->idx)); } } for (int i = 0; i < n; ++i) { if (nodes[i] != nullptr) { result += nodes[i]->sedimentVisit(g); } } printf("%lld\n", result); return 0; } |