Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- using namespace std;
- int main()
- {
- int t;
- int cs = 1;
- cin >> t;
- while(t--)
- {
- int limit = 10010;
- int n,m;
- cin >> n >> m;
- int mon[n+1];
- int totMon = 0;
- vector <int> v[limit];
- for(int i = 1; i <= n; i++)
- {
- cin >> mon[i];
- totMon += mon[i];
- }
- int avgMon = totMon/n;
- while(m--)
- {
- int u,y;
- cin >> u >> y;
- v[u].push_back(y);
- v[y].push_back(u);
- }
- queue <int> qPos, qNeg;
- for(int i = 1; i <= n; i++)
- {
- mon[i] = mon[i]-avgMon;
- if(mon[i] < 0)
- qNeg.push(i);
- else if(mon[i] > 0)
- qPos.push(i);
- }
- while (!qPos.empty())
- {
- bool alvi = false;
- int start = qPos.front();
- qPos.pop();
- int end = qNeg.front();
- qNeg.pop();
- queue <int> q;
- q.push(start);
- bool mark[limit] = {0};
- while(!q.empty())
- {
- int u = q.front();
- q.pop();
- if(mark[u] == 0)
- mark[u] = 1;
- for(int i = 0; i < v[u].size(); i++)
- {
- if(mark[v[u][i]] == 0)
- {
- if(v[u][i] == end)
- {
- int temp1 = mon[start];
- int temp2 = mon[end];
- mon[start] = temp1+temp2;
- mon[end] = temp1+temp2;
- cout << mon[start] << " " << mon[end] << endl;
- if(mon[start] == 0 && mon[end] == 0)
- {
- alvi = true;
- break;
- }
- else if(mon[start] != 0)
- qPos.push(start);
- else if(mon[end] != 0)
- qNeg.push(end);
- }
- q.push(v[u][i]);
- mark[v[u][i]] == 1;
- }
- }
- if(alvi)
- break;
- }
- }
- /*bool alvi = true;
- for(int i = 0; i < n; i++)
- {
- if(mon[i] != 0)
- {
- alvi = false;
- break;
- }
- }
- if(alvi == false)
- cout << "No" << endl;
- else if(alvi == true)
- cout <<"Yes" << endl;
- */
- for(int i = 0; i < n; i++)
- cout << mon[i] << " ";
- cout << endl;
- cs++;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement