Ankit_132

D

Sep 16th, 2023
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define ll     long long
  6. #define ff     first
  7. #define ss     second
  8. #define pb     push_back
  9.  
  10. int main()
  11. {
  12.     int n, m;
  13.     cin>>n>>m;
  14.  
  15.     vector<vector<array<ll int, 3>>> va(n);
  16.  
  17.     for(int i=0; i<m; i++)
  18.     {
  19.         int a, b, x, y;
  20.         cin>>a>>b>>x>>y;
  21.  
  22.         a--, b--;
  23.  
  24.         va[a].pb({b, x, y});
  25.         va[b].pb({a, -x, -y});
  26.     }
  27.  
  28.     vector<pair<ll int, ll int>> ans(n, {-1e18, -1e18});
  29.     ans[0] = {0, 0};
  30.  
  31.     vector<int> vis(n);
  32.  
  33.     function<void(int)> DFS = [&](int u)
  34.     {
  35.         vis[u] = 1;
  36.  
  37.         for(auto [v, a, b]: va[u])
  38.         {
  39.             if(vis[v])      continue;
  40.  
  41.             ans[v] = {a+ans[u].ff, b+ans[u].ss};
  42.  
  43.             DFS(v);
  44.         }
  45.     };
  46.  
  47.     DFS(0);
  48.  
  49.     for(auto [x, y]: ans)
  50.     {
  51.         if(x == -1e18)
  52.             cout<<"undecidable\n";
  53.         else
  54.             cout<<x<<" "<<y<<"\n";
  55.     }
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment