-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11690 - Money Matter.cpp
81 lines (75 loc) · 1.46 KB
/
11690 - Money Matter.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
#include <bits/stdc++.h>
using namespace std;
int parent[50005] , ran[50005] , find_group[50005];
typedef long long int ll;
ll sum[50005];
int arr[50005];
void built (int n)
{
for(int i = 0; i < n; i++)
{
parent[i] = i;
ran[i] = 0;
}
}
int make_friend (int n)
{
if(parent[n] == n) return n;
else return parent[n] = make_friend(parent[n]);
}
void make_union(int a , int b)
{
if(ran[a] > ran[b])
{
parent[b] = a;
arr[a] += arr[b];
}
else
{
parent[a] = b ;
arr[b] += arr[a];
if(ran[a] == ran[b])
ran[b]++ ;
}
}
int main()
{
int tes;
cin >> tes;
while(tes--)
{
int n , m;
cin >> n >> m;
memset(arr, 0, sizeof arr);
memset(find_group , 0 ,sizeof find_group);
built(n);
for(int i = 0; i < n; i++)
{
cin >> arr[i];
}
for(int i = 0; i < m; i++)
{
int a, b;
cin >> a >> b;
int pa , pb;
pa = make_friend(a);
pb = make_friend(b);
if(pa != pb)
make_union(pa, pb);
}
int f = true;
for(int i = 0; i < n; i++)
{
int x= make_friend(i);
if(arr[x] != 0)
{
f = false;
break;
}
}
if(f)
printf("POSSIBLE\n");
else
printf("IMPOSSIBLE\n");
}
}