Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <bitset>
- #include <queue>
- using namespace std;
- #define FOR(i, n) for(int i = 0; i < (int)n; i++)
- #define MAX 30
- int n, m, k;
- bitset<20> type[MAX][MAX], ans;
- vector< vector<int> > g, matrix;
- vector< bitset<20> > bs;
- inline bool check (int v, int u, bitset<20> &a) {
- return (a & type[v][u]) != 0;
- }
- bool bfs (bitset<20> &a, int s, int t) {
- queue<int> q; q.push(s);
- vector<char> r(n, false);
- r[s] = true;
- while(!q.empty()) {
- int v = q.front(); q.pop();
- for (auto to : g[v])
- if(!r[to]) {
- if( (r[to] = check(v, to, a)) == true)
- q.push(to);
- }
- }
- return r[t];
- }
- // 0 n value
- int lower_bound (int first, int last, int val) {
- int it, step, count = last - first;
- while (count > 0) {
- it = first; step = count/2; it+=step;
- if(bs[it].count() < val) {
- first = ++it;
- count -= step + 1;
- }
- else count = step;
- }
- return first;
- }
- // 1.9 seconds
- int main() {
- ios::sync_with_stdio(false); cin.tie(NULL);
- cin >> k >> n >> m;
- g.resize(n); matrix.assign(n, vector<int>(n, 0));
- FOR (i, m) {
- int u, v, t; cin >> u >> v >> t;
- type[u][v][t] = type[v][u][t] = 1;
- if(!matrix[v][u]) {
- g[u].push_back(v), g[v].push_back(u);
- matrix[u][v] = matrix[v][u] = 1;
- }
- }
- int p = pow(2, k);
- for (int i = 1; i < p; i++)
- bs.push_back(bitset<20>(i));
- sort(bs.begin(), bs.end(), [] (const bitset<20> &a, const bitset<20> &b) {
- return a.count() < b.count();
- });
- ans = bitset<20>(p-1);
- int mid, l = 1, r = k;
- while (l < r) {
- mid = (r+l)/2;
- int first = lower_bound(0, bs.size(), mid), i;
- bool f = false;
- while(bs[first].count() == mid) {
- if(bfs(bs[first], 0, 1)) {
- f = true;
- i = first;
- }
- first++;
- }
- if(f) {
- if(ans.count() > mid) ans = bs[i];
- r = mid;
- }
- else l = mid + 1;
- }
- cout << ans.count() << "\n";
- FOR(i, k) {
- if(ans[i]) cout << i << ' ';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment