結果
| 問題 |
No.1507 Road Blocked
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-05-14 22:38:18 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 71 ms / 2,000 ms |
| コード長 | 3,695 bytes |
| コンパイル時間 | 4,431 ms |
| コンパイル使用メモリ | 256,664 KB |
| 最終ジャッジ日時 | 2025-01-21 11:41:54 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 30 |
ソースコード
#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;
}