結果

問題 No.1483 Many Graph in Namori
ユーザー 👑 PCTprobabilityPCTprobability
提出日時 2021-03-06 19:43:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 497 ms / 2,000 ms
コード長 4,578 bytes
コンパイル時間 5,929 ms
コンパイル使用メモリ 293,440 KB
実行使用メモリ 81,348 KB
最終ジャッジ日時 2024-04-25 14:42:33
合計ジャッジ時間 11,954 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,812 KB
testcase_01 AC 3 ms
6,816 KB
testcase_02 AC 3 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 3 ms
7,240 KB
testcase_05 AC 3 ms
6,940 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 3 ms
6,944 KB
testcase_10 AC 51 ms
9,928 KB
testcase_11 AC 29 ms
7,928 KB
testcase_12 AC 40 ms
8,648 KB
testcase_13 AC 36 ms
8,308 KB
testcase_14 AC 41 ms
8,644 KB
testcase_15 AC 66 ms
10,436 KB
testcase_16 AC 31 ms
8,000 KB
testcase_17 AC 13 ms
7,108 KB
testcase_18 AC 45 ms
9,072 KB
testcase_19 AC 44 ms
9,664 KB
testcase_20 AC 30 ms
7,884 KB
testcase_21 AC 15 ms
8,008 KB
testcase_22 AC 47 ms
9,160 KB
testcase_23 AC 52 ms
9,544 KB
testcase_24 AC 31 ms
8,000 KB
testcase_25 AC 34 ms
9,412 KB
testcase_26 AC 72 ms
10,568 KB
testcase_27 AC 75 ms
10,824 KB
testcase_28 AC 17 ms
7,028 KB
testcase_29 AC 35 ms
9,028 KB
testcase_30 AC 73 ms
11,328 KB
testcase_31 AC 76 ms
11,460 KB
testcase_32 AC 75 ms
11,072 KB
testcase_33 AC 74 ms
11,204 KB
testcase_34 AC 74 ms
11,720 KB
testcase_35 AC 69 ms
13,252 KB
testcase_36 AC 84 ms
14,660 KB
testcase_37 AC 150 ms
20,680 KB
testcase_38 AC 30 ms
9,664 KB
testcase_39 AC 201 ms
24,260 KB
testcase_40 AC 23 ms
8,092 KB
testcase_41 AC 50 ms
10,624 KB
testcase_42 AC 36 ms
9,136 KB
testcase_43 AC 47 ms
10,880 KB
testcase_44 AC 53 ms
10,644 KB
testcase_45 AC 8 ms
6,984 KB
testcase_46 AC 44 ms
11,844 KB
testcase_47 AC 121 ms
17,608 KB
testcase_48 AC 127 ms
18,492 KB
testcase_49 AC 148 ms
18,500 KB
testcase_50 AC 62 ms
12,612 KB
testcase_51 AC 62 ms
12,228 KB
testcase_52 AC 61 ms
12,864 KB
testcase_53 AC 64 ms
12,036 KB
testcase_54 AC 61 ms
12,740 KB
testcase_55 AC 64 ms
13,640 KB
testcase_56 AC 497 ms
81,348 KB
testcase_57 AC 108 ms
19,012 KB
testcase_58 AC 78 ms
13,892 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