QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#304403#8011. Instituteucup-team180#AC ✓101ms61384kbC++2034.8kb2024-01-13 19:17:212024-01-13 19:17:22

Judging History

你现在查看的是最新测评结果

  • [2024-01-13 19:17:22]
  • 评测
  • 测评结果:AC
  • 用时:101ms
  • 内存:61384kb
  • [2024-01-13 19:17:21]
  • 提交

answer

#pragma region Macros
#ifdef noimi
#include "my_template.hpp"
#else
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <immintrin.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <cfloat>
#include <chrono>
#include <cinttypes>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <immintrin.h>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <tuple>
#include <type_traits>
#include <utility>
#include <variant>

#ifdef noimi
#define oj_local(a, b) b
#else
#define oj_local(a, b) a
#endif

#define LOCAL if(oj_local(0, 1))
#define OJ if(oj_local(1, 0))

using namespace std;
using ll = long long;
using ull = unsigned long long int;
using i128 = __int128_t;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ld = long double;
template <typename T> using vc = vector<T>;
template <typename T> using vvc = vector<vc<T>>;
template <typename T> using vvvc = vector<vvc<T>>;
using vi = vc<int>;
using vl = vc<ll>;
using vpi = vc<pii>;
using vpl = vc<pll>;
template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
template <typename T> int si(const T &x) { return x.size(); }
template <class T, class S> inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); }
template <class T, class S> inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); }
vi iota(int n) {
    vi a(n);
    return iota(a.begin(), a.end(), 0), a;
}
template <typename T> vi iota(const vector<T> &a, bool greater = false) {
    vi res(a.size());
    iota(res.begin(), res.end(), 0);
    sort(res.begin(), res.end(), [&](int i, int j) {
        if(greater) return a[i] > a[j];
        return a[i] < a[j];
    });
    return res;
}

// macros
#define overload5(a, b, c, d, e, name, ...) name
#define overload4(a, b, c, d, name, ...) name
#define endl '\n'
#define REP0(n) for(ll jidlsjf = 0; jidlsjf < n; ++jidlsjf)
#define REP1(i, n) for(ll i = 0; i < (n); ++i)
#define REP2(i, a, b) for(ll i = (a); i < (b); ++i)
#define REP3(i, a, b, c) for(ll i = (a); i < (b); i += (c))
#define rep(...) overload4(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)
#define per0(n) for(int jidlsjf = 0; jidlsjf < (n); ++jidlsjf)
#define per1(i, n) for(ll i = (n)-1; i >= 0; --i)
#define per2(i, a, b) for(ll i = (a)-1; i >= b; --i)
#define per3(i, a, b, c) for(ll i = (a)-1; i >= (b); i -= (c))
#define per(...) overload4(__VA_ARGS__, per3, per2, per1, per0)(__VA_ARGS__)
#define fore0(a) rep(a.size())
#define fore1(i, a) for(auto &&i : a)
#define fore2(a, b, v) for(auto &&[a, b] : v)
#define fore3(a, b, c, v) for(auto &&[a, b, c] : v)
#define fore4(a, b, c, d, v) for(auto &&[a, b, c, d] : v)
#define fore(...) overload5(__VA_ARGS__, fore4, fore3, fore2, fore1, fore0)(__VA_ARGS__)
#define setbits(j, n) for(ll iiiii = (n), j = lowbit(iiiii); iiiii; iiiii ^= 1 << j, j = lowbit(iiiii))
#define perm(v) for(bool permrepflag = true; (permrepflag ? exchange(permrepflag, false) : next_permutation(all(v)));)
#define fi first
#define se second
#define pb push_back
#define ppb pop_back
#define ppf pop_front
#define eb emplace_back
#define drop(s) cout << #s << endl, exit(0)
#define si(c) (int)(c).size()
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define lbg(c, x) distance((c).begin(), lower_bound(all(c), (x), greater{}))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define ubg(c, x) distance((c).begin(), upper_bound(all(c), (x), greater{}))
#define rng(v, l, r) v.begin() + (l), v.begin() + (r)
#define all(c) begin(c), end(c)
#define rall(c) rbegin(c), rend(c)
#define SORT(v) sort(all(v))
#define REV(v) reverse(all(v))
#define UNIQUE(x) SORT(x), x.erase(unique(all(x)), x.end())
template <typename T = ll, typename S> T SUM(const S &v) { return accumulate(all(v), T(0)); }
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define overload2(_1, _2, name, ...) name
#define vec(type, name, ...) vector<type> name(__VA_ARGS__)
#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)                                                                                                                         \
    vector<vector<vector<vector<type>>>> name(a, vector<vector<vector<type>>>(b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))
constexpr pii dx4[4] = {pii{1, 0}, pii{0, 1}, pii{-1, 0}, pii{0, -1}};
constexpr pii dx8[8] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};

namespace yesno_impl {
const string YESNO[2] = {"NO", "YES"};
const string YesNo[2] = {"No", "Yes"};
const string yesno[2] = {"no", "yes"};
const string firstsecond[2] = {"second", "first"};
const string FirstSecond[2] = {"Second", "First"};
const string possiblestr[2] = {"impossible", "possible"};
const string Possiblestr[2] = {"Impossible", "Possible"};
void YES(bool t = 1) { cout << YESNO[t] << endl; }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { cout << YesNo[t] << endl; }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { cout << yesno[t] << endl; }
void no(bool t = 1) { yes(!t); }
void first(bool t = 1) { cout << firstsecond[t] << endl; }
void First(bool t = 1) { cout << FirstSecond[t] << endl; }
void possible(bool t = 1) { cout << possiblestr[t] << endl; }
void Possible(bool t = 1) { cout << Possiblestr[t] << endl; }
}; // namespace yesno_impl
using namespace yesno_impl;

#define INT(...)                                                                                                                                               \
    int __VA_ARGS__;                                                                                                                                           \
    IN(__VA_ARGS__)
#define INTd(...)                                                                                                                                              \
    int __VA_ARGS__;                                                                                                                                           \
    IN2(__VA_ARGS__)
#define LL(...)                                                                                                                                                \
    ll __VA_ARGS__;                                                                                                                                            \
    IN(__VA_ARGS__)
#define LLd(...)                                                                                                                                               \
    ll __VA_ARGS__;                                                                                                                                            \
    IN2(__VA_ARGS__)
#define STR(...)                                                                                                                                               \
    string __VA_ARGS__;                                                                                                                                        \
    IN(__VA_ARGS__)
#define CHR(...)                                                                                                                                               \
    char __VA_ARGS__;                                                                                                                                          \
    IN(__VA_ARGS__)
#define DBL(...)                                                                                                                                               \
    double __VA_ARGS__;                                                                                                                                        \
    IN(__VA_ARGS__)
#define VEC(type, name, size)                                                                                                                                  \
    vector<type> name(size);                                                                                                                                   \
    IN(name)
#define VECd(type, name, size)                                                                                                                                 \
    vector<type> name(size);                                                                                                                                   \
    IN2(name)
#define VEC2(type, name1, name2, size)                                                                                                                         \
    vector<type> name1(size), name2(size);                                                                                                                     \
    for(int i = 0; i < size; i++) IN(name1[i], name2[i])
#define VEC2d(type, name1, name2, size)                                                                                                                        \
    vector<type> name1(size), name2(size);                                                                                                                     \
    for(int i = 0; i < size; i++) IN2(name1[i], name2[i])
#define VEC3(type, name1, name2, name3, size)                                                                                                                  \
    vector<type> name1(size), name2(size), name3(size);                                                                                                        \
    for(int i = 0; i < size; i++) IN(name1[i], name2[i], name3[i])
#define VEC3d(type, name1, name2, name3, size)                                                                                                                 \
    vector<type> name1(size), name2(size), name3(size);                                                                                                        \
    for(int i = 0; i < size; i++) IN2(name1[i], name2[i], name3[i])
#define VEC4(type, name1, name2, name3, name4, size)                                                                                                           \
    vector<type> name1(size), name2(size), name3(size), name4(size);                                                                                           \
    for(int i = 0; i < size; i++) IN(name1[i], name2[i], name3[i], name4[i]);
#define VEC4d(type, name1, name2, name3, name4, size)                                                                                                          \
    vector<type> name1(size), name2(size), name3(size), name4(size);                                                                                           \
    for(int i = 0; i < size; i++) IN2(name1[i], name2[i], name3[i], name4[i]);
#define VV(type, name, h, w)                                                                                                                                   \
    vector<vector<type>> name(h, vector<type>(w));                                                                                                             \
    IN(name)
#define VVd(type, name, h, w)                                                                                                                                  \
    vector<vector<type>> name(h, vector<type>(w));                                                                                                             \
    IN2(name)
int scan() { return getchar(); }
void scan(int &a) { cin >> a; }
void scan(long long &a) { cin >> a; }
void scan(char &a) { cin >> a; }
void scan(double &a) { cin >> a; }
void scan(string &a) { cin >> a; }
template <class T, class S> void scan(pair<T, S> &p) { scan(p.first), scan(p.second); }
template <class T> void scan(vector<T> &);
template <class T> void scan(vector<T> &a) {
    for(auto &i : a) scan(i);
}
template <class T> void scan(T &a) { cin >> a; }
void IN() {}
void IN2() {}
template <class Head, class... Tail> void IN(Head &head, Tail &...tail) {
    scan(head);
    IN(tail...);
}
template <class Head, class... Tail> void IN2(Head &head, Tail &...tail) {
    scan(head);
    --head;
    IN2(tail...);
}

template <int p = -1> void pat() {}
template <int p = -1, class Head, class... Tail> void pat(Head &h, Tail &...tail) {
    h += p;
    pat<p>(tail...);
}

template <typename T, typename S> T ceil(T x, S y) {
    assert(y);
    return (y < 0 ? ceil(-x, -y) : (x > 0 ? (x + y - 1) / y : x / y));
}

template <typename T, typename S> T floor(T x, S y) {
    assert(y);
    return (y < 0 ? floor(-x, -y) : (x > 0 ? x / y : x / y - (x % y == 0 ? 0 : 1)));
}
template <typename T, typename S, typename U> U bigmul(const T &x, const S &y, const U &lim) { // clamp(x * y, -lim, lim)
    if(x < 0 and y < 0) return bigmul(-x, -y, lim);
    if(x < 0) return -bigmul(-x, y, lim);
    if(y < 0) return -bigmul(x, -y, lim);
    return y == 0 or x <= lim / y ? x * y : lim;
}
template <class T> T POW(T x, int n) {
    T res = 1;
    for(; n; n >>= 1, x *= x)
        if(n & 1) res *= x;
    return res;
}
template <class T, class S> T POW(T x, S n, const ll &mod) {
    T res = 1;
    x %= mod;
    for(; n; n >>= 1, x = x * x % mod)
        if(n & 1) res = res * x % mod;
    return res;
}
vector<pll> factor(ll x) {
    vector<pll> ans;
    for(ll i = 2; i * i <= x; i++)
        if(x % i == 0) {
            ans.push_back({i, 1});
            while((x /= i) % i == 0) ans.back().second++;
        }
    if(x != 1) ans.push_back({x, 1});
    return ans;
}
template <class T> vector<T> divisor(T x) {
    vector<T> ans;
    for(T i = 1; i * i <= x; i++)
        if(x % i == 0) {
            ans.pb(i);
            if(i * i != x) ans.pb(x / i);
        }
    return ans;
}
template <typename T> void zip(vector<T> &x) {
    vector<T> y = x;
    UNIQUE(y);
    for(int i = 0; i < x.size(); ++i) { x[i] = lb(y, x[i]); }
}
template <class S> void fold_in(vector<S> &v) {}
template <typename Head, typename... Tail, class S> void fold_in(vector<S> &v, Head &&a, Tail &&...tail) {
    for(auto e : a) v.emplace_back(e);
    fold_in(v, tail...);
}
template <class S> void renumber(vector<S> &v) {}
template <typename Head, typename... Tail, class S> void renumber(vector<S> &v, Head &&a, Tail &&...tail) {
    for(auto &&e : a) e = lb(v, e);
    renumber(v, tail...);
}
template <class S, class... Args> vector<S> zip(vector<S> &head, Args &&...args) {
    vector<S> v;
    fold_in(v, head, args...);
    sort(all(v)), v.erase(unique(all(v)), v.end());
    renumber(v, head, args...);
    return v;
}

template <typename S> void rearrange(const vector<S> &id) {}
template <typename S, typename T> void rearrange_exec(const vector<S> &id, vector<T> &v) {
    vector<T> w(v.size());
    rep(i, si(id)) w[i] = v[id[i]];
    v.swap(w);
}
// 並び替える順番, 並び替える vector 達
template <typename S, typename Head, typename... Tail> void rearrange(const vector<S> &id, Head &a, Tail &...tail) {
    rearrange_exec(id, a);
    rearrange(id, tail...);
}

template <typename T> vector<T> RUI(const vector<T> &v) {
    vector<T> res(v.size() + 1);
    for(int i = 0; i < v.size(); i++) res[i + 1] = res[i] + v[i];
    return res;
}
template <typename T> void zeta_supersetsum(vector<T> &f) {
    int n = f.size();
    for(int i = 1; i < n; i <<= 1) rep(b, n) if(!(i & b)) f[b] += f[b | i];
}

template <typename T> void zeta_subsetsum(vector<T> &f) {
    int n = f.size();
    for(int i = 1; i < n; i <<= 1) rep(b, n) if(!(i & b)) f[b | i] += f[b];
}
template <typename T> void mobius_subset(vector<T> &f) {
    int n = f.size();
    for(int i = 1; i < n; i <<= 1) rep(b, n) if(!(i & b)) f[b] -= f[b | i];
}
template <typename T> void mobius_superset(vector<T> &f) {
    int n = f.size();
    for(int i = 1; i < n; i <<= 1) rep(b, n) if(!(i & b)) f[b | i] -= f[b];
}
// 反時計周りに 90 度回転
template <typename T> void rot(vector<vector<T>> &v) {
    if(empty(v)) return;
    int n = v.size(), m = v[0].size();
    vector<vector<T>> res(m, vector<T>(n));
    rep(i, n) rep(j, m) res[m - 1 - j][i] = v[i][j];
    v.swap(res);
}

vector<int> counter(const vector<int> &v, int max_num = -1) {
    if(max_num == -1) max_num = MAX(v);
    vector<int> res(max_num + 1);
    fore(e, v) res[e]++;
    return res;
}

// x in [l, r)
template <class T, class S> bool inc(const T &x, const S &l, const S &r) { return l <= x and x < r; }
template <class T, class S> bool inc(const T &x, const pair<S, S> &p) { return p.first <= x and x < p.second; }

// 便利関数
constexpr ll ten(int n) { return n == 0 ? 1 : ten(n - 1) * 10; }
constexpr ll tri(ll n) { return n * (n + 1) / 2; }
// l + ... + r
constexpr ll tri(ll l, ll r) { return (l + r) * (r - l + 1) / 2; }
ll max(int x, ll y) { return max((ll)x, y); }
ll max(ll x, int y) { return max(x, (ll)y); }
int min(int x, ll y) { return min((ll)x, y); }
int min(ll x, int y) { return min(x, (ll)y); }
// bit 演算系
#define bit(i) (1LL << i)       // (1 << i)
#define test(b, i) (b >> i & 1) // b の i bit 目が立っているか
ll pow2(int i) { return 1LL << i; }
int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
int topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
int lowbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }
int lowbit(ll a) { return a == 0 ? 64 : __builtin_ctzll(a); }
// int allbit(int n) { return (1 << n) - 1; }
constexpr ll mask(int n) { return (1LL << n) - 1; }
// int popcount(signed t) { return __builtin_popcount(t); }
// int popcount(ll t) { return __builtin_popcountll(t); }
int popcount(uint64_t t) { return __builtin_popcountll(t); }
static inline uint64_t popcount64(uint64_t x) {
    uint64_t m1 = 0x5555555555555555ll;
    uint64_t m2 = 0x3333333333333333ll;
    uint64_t m4 = 0x0F0F0F0F0F0F0F0Fll;
    uint64_t h01 = 0x0101010101010101ll;

    x -= (x >> 1) & m1;
    x = (x & m2) + ((x >> 2) & m2);
    x = (x + (x >> 4)) & m4;

    return (x * h01) >> 56;
}
bool ispow2(int i) { return i && (i & -i) == i; }

ll rnd(ll l, ll r) { //[l, r)
#ifdef noimi
    static mt19937_64 gen;
#else
    static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
#endif
    return uniform_int_distribution<ll>(l, r - 1)(gen);
}
ll rnd(ll n) { return rnd(0, n); }

template <class t> void random_shuffle(vc<t> &a) { rep(i, si(a)) swap(a[i], a[rnd(0, i + 1)]); }

int in() {
    int x;
    cin >> x;
    return x;
}
ll lin() {
    unsigned long long x;
    cin >> x;
    return x;
}

template <class T, class S> pair<T, S> operator-(const pair<T, S> &x) { return pair<T, S>(-x.first, -x.second); }
template <class T, class S> pair<T, S> operator-(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi - y.fi, x.se - y.se); }
template <class T, class S> pair<T, S> operator+(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi + y.fi, x.se + y.se); }
template <class T> pair<T, T> operator&(const pair<T, T> &l, const pair<T, T> &r) { return pair<T, T>(max(l.fi, r.fi), min(l.se, r.se)); }
template <class T, class S> pair<T, S> operator+=(pair<T, S> &l, const pair<T, S> &r) { return l = l + r; }
template <class T, class S> pair<T, S> operator-=(pair<T, S> &l, const pair<T, S> &r) { return l = l - r; }
template <class T> bool intersect(const pair<T, T> &l, const pair<T, T> &r) { return (l.se < r.se ? r.fi < l.se : l.fi < r.se); }

template <class T> vector<T> &operator++(vector<T> &v) {
    fore(e, v) e++;
    return v;
}
template <class T> vector<T> operator++(vector<T> &v, int) {
    auto res = v;
    fore(e, v) e++;
    return res;
}
template <class T> vector<T> &operator--(vector<T> &v) {
    fore(e, v) e--;
    return v;
}
template <class T> vector<T> operator--(vector<T> &v, int) {
    auto res = v;
    fore(e, v) e--;
    return res;
}
template <class T> void connect(vector<T> &l, const vector<T> &r) { fore(e, r) l.eb(e); }
template <class T> vector<T> operator+(const vector<T> &l, const vector<T> &r) {
    vector<T> res(max(si(l), si(r)));
    rep(i, si(l)) res[i] += l[i];
    rep(i, si(r)) res[i] += r[i];
    return res;
}
template <class T> vector<T> operator-(const vector<T> &l, const vector<T> &r) {
    vector<T> res(max(si(l), si(r)));
    rep(i, si(l)) res[i] += l[i];
    rep(i, si(r)) res[i] -= r[i];
    return res;
}
template <class T> vector<T> &operator+=(const vector<T> &l, const vector<T> &r) {
    if(si(l) < si(r)) l.resize(si(r));
    rep(i, si(r)) l[i] += r[i];
    return l;
}
template <class T> vector<T> &operator-=(const vector<T> &l, const vector<T> &r) {
    if(si(l) < si(r)) l.resize(si(r));
    rep(i, si(r)) l[i] -= r[i];
    return l;
}
template <class T> vector<T> &operator+=(vector<T> &v, const T &x) {
    fore(e, v) e += x;
    return v;
}
template <class T> vector<T> &operator-=(vector<T> &v, const T &x) {
    fore(e, v) e -= x;
    return v;
}

template <typename T> struct edge {
    int from, to;
    T cost;
    int id;
    edge(int to, T cost) : from(-1), to(to), cost(cost) {}
    edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}
    edge(int from, int to, T cost, int id) : from(from), to(to), cost(cost), id(id) {}
    constexpr bool operator<(const edge<T> &rhs) const noexcept { return cost < rhs.cost; }
    edge &operator=(const int &x) {
        to = x;
        return *this;
    }
    operator int() const { return to; }
    friend ostream operator<<(ostream &os, const edge &e) { return os << e.to; }
};
template <typename T> using Edges = vector<edge<T>>;

template <typename T = int> Edges<T> read_edges(int m, bool weighted = false) {
    Edges<T> res;
    res.reserve(m);
    for(int i = 0; i < m; i++) {
        int u, v, c = 0;
        scan(u), scan(v), u--, v--;
        if(weighted) scan(c);
        res.eb(u, v, c, i);
    }
    return res;
}

using Tree = vector<vector<int>>;
using Graph = vector<vector<int>>;
template <class T> using Wgraph = vector<vector<edge<T>>>;
Graph getG(int n, int m = -1, bool directed = false, int margin = 1) {
    Tree res(n);
    if(m == -1) m = n - 1;
    while(m--) {
        int a, b;
        cin >> a >> b;
        a -= margin, b -= margin;
        res[a].emplace_back(b);
        if(!directed) res[b].emplace_back(a);
    }
    return res;
}
Graph getTreeFromPar(int n, int margin = 1) {
    Graph res(n);
    for(int i = 1; i < n; i++) {
        int a;
        cin >> a;
        res[a - margin].emplace_back(i);
    }
    return res;
}
template <class T> Wgraph<T> getWg(int n, int m = -1, bool directed = false, int margin = 1) {
    Wgraph<T> res(n);
    if(m == -1) m = n - 1;
    while(m--) {
        int a, b;
        T c;
        scan(a), scan(b), scan(c);
        a -= margin, b -= margin;
        res[a].emplace_back(b, c);
        if(!directed) res[b].emplace_back(a, c);
    }
    return res;
}
void add(Graph &G, int x, int y) { G[x].eb(y), G[y].eb(x); }
template <class S, class T> void add(Wgraph<S> &G, int x, int y, T c) { G[x].eb(y, c), G[y].eb(x, c); }

#define TEST                                                                                                                                                   \
    INT(testcases);                                                                                                                                            \
    while(testcases--)

i128 abs(const i128 &x) { return x > 0 ? x : -x; }
istream &operator>>(istream &is, i128 &v) {
    string s;
    is >> s;
    v = 0;
    for(int i = 0; i < (int)s.size(); i++) {
        if(isdigit(s[i])) { v = v * 10 + s[i] - '0'; }
    }
    if(s[0] == '-') { v *= -1; }
    return is;
}

ostream &operator<<(ostream &os, const i128 &v) {
    if(v == 0) { return (os << "0"); }
    i128 num = v;
    if(v < 0) {
        os << '-';
        num = -num;
    }
    string s;
    for(; num > 0; num /= 10) { s.push_back((char)(num % 10) + '0'); }
    reverse(s.begin(), s.end());
    return (os << s);
}
namespace aux {
template <typename T, unsigned N, unsigned L> struct tp {
    static void output(std::ostream &os, const T &v) {
        os << std::get<N>(v) << (&os == &cerr ? ", " : " ");
        tp<T, N + 1, L>::output(os, v);
    }
};
template <typename T, unsigned N> struct tp<T, N, N> {
    static void output(std::ostream &os, const T &v) { os << std::get<N>(v); }
};
} // namespace aux
template <typename... Ts> std::ostream &operator<<(std::ostream &os, const std::tuple<Ts...> &t) {
    if(&os == &cerr) { os << '('; }
    aux::tp<std::tuple<Ts...>, 0, sizeof...(Ts) - 1>::output(os, t);
    if(&os == &cerr) { os << ')'; }
    return os;
}
template <typename T, typename S, typename U> std::ostream &operator<<(std::ostream &os, const priority_queue<T, S, U> &_pq) {
    auto pq = _pq;
    vector<T> res;
    while(!empty(pq)) res.emplace_back(pq.top()), pq.pop();
    return os << res;
}
template <class T, class S> ostream &operator<<(ostream &os, const pair<T, S> &p) {
    if(&os == &cerr) { return os << "(" << p.first << ", " << p.second << ")"; }
    return os << p.first << " " << p.second;
}
template <class Ch, class Tr, class Container> std::basic_ostream<Ch, Tr> &operator<<(std::basic_ostream<Ch, Tr> &os, const Container &x) {
    bool f = true;
    if(&os == &cerr) os << "[";
    for(auto &y : x) {
        if(&os == &cerr)
            os << (f ? "" : ", ") << y;
        else
            os << (f ? "" : " ") << y;
        f = false;
    }
    if(&os == &cerr) os << "]";
    return os;
}

#define dump(...) static_cast<void>(0)
#define dbg(...) static_cast<void>(0)

void OUT() { cout << endl; }
template <class Head, class... Tail> void OUT(const Head &head, const Tail &...tail) {
    cout << head;
    if(sizeof...(tail)) cout << ' ';
    OUT(tail...);
}

template <typename T> static constexpr T inf = numeric_limits<T>::max() / 2;
template <class T, class S> constexpr pair<T, S> inf<pair<T, S>> = {inf<T>, inf<S>};

template <class T> void OUT2(const T &t, T INF = inf<T>, T res = -1) { OUT(t != INF ? t : res); }
template <class T> void OUT2(vector<T> &v, T INF = inf<T>, T res = -1) {
    fore(e, v) if(e == INF) e = res;
    OUT(v);
    fore(e, v) if(e == res) e = INF;
}

template <class F> struct REC {
    F f;
    REC(F &&f_) : f(forward<F>(f_)) {}
    template <class... Args> auto operator()(Args &&...args) const { return f(*this, forward<Args>(args)...); }
};

template <class S> vector<pair<S, int>> runLength(const vector<S> &v) {
    vector<pair<S, int>> res;
    for(auto &e : v) {
        if(res.empty() or res.back().fi != e)
            res.eb(e, 1);
        else
            res.back().se++;
    }
    return res;
}
vector<pair<char, int>> runLength(const string &v) {
    vector<pair<char, int>> res;
    for(auto &e : v) {
        if(res.empty() or res.back().fi != e)
            res.eb(e, 1);
        else
            res.back().se++;
    }
    return res;
}

struct string_converter {
    char start = 0;
    char type(const char &c) const { return (islower(c) ? 'a' : isupper(c) ? 'A' : isdigit(c) ? '0' : 0); }
    int convert(const char &c) {
        if(!start) start = type(c);
        return c - start;
    }
    int convert(const char &c, const string &chars) { return chars.find(c); }
    template <typename T> auto convert(const T &v) {
        vector<decltype(convert(v[0]))> ret;
        ret.reserve(size(v));
        for(auto &&e : v) ret.emplace_back(convert(e));
        return ret;
    }
    template <typename T> auto convert(const T &v, const string &chars) {
        vector<decltype(convert(v[0], chars))> ret;
        ret.reserve(size(v));
        for(auto &&e : v) ret.emplace_back(convert(e, chars));
        return ret;
    }
    int operator()(const char &v, char s = 0) {
        start = s;
        return convert(v);
    }
    int operator()(const char &v, const string &chars) { return convert(v, chars); }
    template <typename T> auto operator()(const T &v, char s = 0) {
        start = s;
        return convert(v);
    }
    template <typename T> auto operator()(const T &v, const string &chars) { return convert(v, chars); }
} toint;

template <class T, class F> T bin_search(T ok, T ng, const F &f) {
    while((ok > ng ? ok - ng : ng - ok) > 1) {
        T mid = ok + ng >> 1;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}
template <class T, class F> T bin_search_double(T ok, T ng, const F &f, int iter = 80) {
    while(iter--) {
        // T mid = (ok + ng) / 2;
        T mid = sqrtl(ok * ng);
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}

struct Setup_io {
    Setup_io() {
        ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
        cout << fixed << setprecision(11);
    }
} setup_io;

#endif

#pragma endregion

struct SCC {
  private:
    template <typename T, bool Cond = is_void<T>::value> struct E;
    template <typename T> struct E<T, false> {
        int to;
        T cost;
        E() {}
        E(const int &v, const T &c) : to(v), cost(c) {}
        operator int() const { return to; }
    };
    template <typename T> struct E<T, true> {
        int to;
        E() {}
        E(const int &v) : to(v) {}
        operator int() const { return to; }
    };

    template <typename T = void> struct StaticGraph {
      private:
        template <typename It> struct Es {
            It b, e;
            It begin() const { return b; }
            It end() const { return e; }
            int size() const { return int(e - b); }
            auto &&operator[](int i) const { return b[i]; }
        };

        int N, M, ec;
        vector<int> head;
        vector<pair<int, E<T>>> buf;
        vector<E<T>> es;

        void build() {
            partial_sum(begin(head), end(head), begin(head));
            es.resize(M);
            for(auto &&[u, e] : buf) es[--head[u]] = e;
        }

      public:
        StaticGraph(int _n, int _m) : N(_n), M(_m), ec(0), head(N + 1, 0) { buf.reserve(M); }

        template <typename... Args> void add_edge(int u, Args &&...args) {
#pragma GCC diagnostic ignored "-Wnarrowing"
            buf.emplace_back(u, E<T>{std::forward<Args>(args)...});
#pragma GCC diagnostic warning "-Wnarrowing"
            ++head[u];
            if((int)buf.size() == M) build();
        }

        Es<typename vector<E<T>>::iterator> operator[](int u) { return {begin(es) + head[u], begin(es) + head[u + 1]}; }
        const Es<typename vector<E<T>>::const_iterator> operator[](int u) const { return {begin(es) + head[u], begin(es) + head[u + 1]}; }
        int size() const { return N; }
    };

  public:
    int n;
    vector<pair<int, int>> edges;
    vector<int> comp;

    int num; // 強連結成分の数

    template <typename G> SCC(const G &g) : n(g.size()), comp(g.size()), num(0) {
        int M = 0;
        for(auto &&e : g) M += e.size();
        edges.reserve(M);
        for(int i = 0; i < n; i++)
            for(auto &&e : g[i]) edges.emplace_back(i, e);
        build();
    };
    SCC(int n, int m = 0) : n(n), comp(n, -1), num(0) { edges.reserve(m); };
    inline void add(int a, int b) { edges.emplace_back(a, b); }
    const int operator[](int k) const { return comp[k]; }

    void build() {
        StaticGraph<void> g(n, edges.size());
        for(auto &&[u, v] : edges) g.add_edge(u, v);
        vector<int> ord(n, -1), low(n), visit(n), node_idx(n), par(n, -1);
        int now = 0, vi = 0;

        for(int x = 0; x < n; x++) {
            if(ord[x] == -1) {
                int tmp = x;
                while(tmp >= 0) {
                    if(!node_idx[tmp]) low[tmp] = ord[tmp] = now++, visit[vi++] = tmp;
                    if(node_idx[tmp] == g[tmp].size()) {
                        if(low[tmp] == ord[tmp]) {
                            while(true) {
                                int u = visit[--vi];
                                ord[u] = n, comp[u] = num;
                                if(u == tmp) break;
                            }
                            num++;
                        }
                        if(par[tmp] < 0) break;
                        low[par[tmp]] = min(low[par[tmp]], low[tmp]);
                        tmp = par[tmp];
                    } else {
                        int nxt = g[tmp][node_idx[tmp]++];
                        if(ord[nxt] == -1) {
                            par[nxt] = exchange(tmp, nxt);
                        } else {
                            low[tmp] = min(low[tmp], ord[nxt]);
                        }
                    }
                }
            }
        }
        for(auto &&e : comp) { e = num - 1 - e; }
    }
    // 強連結成分を潰した DAG を返す
    Graph getGraph() {
        Graph res(num);
        for(auto &&[u, v] : edges) {
            if(comp[u] != comp[v]) res[comp[u]].emplace_back(comp[v]);
        }
        vector<int> mem(n);
        rep(i, num) {
            rep(j, si(res[i])) {
                if(mem[res[i][j]]++) {
                    swap(res[i][j], res[i].back());
                    res[i].pop_back();
                    j--;
                }
            }
            fore(e, res[i]) mem[e] = 0;
        }
        return res;
    }
    // 強連結成分ごとに属する頂点を返す
    vector<vector<int>> belong() {
        vector<vector<int>> res(num);
        rep(i, n) res[comp[i]].emplace_back(i);
        return res;
    }
};

int main() {
    INT(n, m);
    auto g = getWg<int>(n, m, true);
    Graph G(n);
    rep(i, n) fore(e, g[i]) {
        if(e.cost == 2) G[i].eb(e);
    }
    SCC scc(G);
    vi ok(n);
    auto res = scc.belong();
    auto gg = scc.getGraph();
    rep(i, si(res)) {
        if(si(gg[i]) == 0) continue;
        fore(e, res[i]) ok[e] = true;
    }

    queue<int> q;
    vi vis(n);
    vis[0] = true;
    q.emplace(0);
    while(!empty(q)) {
        int x = q.front();
        q.pop();
        if(ok[x]) drop(Yes);
        fore(e, g[x]) {
            if(!vis[e]) {
                vis[e] = true;
                q.emplace(e);
            }
        }
    }
    drop(No);
}

这程序好像有点Bug,我给组数据试试?

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3576kb

input:

3 4
1 2 1
2 3 2
3 2 1
3 1 2

output:

Yes

result:

ok answer is YES

Test #2:

score: 0
Accepted
time: 0ms
memory: 3644kb

input:

6 8
1 2 1
2 3 2
3 2 2
3 4 1
4 1 2
1 5 2
5 4 2
6 1 2

output:

No

result:

ok answer is NO

Test #3:

score: 0
Accepted
time: 1ms
memory: 3784kb

input:

1000 1000
141 466 1
634 659 1
179 96 2
445 344 2
993 974 1
310 114 2
32 333 1
758 832 1
834 1 1
874 825 2
480 61 2
765 100 2
804 616 1
496 545 1
786 261 2
899 263 1
962 237 2
766 807 1
561 583 1
35 425 1
201 291 1
6 142 1
61 386 2
785 861 2
386 986 2
288 769 2
850 209 1
660 259 2
258 143 2
646 715 2...

output:

Yes

result:

ok answer is YES

Test #4:

score: 0
Accepted
time: 1ms
memory: 3828kb

input:

1000 3000
719 279 2
857 23 1
984 625 2
904 509 2
892 456 2
589 195 2
718 932 2
608 363 1
474 672 1
217 993 2
165 895 2
727 329 2
932 404 2
952 146 2
201 272 2
412 895 2
228 267 2
396 365 2
813 794 2
259 250 1
968 764 2
100 76 2
924 665 2
981 931 2
292 975 2
903 649 2
793 101 2
54 703 1
853 58 2
356 ...

output:

Yes

result:

ok answer is YES

Test #5:

score: 0
Accepted
time: 1ms
memory: 3804kb

input:

1000 3000
686 470 2
132 418 2
775 962 2
814 8 2
450 767 2
580 243 2
742 534 2
508 304 2
396 513 2
731 385 2
499 309 2
144 150 2
111 209 2
340 189 1
219 755 2
511 655 2
428 941 2
165 707 2
253 619 2
140 766 2
999 132 2
415 101 2
887 192 2
598 262 2
312 675 1
97 527 2
407 179 2
11 154 1
107 996 2
586 ...

output:

No

result:

ok answer is NO

Test #6:

score: 0
Accepted
time: 3ms
memory: 5548kb

input:

10000 10000
1496 8533 1
6761 8802 2
3147 8733 2
7074 899 1
4191 9520 2
2576 1464 1
8600 116 2
72 5894 1
1605 6769 1
344 2583 2
9951 8053 2
2663 1396 1
3172 7307 1
8490 8085 2
6623 7814 2
680 4471 2
4906 3810 1
5952 8860 1
9670 3644 2
7993 6329 2
4666 1119 2
3115 3676 2
4506 2979 2
1451 2540 2
5002 9...

output:

No

result:

ok answer is NO

Test #7:

score: 0
Accepted
time: 7ms
memory: 5632kb

input:

10000 30000
8022 6065 2
8185 3186 1
9803 6371 1
4947 1271 2
926 9103 2
8367 1328 2
6314 1627 2
203 4366 2
9992 1021 2
5092 9288 1
7412 6217 2
4569 5568 2
7064 6970 2
4363 1581 2
772 6243 2
2571 4950 2
5821 8367 1
7985 5827 2
2064 4754 2
900 605 1
2083 3137 2
7852 1194 2
4679 6769 2
9389 6753 2
2980 ...

output:

Yes

result:

ok answer is YES

Test #8:

score: 0
Accepted
time: 7ms
memory: 5544kb

input:

10000 30000
6708 6418 2
120 3115 2
1647 6703 1
86 1015 2
5041 8379 2
1926 2108 2
1030 4579 2
1129 4269 2
7245 1725 2
9605 679 2
2903 1467 2
233 9636 2
8796 738 2
1535 7292 2
6010 1476 2
9300 4436 2
7465 5575 2
4508 1537 2
4352 9653 2
6646 4932 2
6069 2244 2
8361 4603 2
3566 9063 2
6836 5173 2
2194 3...

output:

No

result:

ok answer is NO

Test #9:

score: 0
Accepted
time: 95ms
memory: 61384kb

input:

300000 300000
94246 283175 1
83027 278500 2
265400 62933 2
174359 187478 2
175633 104311 1
279454 288119 1
143302 18555 1
258847 186237 1
207649 136874 1
182701 13491 1
261536 220848 1
206849 280776 2
60075 295 2
242474 256281 2
293852 21768 2
36248 183324 2
145642 275253 1
9956 11629 2
25110 265376...

output:

No

result:

ok answer is NO

Test #10:

score: 0
Accepted
time: 94ms
memory: 53232kb

input:

300000 300000
2755 160424 2
156539 216928 2
41979 111770 2
94794 284215 2
88031 115072 1
55508 189463 2
142066 99620 2
68946 262040 2
234053 268389 2
177636 61056 2
216374 97188 1
189152 250356 2
128748 241240 2
260431 9729 2
165046 99023 2
106219 190566 1
228664 97992 2
80967 18715 2
118443 178799 ...

output:

Yes

result:

ok answer is YES

Test #11:

score: 0
Accepted
time: 101ms
memory: 53108kb

input:

300000 300000
201841 218620 2
25571 244075 2
148023 244788 1
12618 187161 2
123903 157292 2
140719 283744 2
48104 57739 2
94140 217701 2
260292 235506 2
244136 52545 2
113842 82619 2
284670 286686 2
102210 172102 2
93693 135210 2
169155 31435 1
121590 294638 2
231463 149656 2
132100 245329 2
78578 2...

output:

No

result:

ok answer is NO

Test #12:

score: 0
Accepted
time: 45ms
memory: 14800kb

input:

10000 300000
8794 9638 2
4124 3198 2
3709 4571 1
7094 2604 2
9787 8634 1
9259 7034 1
805 8951 1
523 1299 2
4067 2337 1
3442 8604 1
990 229 2
5669 9222 2
794 5188 2
7066 4135 1
2328 6841 2
5400 3669 2
8572 6053 2
5808 755 2
1035 9229 2
4699 4976 1
5592 6836 2
3316 5324 1
911 8988 1
6872 3245 2
1266 1...

output:

No

result:

ok answer is NO

Test #13:

score: 0
Accepted
time: 83ms
memory: 38816kb

input:

200000 300000
51429 85416 2
35500 102166 2
77261 111280 2
109595 198771 2
52752 98485 2
123959 146471 1
71558 134628 2
53580 97704 2
41770 26184 2
38726 42080 2
32420 161261 1
93622 14014 2
62442 37971 2
97434 174665 2
35402 17953 2
32481 189105 2
133381 197488 2
59630 152080 2
145316 134738 2
15637...

output:

Yes

result:

ok answer is YES

Test #14:

score: 0
Accepted
time: 82ms
memory: 38644kb

input:

200000 300000
100604 79567 2
78619 82912 2
160923 6515 2
111511 22849 2
191823 122510 2
184746 42969 1
367 87329 2
12385 186721 2
22322 89968 2
30296 23410 2
55379 95908 2
78731 185005 2
16026 95076 2
117553 81021 2
17586 162896 2
112457 64630 2
98272 194597 2
94737 47348 2
182434 141187 2
48901 148...

output:

No

result:

ok answer is NO

Test #15:

score: 0
Accepted
time: 69ms
memory: 25780kb

input:

100000 300000
60010 76924 2
98779 52066 2
29705 3673 2
19807 92766 2
89092 55572 2
57798 98074 2
49433 4252 2
9568 11858 2
6068 37755 2
42537 69332 2
34120 95829 2
70957 69524 1
6979 42493 2
18708 80499 2
23615 10086 1
54682 14663 2
66240 6913 1
96259 63852 2
33994 20136 2
50051 79458 2
59557 23910 ...

output:

Yes

result:

ok answer is YES

Test #16:

score: 0
Accepted
time: 82ms
memory: 25856kb

input:

100000 300000
31467 31057 2
53376 30862 2
34548 19018 1
49459 40636 2
24713 26389 2
72031 39008 2
88386 42964 2
6089 36050 2
95525 7354 2
2150 65839 2
23017 48586 2
96644 53330 1
34717 92329 2
60439 23051 2
93738 40918 2
14792 19952 2
47044 31402 2
50447 82391 2
39716 49649 2
53032 32891 1
21843 227...

output:

No

result:

ok answer is NO

Test #17:

score: 0
Accepted
time: 60ms
memory: 18176kb

input:

50000 300000
13657 42151 1
4207 48263 2
19913 14856 1
20252 19946 1
36552 16773 2
15641 47894 1
31929 33862 1
37186 35146 1
26141 19435 1
13344 16689 1
40537 30282 1
44007 12832 2
34199 4971 2
9613 32152 1
22680 7733 1
22821 37016 1
28192 6124 2
20231 20606 2
31231 11050 1
29171 1156 2
10913 10822 2...

output:

Yes

result:

ok answer is YES

Test #18:

score: 0
Accepted
time: 57ms
memory: 18312kb

input:

50000 300000
27901 2832 1
36611 18040 1
11569 29790 1
18772 5119 1
7295 20713 2
34886 41675 1
25208 25186 2
16871 28421 1
16342 25825 1
45452 6926 1
19333 47812 1
49794 31792 2
47593 29029 1
28847 13670 2
10893 34216 2
38904 38242 1
20662 21579 1
39456 43904 2
10339 35210 1
28253 11494 2
22 12016 2
...

output:

No

result:

ok answer is NO

Test #19:

score: 0
Accepted
time: 49ms
memory: 14412kb

input:

25000 300000
15694 6658 1
12187 18447 1
7540 10018 2
6657 5485 1
21747 711 1
12141 9379 1
21107 3608 1
2289 12866 1
23634 21695 2
19657 17685 2
7651 7593 1
14284 17908 1
7629 9649 1
6823 23485 1
534 24307 2
22109 6259 1
21590 6443 2
22852 13772 1
6668 21394 1
11103 4709 1
13738 22004 2
1428 11491 1
...

output:

Yes

result:

ok answer is YES

Test #20:

score: 0
Accepted
time: 57ms
memory: 14516kb

input:

25000 300000
20441 13493 1
362 263 1
2835 1312 1
22325 18409 1
4902 3889 1
11908 10885 2
20159 19508 1
22897 21371 1
803 24842 1
15205 19941 1
1454 20196 2
24937 1426 1
7432 16934 1
2006 12538 1
14316 17932 1
10790 1834 1
142 19649 2
3390 22164 2
14482 11138 1
24265 21337 1
16629 20993 1
14448 23824...

output:

No

result:

ok answer is NO

Test #21:

score: 0
Accepted
time: 48ms
memory: 46200kb

input:

200000 299998
1 2 1
1 3 1
3 2 2
1 4 1
4 2 2
1 5 1
5 2 2
1 6 1
6 2 2
1 7 1
7 2 2
1 8 1
8 2 2
1 9 1
9 2 2
1 10 1
10 2 2
1 11 1
11 2 2
1 12 1
12 2 2
1 13 1
13 2 2
1 14 1
14 2 2
1 15 1
15 2 2
1 16 1
16 2 2
1 17 1
17 2 2
1 18 1
18 2 2
1 19 1
19 2 2
1 20 1
20 2 2
1 21 1
21 2 2
1 22 1
22 2 2
1 23 1
23 2 2
...

output:

Yes

result:

ok answer is YES

Extra Test:

score: 0
Extra Test Passed