結果

問題 No.1507 Road Blocked
ユーザー MiMi
提出日時 2021-05-14 22:38:18
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 72 ms / 2,000 ms
コード長 3,695 bytes
コンパイル時間 4,571 ms
コンパイル使用メモリ 259,892 KB
実行使用メモリ 18,120 KB
最終ジャッジ日時 2024-04-10 01:21:59
合計ジャッジ時間 7,946 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 41 ms
18,120 KB
testcase_04 AC 72 ms
10,496 KB
testcase_05 AC 69 ms
10,368 KB
testcase_06 AC 65 ms
10,496 KB
testcase_07 AC 71 ms
10,348 KB
testcase_08 AC 69 ms
10,368 KB
testcase_09 AC 68 ms
10,496 KB
testcase_10 AC 61 ms
10,496 KB
testcase_11 AC 67 ms
10,364 KB
testcase_12 AC 59 ms
10,368 KB
testcase_13 AC 57 ms
10,368 KB
testcase_14 AC 62 ms
10,368 KB
testcase_15 AC 70 ms
10,496 KB
testcase_16 AC 62 ms
10,496 KB
testcase_17 AC 65 ms
10,368 KB
testcase_18 AC 63 ms
10,496 KB
testcase_19 AC 64 ms
10,368 KB
testcase_20 AC 68 ms
10,368 KB
testcase_21 AC 62 ms
10,368 KB
testcase_22 AC 60 ms
10,368 KB
testcase_23 AC 69 ms
10,528 KB
testcase_24 AC 64 ms
10,344 KB
testcase_25 AC 59 ms
10,364 KB
testcase_26 AC 63 ms
10,368 KB
testcase_27 AC 65 ms
10,368 KB
testcase_28 AC 62 ms
10,368 KB
testcase_29 AC 70 ms
10,496 KB
testcase_30 AC 67 ms
10,496 KB
testcase_31 AC 68 ms
10,496 KB
testcase_32 AC 58 ms
10,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> P;
typedef pair<int,int> Pi;
#define rep(i,n) for(ll i=0;i<n;i++)
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define rrep(i,n) for(ll i=n-1;i>=0;i--)
#define rFOR(i,a,b) for(ll i=a-1;i>=b;i--)
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define endl "\n"

istream &operator>>(istream &is, modint &a) { long long v; is >> v; a = v; return is; }
ostream &operator<<(ostream &os, const modint &a) { return os << a.val(); }
istream &operator>>(istream &is, modint998244353 &a) { long long v; is >> v; a = v; return is; }
ostream &operator<<(ostream &os, const modint998244353 &a) { return os << a.val(); }
istream &operator>>(istream &is, modint1000000007 &a) { long long v; is >> v; a = v; return is; }
ostream &operator<<(ostream &os, const modint1000000007 &a) { return os << a.val(); }

template<typename T> inline bool chmax(T &a, T b){if(a<b){a=b;return true;}return false;}
template<typename T> inline bool chmin(T &a, T b){if(a>b){a=b;return true;}return false;}
template<typename T> ostream& operator<<(ostream& s,const complex<T>& d) {return s<<"("<<d.real()<<", "<<d.imag()<< ")";}
template<typename T1, typename T2> ostream& operator<<(ostream& s,const pair<T1,T2>& d) {return s<<"("<<d.first<<", "<<d.second<<")";}
template<typename T> ostream& operator<<(ostream& s, const vector<T>& d){int len=d.size();rep(i,len){s<<d[i];if(i<len-1) s<<" ";}return s;}
template<typename T> ostream& operator<<(ostream& s,const vector<vector<T>>& d){int len=d.size();rep(i,len){s<<d[i]<<endl;}return s;}
template<typename T> ostream& operator<<(ostream& s,const set<T>& v){s<<"{ ";for(auto itr=v.begin();itr!=v.end();++itr) {if (itr!=v.begin()) {s<< ", ";}s<<(*itr);}s<<" }";return s;}
template<typename T> ostream& operator<<(ostream& s,const multiset<T>& v){s<<"{ ";for(auto itr=v.begin();itr!=v.end();++itr) {if (itr!=v.begin()) {s<< ", ";}s<<(*itr);}s<<" }";return s;}
template<typename T1, typename T2> ostream& operator<<(ostream& s,const map<T1,T2>& m){s<<"{"<<endl;for(auto itr=m.begin();itr!=m.end();++itr){s<<" "<<(*itr).first<<" : "<<(*itr).second<<endl;}s<<"}"<<endl;return s;}
template<class T> vector<T> make_vec(size_t n, T a) { return vector<T>(n, a); }
template<class... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec(ts...))>(n, make_vec(ts...)); }

const ll mod=1'000'000'007;
const ll inf=1'000'000'000'000'000'00;
const int INF=1'000'000'000;
const double EPS=1e-10;
const double PI=acos(-1);

using mint=modint998244353;

struct combination{
    vector<mint> fact,ifact;
    combination(int n):fact(n+1),ifact(n+1){
        assert(n<mod);
        fact[0]=1;
        for(int i=1;i<=n;i++) fact[i]=fact[i-1]*i;
        ifact[n]=fact[n].inv();
        for(int i=n;i>=1;i--) ifact[i-1]=ifact[i]*i;
    }
    mint operator()(int n,int k){
        if (k<0 || k>n) return 0;
        return fact[n]*ifact[k]*ifact[n-k];
    }
};

int main(){
    cin.tie(0);ios::sync_with_stdio(false);
    int n;
    cin>>n;
    vector<vector<int>> g(n);
    rep(i,n-1){
        int u,v;
        cin>>u>>v;
        u--,v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    mint ans=0;
    vector<ll> dp(n);
    combination comb(n+1);
    auto dfs=[&](auto &&dfs,int v,int pre)->void{
        for(auto &&to:g[v]){
            if(to==pre) continue;
            dfs(dfs,to,v);
            dp[v]+=dp[to];
            ans+=comb(dp[to],2);
            ans+=comb(n-dp[to],2);
        }
        dp[v]++;
    };
    dfs(dfs,0,-1);
    ans/=(n-1);
    ans/=comb(n,2);
    cout<<ans<<endl;
}
0