Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     int t;
  10.     int cs = 1;
  11.     cin >> t;
  12.     while(t--)
  13.     {
  14.         int limit = 10010;
  15.         int n,m;
  16.         cin >> n >> m;
  17.         int mon[n+1];
  18.         int totMon = 0;
  19.         vector <int> v[limit];
  20.         for(int i = 1; i <= n; i++)
  21.         {
  22.             cin >> mon[i];
  23.             totMon += mon[i];
  24.         }
  25.         int avgMon = totMon/n;
  26.         while(m--)
  27.         {
  28.             int u,y;
  29.             cin >> u >> y;
  30.             v[u].push_back(y);
  31.             v[y].push_back(u);
  32.         }
  33.         queue <int> qPos, qNeg;
  34.         for(int i = 1; i <= n; i++)
  35.         {
  36.             mon[i] = mon[i]-avgMon;
  37.             if(mon[i] < 0)
  38.                 qNeg.push(i);
  39.             else if(mon[i] > 0)
  40.                 qPos.push(i);
  41.         }
  42.         while (!qPos.empty())
  43.         {
  44.             bool alvi = false;
  45.             int start = qPos.front();
  46.             qPos.pop();
  47.             int end = qNeg.front();
  48.             qNeg.pop();
  49.             queue <int> q;
  50.             q.push(start);
  51.             bool mark[limit] = {0};
  52.             while(!q.empty())
  53.             {
  54.                 int u  = q.front();
  55.                 q.pop();
  56.                 if(mark[u] == 0)
  57.                     mark[u] = 1;
  58.                 for(int i = 0; i < v[u].size(); i++)
  59.                 {
  60.                     if(mark[v[u][i]] == 0)
  61.                     {
  62.                         if(v[u][i] == end)
  63.                         {
  64.                             int temp1 = mon[start];
  65.                             int temp2 = mon[end];
  66.                             mon[start] = temp1+temp2;
  67.                             mon[end] = temp1+temp2;
  68.                             cout << mon[start] << " " << mon[end] << endl;
  69.                             if(mon[start] == 0 && mon[end] == 0)
  70.                             {
  71.                                 alvi = true;
  72.                                 break;
  73.                             }
  74.                             else if(mon[start] != 0)
  75.                                 qPos.push(start);
  76.                             else if(mon[end] != 0)
  77.                                 qNeg.push(end);
  78.                         }
  79.                         q.push(v[u][i]);
  80.                         mark[v[u][i]] == 1;
  81.                     }
  82.                 }
  83.                 if(alvi)
  84.                     break;
  85.             }
  86.         }
  87.         /*bool alvi = true;
  88.         for(int i = 0; i < n; i++)
  89.         {
  90.             if(mon[i] != 0)
  91.             {
  92.                 alvi = false;
  93.                 break;
  94.             }
  95.         }
  96.  
  97.         if(alvi == false)
  98.             cout << "No" << endl;
  99.         else if(alvi == true)
  100.             cout <<"Yes" << endl;
  101.         */
  102.         for(int i = 0; i < n; i++)
  103.             cout << mon[i] << " ";
  104.         cout << endl;
  105.         cs++;
  106.     }
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement