- এই সমস্যা তে আমাদের ফ্রেন্ডদের মাঝে যতগুলি আলাদা আলাদা গ্রুপ আছে সেই গ্রুপ গুলোর সবগুলির যোগফল যদি আলাদা আলাদা ভাবে শূন্য হয় তবে POSSIBLE প্রিন্ট করেত হবে । না হলে IMPOSSIBLE প্রিন্ট করত হবে ।
- যেমন ধরে নিলাম আমাদের ৪ টা ফ্রেন্ডেদের গ্রুপ আছে এবং প্রত্যেক গ্রুপে ২/৩ জন করে ফ্রেন্ড আছে । এখন যদি এই ৪ টা গ্রুপের Individually খরচ এর যোগফল যদি শূন্য হয় তবেই কেবল POSSIBLE হবে ।
- যদি m = 0 হয় এবং সবার খরচ যদি ০ হয় তবে কিন্তু POSSIBLE প্রিন্ট করতে হবে :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Problem : 11690 - Money Matters | |
* Verdict : Accepted. | |
* Time : 0.070 ms. | |
* Writer : Mehadi Hasan Menon. | |
* Date : 29.12.16. | |
**/ | |
#include <iostream> | |
#include <cstdio> | |
using namespace std; | |
const int mx = 10005; | |
int parent[mx]; | |
int cost[mx]; | |
int total_cost; | |
int find_rep(int r) | |
{ | |
if(parent[r] == r) { | |
return r; | |
} | |
parent[r] = find_rep(parent[r]); | |
return parent[r]; | |
} | |
void union_ab(int a, int b) | |
{ | |
int u = find_rep(a); int v = find_rep(b); | |
if(u != v) | |
{ | |
parent[v] = u; | |
cost[u] += cost[v]; | |
total_cost = cost[u]; | |
} | |
} | |
int main() | |
{ | |
freopen("input.txt", "r+", stdin); | |
int tc, n, m, x, y; | |
int c; | |
scanf("%d", &tc); | |
for(int i = 0; i < tc; i++) | |
{ | |
scanf("%d %d", &n, &m); | |
int cnt = 0; | |
// initialize set. | |
for(int j = 0; j < n; j++) { | |
scanf("%d", &c); | |
if(c == 0) { | |
cnt += 1; | |
} | |
parent[j] = j; | |
cost[j] = c; | |
} | |
if(cnt == n && m == 0) { | |
printf("POSSIBLE\n"); | |
continue; | |
} | |
total_cost = 0; | |
for(int j = 0; j < m; j++) { | |
scanf("%d %d", &x, &y); | |
union_ab(x, y); | |
} | |
if(total_cost == 0) { | |
printf("POSSIBLE\n"); | |
} | |
else { | |
printf("IMPOSSIBLE\n"); | |
} | |
} | |
return 0; | |
} |
হ্যাপি কোডিং :)
Comments
Post a Comment