// Piotr Golda #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cstdio> #include <algorithm> #include <vector> #include <cstdlib> using namespace std; #define LLD long long int #define MAX_NUM 200000 #define MAX_REACTIONS_NUM 500000 class Node; class Reaction; int M,N,K; int Vol[MAX_NUM]; Node* Vial[MAX_NUM]; Reaction* Reactions; class Reaction { public: int m_a; int m_b; int m_time; Reaction(){} Reaction(int p_a, int p_b, int p_time) :m_a(p_a), m_b(p_b), m_time(p_time){} bool operator<(const Reaction& p_rhs) const { return m_time > p_rhs.m_time; } }; class Node { Node* m_father; Node* m_leftSon; Node* m_rightSon; int m_rootDistance; Node(Node* p_left, Node* p_right) :m_father(NULL), m_leftSon(p_left), m_rightSon(p_right), m_rootDistance(-1){} public: Node() :m_father(NULL), m_leftSon(NULL), m_rightSon(NULL), m_rootDistance(-1){} bool isRoot(){return m_father==NULL;} void computeRootDistances( int distance ) { m_rootDistance = distance; if(m_leftSon) m_leftSon->computeRootDistances( distance + 1 ); if(m_rightSon) m_rightSon->computeRootDistances( distance + 1 ); } static int getCombinationTime( Node* p_left, Node* p_right) { while( p_left->m_rootDistance < p_right->m_rootDistance ) p_right = p_right->m_father; while( p_right->m_rootDistance < p_left->m_rootDistance ) p_left = p_left->m_father; while( p_left != p_right ) { p_right = p_right->m_father; p_left = p_left->m_father; } if( p_left == NULL ) return -1; return p_left->m_rootDistance; } static Node* merge(Node* p_left, Node* p_right) { p_left->m_father = new Node(p_left, p_right); p_right->m_father = p_left->m_father; return p_right->m_father; } }; void scanAndInitialize() { scanf("%d %d %d", &N, &M, &K); Reactions = new Reaction[K]; int from, to; Node* VialCopy[MAX_NUM]; for(unsigned i = 0; i < N; ++i) { scanf("%d", &Vol[i]); Vial[i] = new Node(); VialCopy[i] = Vial[i]; } for(unsigned i = 0; i < M; ++i) { scanf("%d %d", &from, &to); from--; to--; Vial[ to ] = Node::merge( Vial[from], Vial[to]); } for(unsigned i = 0; i < N; ++i) { if( Vial[i]->isRoot() ) Vial[i]->computeRootDistances(0); Vial[i] = VialCopy[i]; } } void scanAnalizeReactions() { int a, b; for(unsigned i = 0; i < K; ++i) { scanf("%d %d", &a, &b); a--; b--; Reactions[i] = Reaction(a, b, Node::getCombinationTime(Vial[a], Vial[b]) ); } stable_sort( Reactions, Reactions + K ); } LLD computeSedimentVol() { LLD sedimentVol = 0; int tmp; for(unsigned i = 0; i < K; ++i) { if( Reactions[i].m_time != -1 ) { tmp = min( Vol[ Reactions[i].m_a ], Vol[ Reactions[i].m_b ] ); Vol[ Reactions[i].m_a ] -= tmp; Vol[ Reactions[i].m_b ] -= tmp; sedimentVol += (LLD)tmp*2L; } } return sedimentVol; } int main() { scanAndInitialize(); scanAnalizeReactions(); printf( "%lld\n", computeSedimentVol() ); 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 | // Piotr Golda #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cstdio> #include <algorithm> #include <vector> #include <cstdlib> using namespace std; #define LLD long long int #define MAX_NUM 200000 #define MAX_REACTIONS_NUM 500000 class Node; class Reaction; int M,N,K; int Vol[MAX_NUM]; Node* Vial[MAX_NUM]; Reaction* Reactions; class Reaction { public: int m_a; int m_b; int m_time; Reaction(){} Reaction(int p_a, int p_b, int p_time) :m_a(p_a), m_b(p_b), m_time(p_time){} bool operator<(const Reaction& p_rhs) const { return m_time > p_rhs.m_time; } }; class Node { Node* m_father; Node* m_leftSon; Node* m_rightSon; int m_rootDistance; Node(Node* p_left, Node* p_right) :m_father(NULL), m_leftSon(p_left), m_rightSon(p_right), m_rootDistance(-1){} public: Node() :m_father(NULL), m_leftSon(NULL), m_rightSon(NULL), m_rootDistance(-1){} bool isRoot(){return m_father==NULL;} void computeRootDistances( int distance ) { m_rootDistance = distance; if(m_leftSon) m_leftSon->computeRootDistances( distance + 1 ); if(m_rightSon) m_rightSon->computeRootDistances( distance + 1 ); } static int getCombinationTime( Node* p_left, Node* p_right) { while( p_left->m_rootDistance < p_right->m_rootDistance ) p_right = p_right->m_father; while( p_right->m_rootDistance < p_left->m_rootDistance ) p_left = p_left->m_father; while( p_left != p_right ) { p_right = p_right->m_father; p_left = p_left->m_father; } if( p_left == NULL ) return -1; return p_left->m_rootDistance; } static Node* merge(Node* p_left, Node* p_right) { p_left->m_father = new Node(p_left, p_right); p_right->m_father = p_left->m_father; return p_right->m_father; } }; void scanAndInitialize() { scanf("%d %d %d", &N, &M, &K); Reactions = new Reaction[K]; int from, to; Node* VialCopy[MAX_NUM]; for(unsigned i = 0; i < N; ++i) { scanf("%d", &Vol[i]); Vial[i] = new Node(); VialCopy[i] = Vial[i]; } for(unsigned i = 0; i < M; ++i) { scanf("%d %d", &from, &to); from--; to--; Vial[ to ] = Node::merge( Vial[from], Vial[to]); } for(unsigned i = 0; i < N; ++i) { if( Vial[i]->isRoot() ) Vial[i]->computeRootDistances(0); Vial[i] = VialCopy[i]; } } void scanAnalizeReactions() { int a, b; for(unsigned i = 0; i < K; ++i) { scanf("%d %d", &a, &b); a--; b--; Reactions[i] = Reaction(a, b, Node::getCombinationTime(Vial[a], Vial[b]) ); } stable_sort( Reactions, Reactions + K ); } LLD computeSedimentVol() { LLD sedimentVol = 0; int tmp; for(unsigned i = 0; i < K; ++i) { if( Reactions[i].m_time != -1 ) { tmp = min( Vol[ Reactions[i].m_a ], Vol[ Reactions[i].m_b ] ); Vol[ Reactions[i].m_a ] -= tmp; Vol[ Reactions[i].m_b ] -= tmp; sedimentVol += (LLD)tmp*2L; } } return sedimentVol; } int main() { scanAndInitialize(); scanAnalizeReactions(); printf( "%lld\n", computeSedimentVol() ); return 0; } |