#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
struct node
{
int value;
int dis;
};
const int N = 1e6 + 10;
bool vis[N];
int bfs(int st, int ed, int dis)
{
memset(vis, 0, sizeof vis);
queue<node> q;
node One, next;
One.value = st;
One.dis = dis;
q.push(One);
vis[st] = true;
while (!q.empty())
{
node t = q.front();
q.pop();
if (t.value == ed)
return t.dis * 2;
next.value = t.value + 1;
if (next.value && next.value < 1000000 && !vis[next.value])
{
next.dis = t.dis + 1;
q.push(next);
vis[next.value] = true;
}
next.value = t.value - 1;
if (next.value && next.value < 1000000 && !vis[next.value])
{
next.dis = t.dis + 1;
q.push(next);
vis[next.value] = true;
}
next.value = t.value * 2;
if (next.value && next.value < 1000000 && !vis[next.value])
{
next.dis = t.dis + 1;
q.push(next);
vis[next.value] = true;
}
if (t.value % 2 == 0)
{
next.value = t.value / 2;
if (next.value && next.value < 1000000 && !vis[next.value]) {
next.dis = t.dis + 1;
q.push(next);
vis[next.value] = true;
}
}
}
}
int main() {
int T;
cin >> T;
while (T -- )
{
int s, t;
cin >> s >> t;
cout << bfs(s, t, 0) << endl;
}
return 0;
}
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include <string>
using namespace std;
string str;
bool st[20];
int k, res;
void dfs(int step){
if (step == k){
int ans = 0;
int pos = 0;
for (int i = 0; i < str.size(); i ++ ){
if (!st[i]){
ans += (str[i] - '0') * pow(10, str.size() - k - 1 - pos);
pos ++ ;
}
}
res = min(res, ans);
return;
}
for (int i = 0; i < str.size(); i ++ ){
if (!st[i]){
st[i] = true;
dfs(step + 1);
st[i] = false;
}
}
}
int main() {
int T;
cin >> T;
while (T -- ){
memset(st, false, sizeof st);
cin >> str >> k;
res = 1e9;
dfs(0);
cout << res << endl;
}
return 0;
}
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
using namespace std;
const int N = 5010, M = 1e5 + 10;
struct Node{
int p, size = 0;
int dic;
} p[N];
priority_queue<int, vector<int>, greater<int>> ans[N];
int n, m;
int h[N], e[M], ne[M], idx;
int belong[N], bent, stk[N], top, dfn[N], low[N], instk[N], timestamp;
bool cmp(Node A, Node B){
if (A.size != B.size) return A.size > B.size;
else return A.dic < B.dic;
}
void add(int a, int b){
e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}
void tarjan(int u){
dfn[u] = low[u] = ++ timestamp;
stk[++ top] = u, instk[u] = true;
for (int i = h[u]; ~i; i = ne[i]){
int j = e[i];
if (!dfn[j]){
tarjan(j);
low[u] = min(low[u], low[j]);
}else if (instk[j]) low[u] = min(low[u], dfn[j]);
}
if (dfn[u] == low[u]){
int v;
++ bent;
p[bent].dic = stk[top];
do{
v = stk[top --];
instk[v] = false;
belong[v] = bent;
p[bent].p = bent;
p[bent].size ++ ;
ans[bent].push(v);
}while(v != u);
}
}
int main() {
memset(h, -1, sizeof h);
cin >> n >> m;
while (m -- ){
int a, b, c;
cin >> a >> b >> c;
add(a, b);
if (c == 2) add(b, a);
}
for (int i = 1; i <= n; i ++ )
if (!dfn[i])
tarjan(i);
sort(p + 1, p + 1 + bent, cmp);
cout << p[1].size << endl;
while (!ans[p[1].p].empty()){
cout << ans[p[1].p].top() << ' ';
ans[p[1].p].pop();
}
return 0;
}