Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cmath>
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <bitset>
  6. using namespace::std;
  7.  
  8. const int N = 25, M = 1024 * 1024 + 5;
  9. vector<int> t[N];
  10. bitset<32> q;
  11. int n, m, dp[M], wyn[M], pot[N], ile;
  12. bool o[M], o2[M];
  13.  
  14. void zrob(int v) {
  15.     int index = v;
  16.     q = v;
  17.     for (int i = 0; i < n; i++)
  18.         if (!o[index | pot[i]]) {
  19.             o[index | pot[i]] = true;
  20.             int biale = 0;
  21.             for (int w : t[i + 1])
  22.                 if (q[w - 1] == 0) biale++;
  23.             dp[index | pot[i]] = dp[index] + 2 * biale - t[i + 1].size();
  24.         }
  25. }
  26.  
  27. void wylicz(int v) {
  28.     int index = v;
  29.     q = v;
  30.     wyn[index] = dp[index];
  31.     for (int i = 0; i < n; i++) {
  32.         if (q[i] == 1) wyn[index] = max(wyn[index], wyn[index ^ pot[i]]);
  33.         if (!o2[index | pot[i]])
  34.             o2[index | pot[i]] = true;
  35.     }
  36. }
  37.  
  38. int liczba(string &s) {
  39.     int rez = 0, mnoz = 1;
  40.     for (int i = 0; i < s.size(); i++) {
  41.         rez += (s[i] - '0') * mnoz;
  42.         mnoz <<= 1;
  43.     }
  44.     return rez;
  45. }
  46.  
  47. int main() {
  48.     ios_base::sync_with_stdio(false);
  49.     cin.tie(0);
  50.     cout.tie(0);
  51.     cin >> n >> m >> ile;
  52.     for (int i = 0; i < m; i++) {
  53.         int a, b;
  54.         cin >> a >> b;
  55.         t[a].push_back(b);
  56.         t[b].push_back(a);
  57.     }
  58.     int s = 1;
  59.     for (int i = 0; i < n; i++) {
  60.         pot[i] = s;
  61.         s *= 2;
  62.     }
  63.     for (int i = 0; i < pow(2, n); i++) {
  64.         zrob(i);
  65.         wylicz(i);
  66.     }  
  67.     for (int i = 0; i < ile; i++) {
  68.         string s;
  69.         cin >> s;
  70.         cout << wyn[liczba(s)]<< endl;
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement