Advertisement
Guest User

Untitled

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