Advertisement
Dang_Quan_10_Tin

Bài B 5.12.2022

Dec 4th, 2022
712
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.23 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. using ll = long long;
  5. using ld = long double;
  6. using ull = unsigned long long;
  7.  
  8. template <class T>
  9. void read(T &x)
  10. {
  11.     x = 0;
  12.     register int c;
  13.     while ((c = getchar()) && (c > '9' || c < '0'))
  14.         ;
  15.     for (; c >= '0' && c <= '9'; c = getchar())
  16.         x = x * 10 + c - '0';
  17. }
  18.  
  19. constexpr bool typetest = 0;
  20. constexpr int N = 1e2 + 5;
  21. constexpr int Inf = 1e9 + 7;
  22.  
  23. int n, m;
  24. vector<int> adj[N];
  25. int c[N], s[N], res[N];
  26.  
  27. void Read()
  28. {
  29.     cin >> n >> m;
  30.  
  31.     for (int i = 1; i <= n; ++i)
  32.         cin >> c[i] >> s[i];
  33.  
  34.     for (int i = 1; i <= m; ++i)
  35.     {
  36.         int u, v;
  37.         cin >> u >> v;
  38.  
  39.         adj[u].emplace_back(v);
  40.         adj[v].emplace_back(u);
  41.     }
  42. }
  43.  
  44. struct Matrix
  45. {
  46.     int m, n;
  47.     int a[N * 2][N];
  48.     Matrix(const int &m = 0, const int &n = 0) : m(m), n(n)
  49.     {
  50.         memset(a, 0, sizeof a);
  51.     }
  52.  
  53.     void Elimination(int ans[N]) // ans will store the root
  54.     {
  55.         for (int i = 1; i < n; ++i)
  56.             ans[i] = 0;
  57.  
  58.         for (int i = 1; i <= m; ++i)
  59.         {
  60.             int k = -1, deg = Inf;
  61.  
  62.             for (int j = i; j <= m; ++j)
  63.             {
  64.                 // Find deg of j-th row
  65.  
  66.                 for (int t = 1; t < n; ++t)
  67.                     if (a[j][t] != 0)
  68.                     {
  69.                         if (deg > t)
  70.                         {
  71.                             deg = t;
  72.                             k = j;
  73.                         }
  74.  
  75.                         break;
  76.                     }
  77.             }
  78.  
  79.             if (deg == Inf)
  80.                 break;
  81.  
  82.             // Swap row
  83.             for (int t = 1; t <= n; ++t)
  84.                 swap(a[i][t], a[k][t]);
  85.  
  86.             // Elimination
  87.             for (int j = 1; j <= m; ++j)
  88.                 if (j != i)
  89.                 {
  90.                     int div = a[j][deg] * a[i][deg] % 3; // a[j][deg] / a[i][deg];
  91.  
  92.                     for (int t = 1; t <= n; ++t)
  93.                         a[j][t] = ((a[j][t] - a[i][t] * div) % 3 + 3) % 3;
  94.                 }
  95.         }
  96.  
  97.         for (int i = 1; i <= m; ++i)
  98.         {
  99.             int deg = -1;
  100.  
  101.             for (int j = 1; j < n; ++j)
  102.                 if (a[i][j] != 0)
  103.                 {
  104.                     deg = j;
  105.                     break;
  106.                 }
  107.  
  108.             if (deg == -1)
  109.             {
  110.                 if (a[i][n] != 0)
  111.                 {
  112.                     ans[1] = -1;
  113.                     return;
  114.                 }
  115.                 continue;
  116.             }
  117.  
  118.             ans[deg] = a[i][n] * a[i][deg] % 3; // a[i][n] / a[i][deg]
  119.         }
  120.     }
  121. };
  122.  
  123. bool Check(int v)
  124. {
  125.     Matrix f(n, n + 1);
  126.  
  127.     // Make equations
  128.     for (int i = 1; i <= n; ++i)
  129.     {
  130.         for (auto j : adj[i])
  131.             (++f.a[i][j]) %= 3;
  132.  
  133.         f.a[i][i] = ((-(int)adj[i].size()) % 3 + 3) % 3;
  134.         f.a[i][n + 1] = ((-c[i]) % 3 + 3) % 3;
  135.  
  136.         if (s[i] > v)
  137.         {
  138.             ++f.m;
  139.             f.a[f.m][i] = 1;
  140.             f.a[f.m][n + 1] = 0;
  141.         }
  142.     }
  143.  
  144.     for (int i = 1; i <= n; ++i)
  145.         for (int j = 1; j <= n + 1; ++j)
  146.             f.a[i][j] = ((f.a[i][j] - f.a[n][j]) % 3 + 3) % 3;
  147.  
  148.     // Guass Elimination
  149.     f.Elimination(res);
  150.  
  151.     if (res[1] == -1)
  152.         return false;
  153.  
  154.     return true;
  155. }
  156.  
  157. void Solve()
  158. {
  159.  
  160.     int l = -Inf, mid, h = Inf;
  161.  
  162.     while (l <= h)
  163.     {
  164.         mid = (l + h) / 2;
  165.  
  166.         if (!Check(mid))
  167.             l = mid + 1;
  168.         else
  169.             h = mid - 1;
  170.     }
  171.  
  172.     if (l >= Inf)
  173.     {
  174.         cout << -1;
  175.         return;
  176.     }
  177.  
  178.     cout << l << "\n";
  179.  
  180.     Check(l);
  181.  
  182.     for (int i = 1; i <= n; ++i)
  183.         cout << res[i] << " ";
  184. }
  185.  
  186. int32_t main()
  187. {
  188.     ios::sync_with_stdio(0);
  189.     cin.tie(0);
  190.     cout.tie(0);
  191.     if (fopen("tests.inp", "r"))
  192.     {
  193.         freopen("test.inp", "r", stdin);
  194.         freopen("test.out", "w", stdout);
  195.     }
  196.  
  197.     int t(1);
  198.     if (typetest)
  199.         cin >> t;
  200.     for (int _ = 1; _ <= t; ++_)
  201.     {
  202.         // cout << "Case #" << _ << endl;
  203.         Read();
  204.         Solve();
  205.     }
  206.     // cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  207. }
  208.  
  209. /*
  210. Input:
  211. 4 5 4
  212. 2 3
  213. 2 5
  214. 3 1
  215. 4 4
  216.  
  217. Output:
  218. 9
  219. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement