for (int i = 1; i <= m; i++) { int u = read(); int v = read(); int c = read();
if (u > v) swap(u, v); if (c) { g[u].eb(v), g[v].eb(u); } else { s.ep(u, v); } }
vector<int> path;
auto check = [&]() { int m = path.size(); DSU dsu(m);
for (int i = 0; i < m; i++) { for (int j = i + 1; j < m; j++) { int u = path[i]; int v = path[j]; if (u > v) swap(u, v); if (s.find({u, v}) == s.end()) continue;
dsu.merge(i, j); } }
for (int i = 1; i < m; i++) { if (!dsu.same(0, i)) { return0; } } return1; };
int ans = 0; vector<int> vis(n + 1);
auto dfs = [&](thisauto&& self, int u, int p) -> void { vis[u] = 1; path.eb(u);
if (p > 1) ans += check(); if (p < 4) { for (int v : g[u]) { if (vis[v]) continue; self(v, p + 1); } } vis[u] = 0; path.pop_back(); };
vector<int> a(n), b(n); for (int &x : a) cin >> x, x--; for (int &x : b) cin >> x, x--;
vector<vector<int>> g(n);
vector<int> stk; for (int i = 0; i < n; i++) { while (not stk.empty() and stk.back() < a[i]) stk.pop_back(); if (not stk.empty()) g[stk.back()].eb(a[i]); stk.eb(a[i]); } stk.clear(); for (int i = 0; i < n; i++) { while (not stk.empty() and stk.back() > b[i]) stk.pop_back(); if (not stk.empty()) g[stk.back()].eb(b[i]); stk.eb(b[i]); }
// for (int i = 0; i < n; i++) { // cerr << i << ' ' << g[i] << '\n'; // }
std::priority_queue<int> q;
vector<int> ind(n); vector<int> A; for (int i = 0; i < n; i++) { std::sort(g[i].begin(), g[i].end()); for (int j : g[i]) { // cout << i + 1 << ' ' << j + 1 << '\n'; ind[j]++; } } for (int i = 0; i < n; i++) { if (!ind[i]) { q.ep(-i); // cerr << "inque: " << i + 1 << '\n'; } } while (!q.empty()) { int u = -q.top(); q.pop(); A.eb(u); // cerr << "deque: " << u + 1 << '\n'; for (int v : g[u]) { if (!--ind[v]) { q.ep(-v); // cerr << "inque: " << v + 1 << '\n'; } } }
ind.assign(n, 0);
vector<int> B; for (int i = 0; i < n; i++) { std::sort(g[i].rbegin(), g[i].rend()); for (int j : g[i]) { ind[j]++; } } for (int i = n - 1; ~i; i--) { if (!ind[i]) { q.ep(i); } } while (!q.empty()) { int u = q.top(); q.pop(); B.eb(u); for (int v : g[u]) { if (!--ind[v]) { q.ep(v); } } }
// cerr << A << '\n'; // cerr << B << '\n';
if (a != A or b != B) { cout << "No\n"; return; }
cout << "Yes\n";
vector<pii> ans; for (int i = 0; i < n; i++) { for (int j : g[i]) { ans.eb(i + 1, j + 1); } } cout << ans.size() << '\n'; for (auto [u, v] : ans) cout << u << ' ' << v << '\n'; }
int nod = 2 * n; vector<int> a(nod); for (int i = 0; i < n; i++) { a[i] = read(); a[i + n] = a[i]; }
vector<int> vis(nod); vector<int> dis(nod, inf);
std::priority_queue<Node> q;
dis[0] = 0; q.ep(0, 0);
auto relax = [&](int u, int v, int dist) { if (dis[v] > dis[u] + dist) { dis[v] = dis[u] + dist; if (!vis[v]) q.ep(v, dis[v]); } };
while (!q.empty()) { auto [u, _] = q.top(); q.pop();
if (vis[u]) continue; vis[u] = 1;
if (u < n) { relax(u, (u + a[u]) % n, 1); relax(u, n + (u + a[u] + 1) % n, 2); } else { relax(u, n + (u + a[u]) % n, 1); relax(u, n + (u + 1) % n, 1); } } cout << min(dis[T], dis[T + n]); }