#include #include typedef long long int ll; typedef long double ld; using namespace std; using namespace atcoder; template struct edge { int src, to; T cost; edge(int _to, T _cost) : src(-1), to(_to), cost(_cost) {} edge(int _src, int _to, T _cost) : src(_src), to(_to), cost(_cost) {} edge &operator=(const int &x) { to = x; return *this; } operator int() const { return to; } }; template using Edges = vector>; template using WeightedGraph = vector>; using UnweightedGraph = vector>; // Input of (Unweighted) Graph UnweightedGraph graph(int N, int M = -1, bool is_directed = false, bool is_1origin = true) { UnweightedGraph g(N); if (M == -1) M = N - 1; for (int _ = 0; _ < M; _++) { int x, y; cin >> x >> y; if (is_1origin) x--, y--; g[x].push_back(y); if (!is_directed) g[y].push_back(x); } return g; } // Input of Weighted Graph template WeightedGraph wgraph(int N, int M = -1, bool is_directed = false, bool is_1origin = true) { WeightedGraph g(N); if (M == -1) M = N - 1; for (int _ = 0; _ < M; _++) { int x, y; cin >> x >> y; T c; cin >> c; if (is_1origin) x--, y--; g[x].emplace_back(x, y, c); if (!is_directed) g[y].emplace_back(y, x, c); } return g; } // Input of Edges template Edges esgraph(int N, int M, int is_weighted = true, bool is_1origin = true) { Edges es; for (int _ = 0; _ < M; _++) { int x, y; cin >> x >> y; T c; if (is_weighted) cin >> c; else c = 1; if (is_1origin) x--, y--; es.emplace_back(x, y, c); } return es; } // Input of Adjacency Matrix template vector> adjgraph(int N, int M, T INF, int is_weighted = true, bool is_directed = false, bool is_1origin = true) { vector> d(N, vector(N, INF)); for (int _ = 0; _ < M; _++) { int x, y; cin >> x >> y; T c; if (is_weighted) cin >> c; else c = 1; if (is_1origin) x--, y--; d[x][y] = c; if (!is_directed) d[y][x] = c; } return d; } // 一般のグラフのstからの距離!!!! // unvisited nodes : d = -1 vector Depth(const UnweightedGraph &g, int start = 0) { int n = g.size(); vector ds(n, -1); ds[start] = 0; queue q; q.push(start); while (!q.empty()) { int c = q.front(); q.pop(); int dc = ds[c]; for (auto &d : g[c]) { if (ds[d] == -1) { ds[d] = dc + 1; q.push(d); } } } return ds; } // Depth of Rooted Weighted Tree // unvisited nodes : d = -1 template vector Depth(const WeightedGraph &g, int start = 0) { vector d(g.size(), -1); auto dfs = [&](auto rec, int cur, T val, int par = -1) -> void { d[cur] = val; for (auto &dst : g[cur]) { if (dst == par) continue; rec(rec, dst, val + dst.cost, cur); } }; dfs(dfs, start, 0); return d; } // Diameter of Tree // return value : { {u, v}, length } pair, int> Diameter(const UnweightedGraph &g) { auto d = Depth(g, 0); int u = max_element(begin(d), end(d)) - begin(d); d = Depth(g, u); int v = max_element(begin(d), end(d)) - begin(d); return make_pair(make_pair(u, v), d[v]); } // Diameter of Weighted Tree // return value : { {u, v}, length } template pair, T> Diameter(const WeightedGraph &g) { auto d = Depth(g, 0); int u = max_element(begin(d), end(d)) - begin(d); d = Depth(g, u); int v = max_element(begin(d), end(d)) - begin(d); return make_pair(make_pair(u, v), d[v]); } // nodes on the path u-v ( O(N) ) template vector Path(G &g, int u, int v) { vector ret; int end = 0; auto dfs = [&](auto rec, int cur, int par = -1) -> void { ret.push_back(cur); if (cur == v) { end = 1; return; } for (int dst : g[cur]) { if (dst == par) continue; rec(rec, dst, cur); if (end) return; } if (end) return; ret.pop_back(); }; dfs(dfs, u); return ret; } template vector dijkstra(WeightedGraph &g, int start = 0) { using P = pair; int N = (int)g.size(); vector d(N, T(-1)); priority_queue, greater

> Q; d[start] = 0; Q.emplace(0, start); while (!Q.empty()) { P p = Q.top(); Q.pop(); int cur = p.second; if (d[cur] < p.first) continue; for (auto dst : g[cur]) { if (d[dst] == T(-1) || d[cur] + dst.cost < d[dst]) { d[dst] = d[cur] + dst.cost; Q.emplace(d[dst], dst); } } } return d; } // Nyaanさんのグラフ構造体 // edge 型Tの重みが付いた辺 // Edges 型Tの重みが付いた辺のリスト // WeightedGraph 型Tの重みが付いた(有向/無向)グラフ // UnweightedGraph 重みなし(有向/無向)グラフ // graph(N, M(-1の時N-1), bool is_directed = false, bool is_1origin = true) // 重みなしのグラフを標準入力から読み込み、UnweightedGraph型で返す。 // ex) graph(n,,,) であればN頂点の無向木 // wgraph(,,,) = 重みつきグラフ // esgraph,adjgraph = 隣接リスト,隣接行列を返す // - 機能 - ( だいたいO(N) ) // vector Depth(UnweightedGraph g, int s = 0) // sからの最短距離 // vector Depth(weightedGraph g, int s = 0) // (重み付き根つき木(無向でも可だが遅くなる)) sからの距離 // pair, int> Diameter(UnweightedGraph g) // pair, T> Diameter(WeightedGraph g) // 木の直径の両端,長さを返す // vector Path(G &g, int u, int v) // vector Path(G &g, int u, int v) // (両端含む)パスの頂点列を返す // vector dijkstra(g, s = 0) // ダイクストラ法 (O(ElogV)) // 計算量:O(ElogV) // 届かなかったら-1 #define inf 1010000000 #define llinf 1001000000000000000ll #define pi 3.141592653589793238 #define rep(i, n) for(ll i = 0; i < (n); i++) #define rep1(i, n) for(ll i = 1; i <= (n); i++) #define rep2(i,l,r) for(ll i = (l); i < (r); i++) #define per(i, n) for(ll i = (n)-1; i >= 0; i--) #define each(x, v) for (auto&& x : v) #define rng(a) a.begin(),a.end() #define fi first #define se second #define pb push_back #define eb emplace_back #define pob pop_back #define st string #define pcnt __builtin_popcountll #define bit(n) (1LL<<(n)) template inline T in(){ T x; cin >> x; return (x);} #define vcin(x,n) {for(ll loop=0; loop<(n); loop++) cin>>x[loop];} #define ret(x) { cout<<(x)<