結果

問題 No.1483 Many Graph in Namori
ユーザー beetbeet
提出日時 2021-03-15 17:44:59
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 271 ms / 2,000 ms
コード長 4,829 bytes
コンパイル時間 3,109 ms
コンパイル使用メモリ 230,608 KB
実行使用メモリ 24,228 KB
最終ジャッジ日時 2024-04-25 14:45:00
合計ジャッジ時間 12,485 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 174 ms
15,616 KB
testcase_11 AC 82 ms
10,496 KB
testcase_12 AC 115 ms
12,928 KB
testcase_13 AC 97 ms
11,776 KB
testcase_14 AC 125 ms
13,164 KB
testcase_15 AC 218 ms
18,700 KB
testcase_16 AC 79 ms
10,496 KB
testcase_17 AC 26 ms
6,944 KB
testcase_18 AC 142 ms
14,080 KB
testcase_19 AC 136 ms
13,696 KB
testcase_20 AC 76 ms
10,624 KB
testcase_21 AC 31 ms
6,944 KB
testcase_22 AC 146 ms
14,336 KB
testcase_23 AC 160 ms
15,184 KB
testcase_24 AC 76 ms
10,368 KB
testcase_25 AC 81 ms
10,880 KB
testcase_26 AC 231 ms
19,408 KB
testcase_27 AC 269 ms
20,052 KB
testcase_28 AC 34 ms
7,296 KB
testcase_29 AC 95 ms
11,540 KB
testcase_30 AC 262 ms
20,640 KB
testcase_31 AC 263 ms
20,648 KB
testcase_32 AC 271 ms
20,796 KB
testcase_33 AC 269 ms
20,828 KB
testcase_34 AC 251 ms
20,612 KB
testcase_35 AC 73 ms
10,112 KB
testcase_36 AC 87 ms
11,136 KB
testcase_37 AC 165 ms
16,512 KB
testcase_38 AC 30 ms
6,940 KB
testcase_39 AC 212 ms
20,348 KB
testcase_40 AC 69 ms
10,880 KB
testcase_41 AC 179 ms
19,704 KB
testcase_42 AC 130 ms
15,872 KB
testcase_43 AC 170 ms
18,800 KB
testcase_44 AC 196 ms
20,640 KB
testcase_45 AC 8 ms
6,944 KB
testcase_46 AC 63 ms
9,856 KB
testcase_47 AC 211 ms
19,364 KB
testcase_48 AC 223 ms
20,392 KB
testcase_49 AC 242 ms
20,768 KB
testcase_50 AC 195 ms
21,016 KB
testcase_51 AC 184 ms
21,044 KB
testcase_52 AC 184 ms
20,292 KB
testcase_53 AC 174 ms
21,016 KB
testcase_54 AC 175 ms
21,184 KB
testcase_55 AC 183 ms
21,012 KB
testcase_56 AC 130 ms
19,780 KB
testcase_57 AC 102 ms
19,880 KB
testcase_58 AC 139 ms
24,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using Int = long long;
const char newl = '\n';

template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}
template<typename T=int>
vector<T> read(size_t n){
  vector<T> ts(n);
  for(size_t i=0;i<n;i++) cin>>ts[i];
  return ts;
}


template<typename T, T MOD = 1000000007>
struct Mint{
  static constexpr T mod = MOD;
  T v;
  Mint():v(0){}
  Mint(signed v):v(v){}
  Mint(long long t){v=t%MOD;if(v<0) v+=MOD;}

  Mint pow(long long k){
    Mint res(1),tmp(v);
    while(k){
      if(k&1) res*=tmp;
      tmp*=tmp;
      k>>=1;
    }
    return res;
  }

  static Mint add_identity(){return Mint(0);}
  static Mint mul_identity(){return Mint(1);}

  Mint inv(){return pow(MOD-2);}

  Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;}
  Mint& operator/=(Mint a){return (*this)*=a.inv();}

  Mint operator+(Mint a) const{return Mint(v)+=a;}
  Mint operator-(Mint a) const{return Mint(v)-=a;}
  Mint operator*(Mint a) const{return Mint(v)*=a;}
  Mint operator/(Mint a) const{return Mint(v)/=a;}

  Mint operator-() const{return v?Mint(MOD-v):Mint(v);}

  bool operator==(const Mint a)const{return v==a.v;}
  bool operator!=(const Mint a)const{return v!=a.v;}
  bool operator <(const Mint a)const{return v <a.v;}

  static Mint comb(long long n,int k){
    Mint num(1),dom(1);
    for(int i=0;i<k;i++){
      num*=Mint(n-i);
      dom*=Mint(i+1);
    }
    return num/dom;
  }
};
template<typename T, T MOD> constexpr T Mint<T, MOD>::mod;
template<typename T, T MOD>
ostream& operator<<(ostream &os,Mint<T, MOD> m){os<<m.v;return os;}


template<typename F>
struct FixPoint : F{
  FixPoint(F&& f):F(forward<F>(f)){}
  template<typename... Args>
  decltype(auto) operator()(Args&&... args) const{
    return F::operator()(*this,forward<Args>(args)...);
  }
};
template<typename F>
inline decltype(auto) MFP(F&& f){
  return FixPoint<F>{forward<F>(f)};
}

//INSERT ABOVE HERE
signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n,k;
  cin>>n>>k;

  vector<set<int>> S(n);
  for(int i=0;i<n;i++){
    int a,b;
    cin>>a>>b;
    a--;b--;
    S[a].emplace(b);
    S[b].emplace(a);
  }

  vector<vector<int>> T(n);
  queue<int> que;
  for(int i=0;i<n;i++)
    if(S[i].size()==1)
      que.emplace(i);

  while(!que.empty()){
    int v=que.front();que.pop();
    int p=*S[v].begin();
    T[p].emplace_back(v);
    S[p].erase(v);
    S[v].erase(p);
    if(S[p].size()==1)
      que.emplace(p);
  }

  vector<int> in_loop=[&](){
    int pos=-1;
    for(int i=0;i<n;i++)
      if(S[i].size()>0) pos=i;
    assert(~pos);

    vector<int> res;
    while(not S[pos].empty()){
      res.emplace_back(pos);
      int nxt=*S[pos].begin();
      S[pos].erase(nxt);
      S[nxt].erase(pos);
      pos=nxt;
    }
    return res;
  }();

  using M = Mint<int, 998244353>;
  M ans{0};

  vector<M> dp(n);
  vector<int> sz(n);

  M inv=(M(1)-M(k)).inv();
  // not use loop
  for(int r:in_loop){
    MFP([&](auto dfs,int v)->void{
      sz[v]=1;
      for(int u:T[v]){
        dfs(u);
        sz[v]+=sz[u];
      }

      dp[v]=inv.pow(sz[v])-M(1);
      for(int u:T[v])
        dp[v]+=dp[u]*inv.pow(sz[v]-sz[u]);

      // see from descents
      for(int u:T[v])
        ans+=(inv.pow(sz[v]-sz[u])-M(1))*dp[u];

      // see from v
      M zero=1;
      M one=0;
      M all=inv.pow(sz[v]-1);
      for(int u:T[v])
        one+=inv.pow(sz[u])-M(1);

      ans+=M(k)*inv*zero;
      ans+=M(k)*inv*one;
      ans+=inv*(all-(zero+one));
    })(r);
  }

  auto calc=MFP([&](auto dfs,int r,int v)->M{
    M res=inv.pow(sz[v])-M(1);
    if(v==r) res+=M(1);
    for(int u:T[v]) res+=dfs(r,u)*inv.pow(sz[v]-sz[u]);
    return res;
  });

  // use all edges in loop
  for(int r:in_loop)
    ans+=calc(r,r)*inv.pow(n-sz[r]);

  // use partial edges in loop
  // prepare
  int num=0;
  M sum{0};
  M uku{0};
  auto proceed=[&](int r){
    sum*=inv.pow(sz[r]);
    uku*=inv.pow(sz[r]);
    sum+=calc(r,r)*inv.pow(num+n);
    uku-=M(1);
    num+=sz[r];
  };
  for(int r:in_loop) proceed(r);
  assert(num==n);

  // add
  for(int r:in_loop){
    // see from r's subtree
    ans+=MFP([&](auto dfs,int v)->M{ // OK
      M res=inv.pow(sz[v])-M(1);
      for(int u:T[v])
        res+=dfs(u)*inv.pow(sz[v]-sz[u]);
      return res;
    })(r)*(inv.pow(n-sz[r])-M(1));

    // see from left
    sum-=calc(r,r)*inv.pow(num+n-n)*inv.pow(n-sz[r]);
    uku+=M(1)*inv.pow(n-sz[r]);
    ans+=(sum/inv.pow(num+sz[r])+uku)*(inv.pow(sz[r])-M(1));
    proceed(r);
  }

  ans/=inv.pow(n);
  cout<<ans<<newl;
  return 0;
}
0