forked from IElgohary/Uva-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
11690 - Money Matters.cpp
executable file
·87 lines (72 loc) · 1.44 KB
/
11690 - Money Matters.cpp
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
#include <iostream>
using namespace std;
int ppl[10000];
int rnk[10000] = {0};
int mny[10000];
int findset(int a )
{
if ( ppl[a] == a)
return a;
return ppl[a] = findset(ppl[a]);
}
void unionF(int a , int b)
{
int x = findset(a);
int y = findset(b);
if ( x != y)
{
if ( rnk[x] > rnk[y])
{
ppl[y] = x;
mny[x] += mny[y];
mny[y] = 0;
}
else if ( rnk[y] > rnk[x])
{
ppl[x] = y;
mny[y] += mny [x];
mny[x] = 0;
}
else
{
ppl[y] = x;
rnk[x] ++;
mny [x] += mny [y];
mny[y] = 0;
}
}
}
int main()
{
int t;
cin >> t;
while (t--)
{
int n, m;
cin >> n >> m;
for ( int i = 0 ; i < n ; i++)
ppl[i] = i;
for ( int i = 0 ; i < n ; i++)
cin >> mny[i];
while ( m--)
{
int a , b;
cin >> a >> b;
unionF(a , b);
}
bool done = true;
for ( int i = 0 ; i < n ; i++)
{
if( mny[i] != 0)
{
done = false;
break;
}
}
if(done)
cout << "POSSIBLE"<<endl;
else
cout << "IMPOSSIBLE"<<endl;
}
return 0;
}