結果

問題 No.1507 Road Blocked
ユーザー auauaauaua
提出日時 2021-05-14 22:31:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 86 ms / 2,000 ms
コード長 1,935 bytes
コンパイル時間 1,741 ms
コンパイル使用メモリ 170,044 KB
実行使用メモリ 15,104 KB
最終ジャッジ日時 2024-04-10 01:04:46
合計ジャッジ時間 5,395 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 72 ms
15,104 KB
testcase_04 AC 83 ms
9,216 KB
testcase_05 AC 80 ms
9,472 KB
testcase_06 AC 81 ms
9,216 KB
testcase_07 AC 80 ms
9,184 KB
testcase_08 AC 80 ms
9,344 KB
testcase_09 AC 81 ms
9,216 KB
testcase_10 AC 81 ms
9,216 KB
testcase_11 AC 79 ms
9,164 KB
testcase_12 AC 80 ms
9,216 KB
testcase_13 AC 80 ms
9,344 KB
testcase_14 AC 79 ms
9,200 KB
testcase_15 AC 82 ms
9,344 KB
testcase_16 AC 77 ms
9,216 KB
testcase_17 AC 82 ms
9,216 KB
testcase_18 AC 80 ms
9,396 KB
testcase_19 AC 86 ms
9,216 KB
testcase_20 AC 77 ms
9,344 KB
testcase_21 AC 79 ms
9,172 KB
testcase_22 AC 79 ms
9,168 KB
testcase_23 AC 82 ms
9,216 KB
testcase_24 AC 81 ms
9,344 KB
testcase_25 AC 79 ms
9,192 KB
testcase_26 AC 83 ms
9,344 KB
testcase_27 AC 82 ms
9,216 KB
testcase_28 AC 82 ms
9,216 KB
testcase_29 AC 81 ms
9,216 KB
testcase_30 AC 82 ms
9,216 KB
testcase_31 AC 84 ms
9,344 KB
testcase_32 AC 84 ms
9,188 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
#define vec vector<ll>
#define mat vector<vector<ll> >
#define fi first
#define se second
#define double long double
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
//typedef long double ld;
typedef complex<double> Complex;
const ll INF=1e9+7;
const ll MOD=998244353;
const ll inf=INF*INF;
const ll mod=MOD;
const ll MAX=200010;
ll n;
ll ans=0;
//Combination
long long extGCD(long long a,long long mod,long long &x,long long &y){
    if(mod==0){
        x=1;
        y=0;
        return a;
    }
    long long d=extGCD(mod,a%mod,y,x);
    y-=(a/mod)*x;
    return d;
}
long long invmod(long long a,long long mod){
    long long x,y;
    extGCD(a,mod,x,y);
    x%=mod;
    if(x<0)x+=mod;
    return x;
}
ll fac[MAX],finv[MAX],inv[MAX];
void combinit(){
    fac[0]=fac[1]=1;
    finv[0]=finv[1]=1;
    inv[1]=1;
    REP(i,2,MAX){
        fac[i]=fac[i-1]*i%mod;
        inv[i]=invmod(i,mod);
        finv[i]=finv[i-1]*inv[i]%mod;
    }
}
ll comb(ll n,ll k){
    if(n<k)return 0;
    if(n<0||k<0)return 0;
    return fac[n]*(finv[k]*finv[n-k]%mod)%mod;
}
ll dfs(ll u,ll p,vector<vector<ll> >&G){
    ll res=1;
    for(auto e:G[u]){
        if(e==p)continue;
        ll x=dfs(e,u,G);
        ans+=x*(x-1)/2+(n-x)*(n-x-1)/2;
        ans%=mod;
        res+=x;
    }
    return res;
}

signed main(){
    cin>>n;
    vector<vector<ll> >G(n);
    rep(i,n-1){
        ll u,v;cin>>u>>v;
        u--;v--;
        G[u].pb(v);
        G[v].pb(u);
    }
    ll st=0;
    rep(i,n){
        if(G[i].size()==1)st=i;
    }
    dfs(st,-1,G);
    ll b=n*(n-1)/2%mod*(n-1)%mod;
    ans=ans*invmod(b,mod)%mod;
    if(ans<0)ans+=mod;
    cout<<ans<<endl;
}
0