結果

問題 No.1483 Many Graph in Namori
ユーザー 👑 PCTprobabilityPCTprobability
提出日時 2021-03-06 19:43:10
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 533 ms / 2,000 ms
コード長 4,578 bytes
コンパイル時間 5,824 ms
コンパイル使用メモリ 302,140 KB
実行使用メモリ 80,252 KB
最終ジャッジ日時 2023-08-07 20:17:01
合計ジャッジ時間 14,454 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,812 KB
testcase_01 AC 2 ms
5,820 KB
testcase_02 AC 2 ms
5,796 KB
testcase_03 AC 3 ms
5,960 KB
testcase_04 AC 2 ms
5,920 KB
testcase_05 AC 3 ms
5,924 KB
testcase_06 AC 2 ms
5,816 KB
testcase_07 AC 2 ms
5,792 KB
testcase_08 AC 2 ms
5,920 KB
testcase_09 AC 2 ms
5,780 KB
testcase_10 AC 52 ms
9,224 KB
testcase_11 AC 30 ms
7,852 KB
testcase_12 AC 40 ms
8,532 KB
testcase_13 AC 35 ms
8,116 KB
testcase_14 AC 41 ms
8,536 KB
testcase_15 AC 67 ms
10,224 KB
testcase_16 AC 30 ms
7,748 KB
testcase_17 AC 13 ms
6,648 KB
testcase_18 AC 45 ms
8,736 KB
testcase_19 AC 43 ms
8,676 KB
testcase_20 AC 31 ms
7,836 KB
testcase_21 AC 15 ms
6,844 KB
testcase_22 AC 48 ms
8,808 KB
testcase_23 AC 51 ms
9,224 KB
testcase_24 AC 29 ms
7,784 KB
testcase_25 AC 34 ms
8,180 KB
testcase_26 AC 73 ms
10,272 KB
testcase_27 AC 76 ms
10,476 KB
testcase_28 AC 18 ms
6,932 KB
testcase_29 AC 35 ms
8,184 KB
testcase_30 AC 78 ms
10,552 KB
testcase_31 AC 79 ms
10,672 KB
testcase_32 AC 77 ms
10,664 KB
testcase_33 AC 77 ms
10,564 KB
testcase_34 AC 81 ms
10,616 KB
testcase_35 AC 74 ms
12,544 KB
testcase_36 AC 86 ms
13,548 KB
testcase_37 AC 152 ms
18,880 KB
testcase_38 AC 32 ms
8,900 KB
testcase_39 AC 206 ms
23,192 KB
testcase_40 AC 23 ms
7,580 KB
testcase_41 AC 50 ms
10,072 KB
testcase_42 AC 37 ms
8,948 KB
testcase_43 AC 48 ms
9,684 KB
testcase_44 AC 52 ms
10,288 KB
testcase_45 AC 8 ms
6,552 KB
testcase_46 AC 45 ms
10,068 KB
testcase_47 AC 121 ms
16,556 KB
testcase_48 AC 132 ms
17,180 KB
testcase_49 AC 140 ms
17,412 KB
testcase_50 AC 60 ms
12,056 KB
testcase_51 AC 59 ms
12,016 KB
testcase_52 AC 57 ms
12,016 KB
testcase_53 AC 58 ms
12,012 KB
testcase_54 AC 60 ms
12,064 KB
testcase_55 AC 63 ms
12,708 KB
testcase_56 AC 533 ms
80,252 KB
testcase_57 AC 111 ms
17,236 KB
testcase_58 AC 77 ms
13,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
using ll = long long;
using ld = long double;
#define all(s) (s).begin(),(s).end()
#define rep2(i, m, n) for (int i = (m); i < (n); ++i)
#define rep(i, n) rep2(i, 0, n)
#define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i)
#define drep(i, n) drep2(i, n, 0)
#define rever(vec) reverse(vec.begin(), vec.end())
#define sor(vec) sort(vec.begin(), vec.end())
#define fi first
#define se second
#define P pair<ll,ll>
const ll mod = 998244353;
//const ll mod = 1000000007;
const ll inf = 2000000000000000000ll;
static const long double pi = 3.141592653589793;
void vcin(vector<ll> &n){for(int i=0;i<int(n.size());i++) cin>>n[i];}
void vcout(vector<ll> &n){for(int i=0;i<int(n.size());i++){cout<<n[i]<<" ";}cout<<endl;}
void YesNo(bool a){if(a){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}}
void YESNO(bool a){if(a){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}}
template<class T,class U> void chmax(T& t,const U& u){if(t<u) t=u;}
template<class T,class U> void chmin(T& t,const U& u){if(t>u) t=u;}
ll modPow(ll a, ll n, ll mod) { ll ret = 1; ll p = a % mod; while (n) { if (n & 1) ret = ret * p % mod; p = p * p % mod; n >>= 1; } return ret; }
using mint = modint998244353;
using fps = vector<mint>;
vector<ll> graph[100100];
bool seen[100100],finish[100100],roop[100100];
ll a[100100],c[100100];
fps g;
ll pos=-1,n,k,U,V,csize=0;
stack<ll> s;
map<ll,ll> m;
map<ll,mint> cyclecount;
mint ans;
mint anscycle;
mint ansno;
map<ll,fps> mf;
fps onediv(mint c) {
    int n = g.size();
  fps r=g;
  for(int i=0;i<n-1;i++){
    r[i+1]-=r[i]*c;
  }
  return r;
}
void dfs(ll v,ll p){
  seen[v]=true;
  s.push(v);
  for(auto nv:graph[v]){
    if(nv==p) continue;
    if(finish[nv]) continue;
    if(seen[nv]&&(!finish[nv])){
      pos=nv;
      return;
    }
    dfs(nv,v);
    if(pos!=-1){
      return;
    }
  }
  s.pop();
  finish[v]=true;
}
void dfs2(ll v,ll p){
  for(auto nv:graph[v]){
    if(nv==p||roop[nv]) continue;
    dfs2(nv,v);
    c[v]+=c[nv];
  }
  c[v]++;
}
void dfs3(ll v,ll p){
  for(auto nv:graph[v]){
    if(nv==p||roop[nv]) continue;
    anscycle+=(mint(k).pow(n-csize-c[nv]))*(mint(k).pow(c[nv])-1);
    dfs3(nv,v);
  }
}
void dfs4(ll v,ll p,ll r){
  for(auto nv:graph[v]){
    if(nv==p||roop[nv]) continue;
    ansno-=mint(k).pow(c[nv])-1;
    cyclecount[c[r]]+=(mint(k).pow(c[nv])-1)*mint(k).pow(c[r]-c[nv]);
    dfs4(nv,v,r);
  }
}
int main() {
  /* mod は 1e9+7 */
  ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
  cout<< fixed << setprecision(10);
  cin>>n>>k;
  ll u=k;
  assert(3<=n&&n<=100000&&2<=k&&k<=6);
  k=modPow(1-k+mod,mod-2,mod);
  for(int i=0;i<n;i++){
    ll x,y;
    cin>>x>>y;
    x--;
    y--;
    assert(0<=x&&x<n&&0<=y&&y<n&&x!=y);
    graph[x].push_back(y);
    graph[y].push_back(x);
  }
  dfs(0,-1);
  ll tmp=0;
  while(!s.empty()){
    ll t=s.top();
    s.pop();
    roop[t]=true;
    a[tmp]=t;
    tmp++;
    csize++;
    if(t==pos){
      break;
    }
  }
  for(int i=0;i<n;i++){
    if(roop[i]){
      dfs2(i,-1);
    }
  }
  //サイクルを含むグラフ
  //サイクル上の辺がカウントされる回数
  anscycle+=mint(k).pow(n-csize)*csize;
  for(int i=0;i<n;i++){
    if(roop[i]){
      dfs3(i,-1);
    }
  }
  anscycle*=mint(k).pow(csize);
  queue<vector<ll>> f;
  //g(i)を求める
  for(int i=0;i<n;i++){
    if(roop[i]){
      vector<ll> fp(2);
      fp[0]=1;
      fp[1]=modPow(k,c[i],mod)-1;
      f.push(fp);
    }
  }
  while(f.size()>1){
    auto p=f.front();
    f.pop();
    auto q=f.front();
    f.pop();
    p=convolution(p,q);
    f.push(p);
  }
  auto s=f.front();
  for(int i=0;i<int(s.size());i++){
    g.push_back(s[i]);
  }
  //g_j(i)を求める
  map<ll,bool> check;
  for(int i=0;i<n;i++){
    if(roop[i]){
      if(!check[c[i]]){
        check[c[i]]=true;
        fps t=onediv(modPow(k,c[i],mod)-1);
        mf[c[i]]=t;
      }
    }
  }
  //サイクルのないグラフについての答えを求める
  //グラフとしてあり得るものの個数
  for(int i=0;i<=csize;i++){
    ansno+=g[i]*i;
  }
  //サイクル上にある辺がカウントされる回数
  for(int i=1;i<=csize;i++){
    ansno+=g[i]*(i-1)*csize;
  }
  //サイクル上にない辺がカウントされる回数
  for(int i=0;i<n;i++){
    if(roop[i]){
      dfs4(i,-1,i);
    }
  }
  for(auto x:mf){
    for(int j=0;j<int(x.se.size());j++){
      ansno+=cyclecount[x.fi]*x.se[j]*(j+1);
    }
  }
  cout<<((anscycle)*mint(1-u).pow(n)+(ansno)*(mint(1-u).pow(n))).val()<<endl;
}
0