#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
long long res = 0;
long long MassOf[200 * 1000];
vector<int> reaction[500 * 1000];
bool Find(int a, int b)
{
int l = 0;
int r = reaction[a].size()-1;
while(l<=r)
{
int c = (l+r)/2;
if(b>reaction[a][c]) r = c-1;
else if(b<reaction[a][c]) l = c+1;
else return true;
}
return false;
}
class Entry
{
public:
vector<int> inside;
Entry(){}
Entry(int ID){inside.push_back(ID);}
void operator +=(Entry b)
{
for(int i = 0; i<b.inside.size(); i++)
{
if(MassOf[b.inside[i]]>0)
{
for(int j = 0; j<inside.size(); j++)
{
if(Find(b.inside[i], inside[j]))
{
if(MassOf[b.inside[i]] <= MassOf[inside[j]])
{
res += 2*MassOf[b.inside[i]];
MassOf[inside[j]] -= MassOf[b.inside[i]];
MassOf[b.inside[i]] = 0;
}
else
{
res += 2*MassOf[inside[j]];
MassOf[b.inside[i]] -= MassOf[inside[j]];
MassOf[inside[j]] = 0;
}
}
}
}
}
for(int i = 0; i<inside.size(); i++)
{
if(MassOf[inside[i]] == 0)
{
swap(inside[i], inside[inside.size()-1]);
i--;
inside.pop_back();
}
}
for(int i = 0; i<b.inside.size(); i++)
{
if(MassOf[b.inside[i]] > 0)
{
inside.push_back(b.inside[i]);
}
}
}
};
Entry Fiol[200 * 1000];
int From[200 * 1000];
int To[200 * 1000];
int main()
{
int N, M, K;
scanf("%d %d %d", &N, &M, &K);
for(int i = 0; i<N; i++)
{
scanf("%lld", &MassOf[i]);
}
for(int i = 0; i<M; i++)
{
scanf("%d %d", &From[i], &To[i]);
}
for(int i = 0; i<K; i++)
{
int a, b;
scanf("%d %d", &a, &b);
a--; b--;
reaction[a].push_back(b);
reaction[b].push_back(a);
}
for(int i = 0; i<N; i++)
{
if(reaction[i].size()>0)
{
Fiol[i] = Entry(i);
sort(reaction[i].begin(), reaction[i].end());
}
}
for(int i = 0; i<M; i++)
{
Fiol[To[i]-1] += Fiol[From[i]-1];
}
printf("%lld\n", res);
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 | #include <cstdio> #include <vector> #include <algorithm> using namespace std; long long res = 0; long long MassOf[200 * 1000]; vector<int> reaction[500 * 1000]; bool Find(int a, int b) { int l = 0; int r = reaction[a].size()-1; while(l<=r) { int c = (l+r)/2; if(b>reaction[a][c]) r = c-1; else if(b<reaction[a][c]) l = c+1; else return true; } return false; } class Entry { public: vector<int> inside; Entry(){} Entry(int ID){inside.push_back(ID);} void operator +=(Entry b) { for(int i = 0; i<b.inside.size(); i++) { if(MassOf[b.inside[i]]>0) { for(int j = 0; j<inside.size(); j++) { if(Find(b.inside[i], inside[j])) { if(MassOf[b.inside[i]] <= MassOf[inside[j]]) { res += 2*MassOf[b.inside[i]]; MassOf[inside[j]] -= MassOf[b.inside[i]]; MassOf[b.inside[i]] = 0; } else { res += 2*MassOf[inside[j]]; MassOf[b.inside[i]] -= MassOf[inside[j]]; MassOf[inside[j]] = 0; } } } } } for(int i = 0; i<inside.size(); i++) { if(MassOf[inside[i]] == 0) { swap(inside[i], inside[inside.size()-1]); i--; inside.pop_back(); } } for(int i = 0; i<b.inside.size(); i++) { if(MassOf[b.inside[i]] > 0) { inside.push_back(b.inside[i]); } } } }; Entry Fiol[200 * 1000]; int From[200 * 1000]; int To[200 * 1000]; int main() { int N, M, K; scanf("%d %d %d", &N, &M, &K); for(int i = 0; i<N; i++) { scanf("%lld", &MassOf[i]); } for(int i = 0; i<M; i++) { scanf("%d %d", &From[i], &To[i]); } for(int i = 0; i<K; i++) { int a, b; scanf("%d %d", &a, &b); a--; b--; reaction[a].push_back(b); reaction[b].push_back(a); } for(int i = 0; i<N; i++) { if(reaction[i].size()>0) { Fiol[i] = Entry(i); sort(reaction[i].begin(), reaction[i].end()); } } for(int i = 0; i<M; i++) { Fiol[To[i]-1] += Fiol[From[i]-1]; } printf("%lld\n", res); return 0; } |
English