Guest User

Untitled

a guest
Jul 19th, 2013
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <bitset>
  5. #include <queue>
  6. using namespace std;
  7. #define FOR(i, n) for(int i = 0; i < (int)n; i++)
  8. #define MAX 30
  9.  
  10. int n, m, k;
  11. bitset<20> type[MAX][MAX], ans;
  12. vector< vector<int> > g, matrix;
  13.  
  14. vector< bitset<20> > bs;
  15.  
  16. inline bool check (int v, int u, bitset<20> &a) {
  17.     return (a & type[v][u]) != 0;
  18. }
  19.  
  20. bool bfs (bitset<20> &a, int s, int t) {
  21.     queue<int> q; q.push(s);
  22.     vector<char> r(n, false);
  23.     r[s] = true;
  24.  
  25.     while(!q.empty()) {
  26.         int v = q.front(); q.pop();
  27.  
  28.         for (auto to : g[v])
  29.             if(!r[to]) {
  30.                 if( (r[to] = check(v, to, a)) == true)
  31.                     q.push(to);
  32.             }
  33.     }
  34.  
  35.     return r[t];
  36. }
  37. // 0 n value
  38. int lower_bound (int first, int last, int val) {
  39.     int it, step, count = last - first;
  40.     while (count > 0) {
  41.         it = first; step = count/2; it+=step;
  42.         if(bs[it].count() < val) {
  43.             first = ++it;
  44.             count -= step + 1;
  45.         }
  46.         else count = step;
  47.     }
  48.     return first;
  49. }
  50.  
  51. // 1.9 seconds
  52.  
  53. int main() {
  54.     ios::sync_with_stdio(false); cin.tie(NULL);
  55.  
  56.     cin >> k >> n >> m;
  57.     g.resize(n); matrix.assign(n, vector<int>(n, 0));
  58.     FOR (i, m) {
  59.         int u, v, t; cin >> u >> v >> t;
  60.  
  61.         type[u][v][t] = type[v][u][t] = 1;
  62.  
  63.         if(!matrix[v][u]) {
  64.             g[u].push_back(v), g[v].push_back(u);
  65.             matrix[u][v] = matrix[v][u] = 1;
  66.         }
  67.     }
  68.  
  69.     int p = pow(2, k);
  70.     for (int i = 1; i < p; i++)
  71.         bs.push_back(bitset<20>(i));
  72.  
  73.  
  74.     sort(bs.begin(), bs.end(), [] (const bitset<20> &a, const bitset<20> &b) {
  75.         return a.count() < b.count();
  76.     });
  77.  
  78.     ans = bitset<20>(p-1);
  79.     int mid, l = 1, r = k;
  80.     while (l < r) {
  81.         mid = (r+l)/2;
  82.         int first = lower_bound(0, bs.size(), mid), i;
  83.         bool f = false;
  84.  
  85.         while(bs[first].count() == mid) {
  86.             if(bfs(bs[first], 0, 1)) {
  87.                 f = true;
  88.                 i = first;
  89.             }
  90.             first++;
  91.         }
  92.  
  93.         if(f) {
  94.             if(ans.count() > mid) ans = bs[i];
  95.             r = mid;
  96.         }
  97.         else l = mid + 1;
  98.     }
  99.  
  100.     cout << ans.count() << "\n";
  101.     FOR(i, k) {
  102.         if(ans[i]) cout << i << ' ';
  103.     }
  104.  
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment