結果

問題 No.1507 Road Blocked
ユーザー どらら
提出日時 2021-05-14 23:23:09
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 114 ms / 2,000 ms
コード長 1,091 bytes
コンパイル時間 3,817 ms
コンパイル使用メモリ 232,900 KB
実行使用メモリ 21,984 KB
最終ジャッジ日時 2024-10-02 05:08:04
合計ジャッジ時間 8,422 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;
//using mint = modint1000000007;
using mint = modint998244353;

int N;
vector<int> G[100005];
vector<pair<ll,ll>> edges;

int dfs(int v, int p){
  int ret = 1;
  rep(i,G[v].size()){
    if(G[v][i] == p) continue;
    ll res = dfs(G[v][i], v);

    edges.emplace_back(res, N-res);

    ret += res;
  }
  
  return ret;
}
  
int main(){
  cin >> N;
  rep(i,N-1){
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  
  dfs(0,-1);

  mint ret1(0), ret2(1);
  ret2 *= N*(ll)(N-1)/2;
  ret2 *= (N-1);
  rep(i,edges.size()){
    ll x = edges[i].first;
    ll y = edges[i].second;
    
    ret1 += x*(x-1)/2 + y*(y-1)/2;
  }

  mint ret = ret1 / ret2;

  cout << ret.val() << endl;
  
  return 0;
}

0