Guest User

Untitled

a guest
Jul 19th, 2013
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <bitset>
  6. #include <queue>
  7. using namespace std;
  8. #define FOR(i, n) for(int i = 0; i < (int)n; i++)
  9. #define MAX 30
  10.  
  11. int n, m, k;
  12. bitset<20> type[MAX][MAX], ans;
  13. vector< vector<int> > g;
  14.  
  15. vector< bitset<20> > bs;
  16.  
  17. inline bool check (int v, int u, bitset<20> &a) {
  18.     return (a & type[v][u]) != 0;
  19. }
  20.  
  21. bool bfs (bitset<20> &a, int s, int t) {
  22.     queue<int> q; q.push(s);
  23.     vector<char> r(n, false);
  24.     r[s] = true;
  25.  
  26.     while(!q.empty()) {
  27.         int v = q.front(); q.pop();
  28.  
  29.         for (auto to : g[v])
  30.             if(!r[to]) {
  31.                 if( (r[to] = check(v, to, a)) == true)
  32.                     q.push(to);
  33.             }
  34.     }
  35.  
  36.     return r[t];
  37. }
  38.  
  39. int main() {
  40. #ifndef ONLINE_JUDGE
  41.     fstream cin("test.txt");
  42. #endif
  43.     ios::sync_with_stdio(false); cin.tie(NULL);
  44.  
  45.     cin >> k >> n >> m;
  46.     g.resize(n);
  47.     FOR (i, m) {
  48.         int u, v, t; cin >> u >> v >> t;
  49.  
  50.         type[u][v][t] = type[v][u][t] = 1;
  51.  
  52.         g[u].push_back(v), g[v].push_back(u);
  53.     }
  54.  
  55.     int p = pow(2, k);
  56.     for (int i = 1; i < p; i++)
  57.         bs.push_back(bitset<20>(i));
  58.    
  59.  
  60.     sort(bs.begin(), bs.end(), [] (const bitset<20> &a, const bitset<20> &b) {
  61.         return a.count() < b.count();
  62.     });
  63.  
  64.     for(int i = 0; i < bs.size(); i++)
  65.         if(bfs (bs[i], 0, 1)) {
  66.             ans = bs[i];
  67.             break;
  68.         }
  69.  
  70.     cout << ans.count() << "\n";
  71.     FOR(i, k) {
  72.         if(ans[i]) cout << i << ' ';
  73.     }
  74.  
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment