QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#678135#9530. A Game On Treeucup-team055#AC ✓301ms17088kbC++2019.2kb2024-10-26 14:06:172024-10-26 14:06:27

Judging History

This is the latest submission verdict.

  • [2024-10-26 14:06:27]
  • Judged
  • Verdict: AC
  • Time: 301ms
  • Memory: 17088kb
  • [2024-10-26 14:06:17]
  • Submitted

answer

#line 1 "f.cpp"
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;
const ll ILL=2167167167167167167;
const int INF=2100000000;
#define rep(i,a,b) for (int i=(int)(a);i<(int)(b);i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,T b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,T b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
bool yneos(bool a,bool upp=0){if(a){cout<<(upp?"YES\n":"Yes\n");}else{cout<<(upp?"NO\n":"No\n");}return a;}
template<class T> void vec_out(vector<T> &p,int ty=0){
    if(ty==2){cout<<'{';for(int i=0;i<(int)p.size();i++){if(i){cout<<",";}cout<<'"'<<p[i]<<'"';}cout<<"}\n";}
    else{if(ty==1){cout<<p.size()<<"\n";}for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}}
template<class T> T vec_min(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmin(ans,x);return ans;}
template<class T> T vec_max(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmax(ans,x);return ans;}
template<class T> T vec_sum(vector<T> &a){T ans=T(0);for(auto &x:a) ans+=x;return ans;}
int pop_count(long long a){int res=0;while(a){res+=(a&1),a>>=1;}return res;}
template<class T> bool inside(T l,T x,T r){return l<=x&&x<r;}



#include <cassert>
#include <numeric>
#include <type_traits>

#ifdef _MSC_VER
#include <intrin.h>
#endif


#include <utility>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

// @param m `1 <= m`
// @return x mod m
constexpr long long safe_mod(long long x, long long m) {
    x %= m;
    if (x < 0) x += m;
    return x;
}

// Fast modular multiplication by barrett reduction
// Reference: https://en.wikipedia.org/wiki/Barrett_reduction
// NOTE: reconsider after Ice Lake
struct barrett {
    unsigned int _m;
    unsigned long long im;

    // @param m `1 <= m`
    explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}

    // @return m
    unsigned int umod() const { return _m; }

    // @param a `0 <= a < m`
    // @param b `0 <= b < m`
    // @return `a * b % m`
    unsigned int mul(unsigned int a, unsigned int b) const {
        // [1] m = 1
        // a = b = im = 0, so okay

        // [2] m >= 2
        // im = ceil(2^64 / m)
        // -> im * m = 2^64 + r (0 <= r < m)
        // let z = a*b = c*m + d (0 <= c, d < m)
        // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im
        // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2
        // ((ab * im) >> 64) == c or c + 1
        unsigned long long z = a;
        z *= b;
#ifdef _MSC_VER
        unsigned long long x;
        _umul128(z, im, &x);
#else
        unsigned long long x =
            (unsigned long long)(((unsigned __int128)(z)*im) >> 64);
#endif
        unsigned long long y = x * _m;
        return (unsigned int)(z - y + (z < y ? _m : 0));
    }
};

// @param n `0 <= n`
// @param m `1 <= m`
// @return `(x ** n) % m`
constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
    if (m == 1) return 0;
    unsigned int _m = (unsigned int)(m);
    unsigned long long r = 1;
    unsigned long long y = safe_mod(x, m);
    while (n) {
        if (n & 1) r = (r * y) % _m;
        y = (y * y) % _m;
        n >>= 1;
    }
    return r;
}

// Reference:
// M. Forisek and J. Jancina,
// Fast Primality Testing for Integers That Fit into a Machine Word
// @param n `0 <= n`
constexpr bool is_prime_constexpr(int n) {
    if (n <= 1) return false;
    if (n == 2 || n == 7 || n == 61) return true;
    if (n % 2 == 0) return false;
    long long d = n - 1;
    while (d % 2 == 0) d /= 2;
    constexpr long long bases[3] = {2, 7, 61};
    for (long long a : bases) {
        long long t = d;
        long long y = pow_mod_constexpr(a, t, n);
        while (t != n - 1 && y != 1 && y != n - 1) {
            y = y * y % n;
            t <<= 1;
        }
        if (y != n - 1 && t % 2 == 0) {
            return false;
        }
    }
    return true;
}
template <int n> constexpr bool is_prime = is_prime_constexpr(n);

// @param b `1 <= b`
// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g
constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
    a = safe_mod(a, b);
    if (a == 0) return {b, 0};

    // Contracts:
    // [1] s - m0 * a = 0 (mod b)
    // [2] t - m1 * a = 0 (mod b)
    // [3] s * |m1| + t * |m0| <= b
    long long s = b, t = a;
    long long m0 = 0, m1 = 1;

    while (t) {
        long long u = s / t;
        s -= t * u;
        m0 -= m1 * u;  // |m1 * u| <= |m1| * s <= b

        // [3]:
        // (s - t * u) * |m1| + t * |m0 - m1 * u|
        // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)
        // = s * |m1| + t * |m0| <= b

        auto tmp = s;
        s = t;
        t = tmp;
        tmp = m0;
        m0 = m1;
        m1 = tmp;
    }
    // by [3]: |m0| <= b/g
    // by g != b: |m0| < b/g
    if (m0 < 0) m0 += b / s;
    return {s, m0};
}

// Compile time primitive root
// @param m must be prime
// @return primitive root (and minimum in now)
constexpr int primitive_root_constexpr(int m) {
    if (m == 2) return 1;
    if (m == 167772161) return 3;
    if (m == 469762049) return 3;
    if (m == 754974721) return 11;
    if (m == 998244353) return 3;
    int divs[20] = {};
    divs[0] = 2;
    int cnt = 1;
    int x = (m - 1) / 2;
    while (x % 2 == 0) x /= 2;
    for (int i = 3; (long long)(i)*i <= x; i += 2) {
        if (x % i == 0) {
            divs[cnt++] = i;
            while (x % i == 0) {
                x /= i;
            }
        }
    }
    if (x > 1) {
        divs[cnt++] = x;
    }
    for (int g = 2;; g++) {
        bool ok = true;
        for (int i = 0; i < cnt; i++) {
            if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
                ok = false;
                break;
            }
        }
        if (ok) return g;
    }
}
template <int m> constexpr int primitive_root = primitive_root_constexpr(m);

// @param n `n < 2^32`
// @param m `1 <= m < 2^32`
// @return sum_{i=0}^{n-1} floor((ai + b) / m) (mod 2^64)
unsigned long long floor_sum_unsigned(unsigned long long n,
                                      unsigned long long m,
                                      unsigned long long a,
                                      unsigned long long b) {
    unsigned long long ans = 0;
    while (true) {
        if (a >= m) {
            ans += n * (n - 1) / 2 * (a / m);
            a %= m;
        }
        if (b >= m) {
            ans += n * (b / m);
            b %= m;
        }

        unsigned long long y_max = a * n + b;
        if (y_max < m) break;
        // y_max < m * (n + 1)
        // floor(y_max / m) <= n
        n = (unsigned long long)(y_max / m);
        b = (unsigned long long)(y_max % m);
        std::swap(m, a);
    }
    return ans;
}

}  // namespace internal

}  // namespace atcoder


#include <cassert>
#include <numeric>
#include <type_traits>

namespace atcoder {

namespace internal {

#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value ||
                                  std::is_same<T, __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int128 =
    typename std::conditional<std::is_same<T, __uint128_t>::value ||
                                  std::is_same<T, unsigned __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using make_unsigned_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value,
                              __uint128_t,
                              unsigned __int128>;

template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
                                                  is_signed_int128<T>::value ||
                                                  is_unsigned_int128<T>::value,
                                              std::true_type,
                                              std::false_type>::type;

template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
                                                 std::is_signed<T>::value) ||
                                                    is_signed_int128<T>::value,
                                                std::true_type,
                                                std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<(is_integral<T>::value &&
                               std::is_unsigned<T>::value) ||
                                  is_unsigned_int128<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<
    is_signed_int128<T>::value,
    make_unsigned_int128<T>,
    typename std::conditional<std::is_signed<T>::value,
                              std::make_unsigned<T>,
                              std::common_type<T>>::type>::type;

#else

template <class T> using is_integral = typename std::is_integral<T>;

template <class T>
using is_signed_int =
    typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<is_integral<T>::value &&
                                  std::is_unsigned<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
                                              std::make_unsigned<T>,
                                              std::common_type<T>>::type;

#endif

template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

}  // namespace internal

}  // namespace atcoder


namespace atcoder {

namespace internal {

struct modint_base {};
struct static_modint_base : modint_base {};

template <class T> using is_modint = std::is_base_of<modint_base, T>;
template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;

}  // namespace internal

template <int m, std::enable_if_t<(1 <= m)>* = nullptr>
struct static_modint : internal::static_modint_base {
    using mint = static_modint;

  public:
    static constexpr int mod() { return m; }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    static_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    static_modint(T v) {
        long long x = (long long)(v % (long long)(umod()));
        if (x < 0) x += umod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    static_modint(T v) {
        _v = (unsigned int)(v % umod());
    }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v -= rhs._v;
        if (_v >= umod()) _v += umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        unsigned long long z = _v;
        z *= rhs._v;
        _v = (unsigned int)(z % umod());
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        if (prime) {
            assert(_v);
            return pow(umod() - 2);
        } else {
            auto eg = internal::inv_gcd(_v, m);
            assert(eg.first == 1);
            return eg.second;
        }
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static constexpr unsigned int umod() { return m; }
    static constexpr bool prime = internal::is_prime<m>;
};

template <int id> struct dynamic_modint : internal::modint_base {
    using mint = dynamic_modint;

  public:
    static int mod() { return (int)(bt.umod()); }
    static void set_mod(int m) {
        assert(1 <= m);
        bt = internal::barrett(m);
    }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    dynamic_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        long long x = (long long)(v % (long long)(mod()));
        if (x < 0) x += mod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        _v = (unsigned int)(v % mod());
    }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v += mod() - rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        _v = bt.mul(_v, rhs._v);
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        auto eg = internal::inv_gcd(_v, mod());
        assert(eg.first == 1);
        return eg.second;
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static internal::barrett bt;
    static unsigned int umod() { return bt.umod(); }
};
template <int id> internal::barrett dynamic_modint<id>::bt(998244353);

using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;
using modint = dynamic_modint<-1>;

namespace internal {

template <class T>
using is_static_modint = std::is_base_of<internal::static_modint_base, T>;

template <class T>
using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;

template <class> struct is_dynamic_modint : public std::false_type {};
template <int id>
struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};

template <class T>
using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;

}  // namespace internal

}  // namespace atcoder

using mint = atcoder::modint998244353;

void solve();
// CYAN / FREDERIC
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;
    cin >> t;
    rep(i, 0, t) solve();
}

void solve(){
    ll N;
    cin >> N;
    vector<vector<int>> G(N);
    rep(i, 0, N - 1){
        int a, b;
        cin >> a >> b;
        a--, b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    vector<int> order = {0}, pare(N, -1);
    pare[0] = -2;
    rep(i, 0, N){
        int a = order[i];
        for (auto x : G[a]) if (pare[x] == -1){
            pare[x] = a;
            order.push_back(x);
        }
    }
    vector dp(N, vector<mint>(3));
    vector<ll> wei(N, 1);
    mint ans = 0;
    rep(rp, 0, N){
        int a = order[N - 1 - rp];
        dp[a][0] = 1;
        for (auto x : G[a]) if (pare[x] == a){
            vector<mint> n_dp(3);
            rep(i, 0, 3) rep(j, 0, 3) if (i + j < 3){
                n_dp[i + j] += dp[a][i] * dp[x][j];
            }
            wei[a] += wei[x];
            n_dp[2] += dp[x][1] * (mint)((N - wei[x]) * (N - wei[x]));
            swap(n_dp, dp[a]);
        }
        dp[a][1] += (mint)(wei[a] * wei[a]);
    }

    ans = dp[0][2];
    ans *= 2;
    rep(i, 1, N){
        mint tmp = (N - wei[i]) * wei[i];
        tmp *= tmp;
        ans -= tmp;
    }
    mint tmp = N * (N - 1) / 2;
    tmp *= tmp;
    ans *= tmp.inv();
    cout << ans.val() << "\n";
}
/*
 2
 3
 1 2
 2 3
 5
 1 2
 1 5
 3 2
 4 2
 */

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

詳細信息

Test #1:

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

input:

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

output:

443664158
918384806

result:

ok 2 lines

Test #2:

score: 0
Accepted
time: 2ms
memory: 3544kb

input:

1000
7
3 6
4 3
5 3
2 6
1 4
7 1
12
5 7
10 7
2 10
11 2
1 7
8 1
4 2
9 11
6 9
12 11
3 5
6
2 5
1 2
4 5
6 4
3 6
5
2 5
1 5
4 5
3 2
8
1 8
2 8
4 2
6 1
5 6
7 6
3 8
8
3 8
7 3
4 8
6 4
2 7
5 2
1 4
4
3 1
4 3
2 1
6
5 1
6 1
2 5
3 5
4 2
12
8 11
5 11
12 8
3 12
6 12
2 3
4 6
10 11
1 5
9 5
7 5
9
6 1
7 6
4 7
8 7
5 4
9 6
...

output:

948445317
468414020
550143557
918384806
711758412
487662742
776412276
869581749
240852807
765628773
211048577
887328316
890334966
940494682
760637552
908032643
592850815
584006902
908525604
221832080
433351719
56023919
867301808
183319566
698771049
366957926
449579681
599710576
310564911
286902823
3...

result:

ok 1000 lines

Test #3:

score: 0
Accepted
time: 22ms
memory: 3504kb

input:

1000
94
59 1
33 59
73 1
6 33
83 59
4 59
20 59
61 6
39 1
76 73
71 6
44 39
9 71
24 4
87 9
57 83
2 9
81 71
82 20
90 2
85 39
12 9
30 83
66 30
53 9
47 9
36 44
43 53
29 12
31 53
64 81
38 31
84 82
77 38
23 71
93 84
78 83
58 31
68 90
42 1
55 64
13 78
70 78
62 24
19 55
92 87
14 57
10 84
65 81
63 6
75 36
91 1...

output:

508107725
996793960
201633249
335988372
842755864
460619380
342223697
207523414
429241811
391691799
542977964
786416604
454278948
685531402
25914978
440729774
228518323
679471537
82764520
554190841
432505337
143444089
189106586
337234245
61954935
905141094
532919674
703954588
185671863
942858630
692...

result:

ok 1000 lines

Test #4:

score: 0
Accepted
time: 297ms
memory: 16876kb

input:

10000
8
1 4
3 1
5 1
7 3
8 4
6 8
2 7
8
2 6
4 6
5 6
8 5
7 6
3 5
1 7
8
8 5
6 5
2 5
7 2
1 6
3 1
4 8
9
8 6
9 8
3 6
1 8
5 9
2 8
4 3
7 9
8
8 6
3 6
5 8
1 6
4 3
7 6
2 6
9
9 5
7 5
2 7
8 7
4 9
3 7
6 3
1 4
8
1 4
5 1
6 5
3 4
8 4
7 8
2 5
9
1 8
6 1
2 1
3 8
5 3
9 8
7 8
4 8
9
4 9
2 9
1 2
3 4
5 2
6 9
8 3
7 2
8
1 2
8 ...

output:

49657566
56023919
387074343
97051536
701572244
211048577
711758412
308100110
761007271
711758412
178698065
285212675
80216065
43380497
267677376
818005792
53239701
765628773
970145625
387074343
436731906
422725927
479157293
977872021
436731906
925779210
487662742
705549251
267677376
711758412
526851...

result:

ok 10000 lines

Test #5:

score: 0
Accepted
time: 280ms
memory: 16908kb

input:

10000
8
7 6
8 6
5 7
4 6
1 4
2 5
3 5
10
10 7
8 7
9 8
2 8
6 7
1 7
5 9
4 1
3 6
10
2 6
3 6
5 6
7 2
1 3
4 5
8 5
9 5
10 4
10
6 5
2 5
4 6
8 5
10 5
9 5
1 8
3 6
7 1
8
5 2
3 5
6 5
1 2
8 2
4 1
7 5
9
5 1
3 1
7 5
9 7
6 3
8 6
2 1
4 9
9
9 8
4 8
3 8
6 9
2 8
7 6
1 2
5 6
9
2 5
8 5
7 8
9 7
1 8
4 8
6 9
3 1
8
6 7
8 6
2 ...

output:

711758412
286902823
691130166
841483019
650641410
887328317
331207619
733278261
56023919
977872021
414394648
183319566
239374924
696059768
855285904
761007271
711758412
86268032
599710576
728310932
178698065
178698065
422725927
219002589
178698065
202450068
599710576
56023919
449579681
760637552
925...

result:

ok 10000 lines

Test #6:

score: 0
Accepted
time: 279ms
memory: 16864kb

input:

10000
9
8 1
2 8
4 1
3 2
7 1
6 7
9 3
5 1
9
7 5
1 7
3 7
9 5
2 3
8 1
6 8
4 6
9
7 8
4 7
5 4
3 4
6 8
1 3
9 8
2 7
9
8 7
2 8
9 8
5 8
1 2
3 7
6 3
4 7
8
6 8
4 6
2 4
5 8
7 5
1 6
3 7
9
9 8
7 8
2 9
5 9
1 5
3 1
6 2
4 8
10
2 10
4 10
6 4
1 10
9 6
8 9
5 10
7 4
3 2
10
3 9
5 3
4 3
7 9
6 3
10 6
2 9
8 5
1 2
10
8 5
2 8
...

output:

211048577
354315128
178698065
705549251
285212675
138645051
449579681
286902823
925779210
294297225
519087065
368179632
422725927
603876215
539175192
867301808
977540027
669439919
211048577
701572244
977872021
138645051
267677376
855285904
977872021
286902823
925286249
705549251
219002589
331207619
...

result:

ok 10000 lines

Test #7:

score: 0
Accepted
time: 290ms
memory: 16812kb

input:

10000
8
4 2
6 2
1 6
5 1
3 1
7 5
8 5
8
4 3
8 4
6 8
2 3
5 8
1 4
7 5
9
6 1
8 6
7 8
5 6
4 1
9 6
3 1
2 5
8
3 2
5 2
7 2
8 2
1 7
4 3
6 8
10
10 2
7 10
3 7
8 7
5 10
1 5
4 3
6 4
9 7
8
5 4
8 4
2 5
7 4
6 8
1 7
3 1
9
3 1
8 1
5 1
6 5
2 6
9 5
7 5
4 2
10
1 3
6 1
2 3
7 3
8 7
9 1
10 7
5 7
4 10
10
3 2
10 3
5 10
9 3
1 ...

output:

422725927
977872021
867301808
407446676
466833287
387074343
97051536
292325385
301691628
765628773
285212675
711758412
650641410
178698065
543242114
286902823
473241769
109930120
841975980
836553418
422725927
286902823
414394648
739440264
436731906
56023919
436731906
530918109
603876215
977872021
40...

result:

ok 10000 lines

Test #8:

score: 0
Accepted
time: 283ms
memory: 17056kb

input:

10000
9
9 3
7 9
2 3
5 2
6 2
1 9
4 5
8 9
10
4 6
9 4
8 6
5 4
10 5
7 8
2 8
3 7
1 2
10
5 4
1 4
3 4
9 3
6 3
7 9
8 7
2 4
10 7
10
3 8
4 3
10 8
6 10
9 4
5 6
2 8
1 2
7 5
10
10 9
4 9
6 10
5 10
2 6
1 10
3 1
8 3
7 10
10
9 7
10 7
2 7
3 10
1 3
4 3
8 9
6 3
5 6
9
2 4
7 4
5 7
9 5
3 5
6 7
8 7
1 2
9
2 4
1 4
9 2
7 2
8 ...

output:

409773147
306621231
836553418
760637552
519087065
304649390
97051536
742521264
387074343
855285904
874737082
358875008
733278261
698524570
908525604
387074343
970145625
449579681
286902823
239374924
650641410
691130166
765628773
603876215
839572800
977872021
742521264
908032643
874737082
299719788
7...

result:

ok 10000 lines

Test #9:

score: 0
Accepted
time: 294ms
memory: 17088kb

input:

10000
9
8 4
5 4
6 4
3 5
7 4
9 6
1 9
2 9
10
3 9
10 3
2 10
6 2
1 6
8 2
4 1
5 3
7 9
9
9 3
7 9
4 3
5 3
2 3
6 5
1 6
8 9
8
6 1
8 1
5 6
7 6
4 8
3 6
2 7
8
2 3
7 2
8 2
6 8
5 2
1 3
4 2
10
2 7
8 7
9 2
5 8
10 8
3 7
1 7
4 8
6 7
9
3 8
4 3
5 3
2 8
9 2
1 5
6 8
7 4
9
5 1
6 5
4 6
9 6
3 4
2 9
8 1
7 4
8
2 4
8 4
3 2
5 3...

output:

331207619
28098733
97051536
599710576
701572244
277043619
368179632
138645051
711758412
626059423
86268032
414394648
368179632
993314752
321410036
530918109
711758412
712327454
603876215
49657566
705549251
765628773
56023919
299719788
887328316
839572800
650641410
211048577
286902823
908032643
28690...

result:

ok 10000 lines

Test #10:

score: 0
Accepted
time: 293ms
memory: 16908kb

input:

10000
10
4 9
7 4
2 4
5 4
6 7
10 2
3 9
8 10
1 8
8
2 4
3 2
5 2
6 4
1 4
8 3
7 1
8
8 7
6 7
4 8
5 4
1 6
2 5
3 4
8
7 2
3 2
5 3
1 5
8 1
4 7
6 2
9
5 9
6 9
4 6
7 9
8 4
3 6
1 6
2 5
10
7 1
4 7
2 7
5 2
8 5
3 2
6 7
10 4
9 2
8
6 2
3 6
7 2
8 3
5 6
4 5
1 2
10
2 8
5 8
10 8
4 10
7 8
3 2
1 10
6 4
9 10
10
3 5
6 5
10 5
...

output:

440213438
977872021
285212675
285212675
705549251
267677376
436731906
267677376
440213438
712327454
711758412
191268549
321410036
436731906
839572800
49657566
519087065
178698065
977872021
285212675
574298605
368179632
466833287
696059768
86268033
308100110
487662742
887328317
977872021
701572244
99...

result:

ok 10000 lines

Test #11:

score: 0
Accepted
time: 301ms
memory: 16572kb

input:

10000
8
1 3
8 3
2 3
6 8
5 8
4 1
7 1
10
5 7
10 7
2 5
4 5
8 10
6 7
1 5
3 2
9 8
9
4 7
2 4
1 4
5 4
9 1
3 2
6 4
8 9
8
5 7
3 5
2 5
6 5
4 7
8 3
1 7
9
1 5
9 5
3 1
7 3
8 3
6 3
4 1
2 8
8
1 2
4 2
6 2
7 2
8 7
3 8
5 6
9
4 5
3 4
6 4
7 3
1 5
9 3
2 1
8 7
9
1 6
3 1
2 3
5 2
4 3
8 5
9 5
7 1
8
5 3
7 3
4 3
8 7
1 3
6 1
2...

output:

202450068
449579681
742521264
56023919
705549251
599710576
765628773
887328316
599710576
97051536
286902823
603876215
321410036
221832080
294297225
479157293
650641410
765628773
908525604
285212675
125704848
414394648
599254713
286902823
707938599
13864507
599710576
304649390
691130166
56023919
7656...

result:

ok 10000 lines

Test #12:

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

input:

1
2
1 2

output:

1

result:

ok single line: '1'

Extra Test:

score: 0
Extra Test Passed