結果

問題 No.1483 Many Graph in Namori
ユーザー 👑 PCTprobabilityPCTprobability
提出日時 2021-04-15 22:23:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 616 ms / 2,000 ms
コード長 5,419 bytes
コンパイル時間 8,684 ms
コンパイル使用メモリ 309,812 KB
実行使用メモリ 94,252 KB
最終ジャッジ日時 2023-09-15 00:24:09
合計ジャッジ時間 20,558 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,780 KB
testcase_01 AC 2 ms
5,788 KB
testcase_02 AC 3 ms
5,864 KB
testcase_03 AC 3 ms
5,824 KB
testcase_04 AC 2 ms
5,796 KB
testcase_05 AC 2 ms
5,788 KB
testcase_06 AC 2 ms
5,780 KB
testcase_07 AC 3 ms
5,844 KB
testcase_08 AC 2 ms
5,892 KB
testcase_09 AC 3 ms
5,776 KB
testcase_10 AC 202 ms
19,144 KB
testcase_11 AC 96 ms
13,236 KB
testcase_12 AC 139 ms
15,796 KB
testcase_13 AC 121 ms
14,472 KB
testcase_14 AC 146 ms
16,204 KB
testcase_15 AC 276 ms
22,124 KB
testcase_16 AC 89 ms
13,308 KB
testcase_17 AC 33 ms
8,924 KB
testcase_18 AC 169 ms
17,160 KB
testcase_19 AC 158 ms
16,884 KB
testcase_20 AC 94 ms
13,308 KB
testcase_21 AC 38 ms
9,380 KB
testcase_22 AC 171 ms
17,348 KB
testcase_23 AC 191 ms
18,412 KB
testcase_24 AC 95 ms
13,240 KB
testcase_25 AC 101 ms
13,716 KB
testcase_26 AC 295 ms
22,840 KB
testcase_27 AC 324 ms
23,672 KB
testcase_28 AC 45 ms
9,832 KB
testcase_29 AC 117 ms
14,536 KB
testcase_30 AC 328 ms
24,208 KB
testcase_31 AC 336 ms
24,160 KB
testcase_32 AC 347 ms
24,224 KB
testcase_33 AC 364 ms
24,164 KB
testcase_34 AC 349 ms
24,288 KB
testcase_35 AC 147 ms
17,804 KB
testcase_36 AC 175 ms
19,820 KB
testcase_37 AC 353 ms
29,300 KB
testcase_38 AC 52 ms
10,992 KB
testcase_39 AC 483 ms
36,304 KB
testcase_40 AC 83 ms
13,628 KB
testcase_41 AC 239 ms
23,296 KB
testcase_42 AC 167 ms
19,096 KB
testcase_43 AC 216 ms
22,496 KB
testcase_44 AC 255 ms
24,516 KB
testcase_45 AC 13 ms
7,344 KB
testcase_46 AC 103 ms
15,260 KB
testcase_47 AC 369 ms
28,908 KB
testcase_48 AC 400 ms
30,804 KB
testcase_49 AC 421 ms
31,616 KB
testcase_50 AC 313 ms
25,860 KB
testcase_51 AC 304 ms
25,748 KB
testcase_52 AC 305 ms
25,872 KB
testcase_53 AC 319 ms
25,812 KB
testcase_54 AC 319 ms
25,856 KB
testcase_55 AC 320 ms
26,320 KB
testcase_56 AC 616 ms
94,252 KB
testcase_57 AC 200 ms
31,120 KB
testcase_58 AC 184 ms
26,964 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);
  }
}
class UnionFind{
public:
  vector<ll> par;
  vector<ll> siz;
  UnionFind(ll sz_):par(sz_),siz(sz_,1ll){
    for(int i=0;i<sz_;i++) par[i]=i;
  }
  void init(ll sz_){
    par.resize(sz_);
    siz.assign(sz_,1ll);
    for(int i=0;i<sz_;i++) par[i]=i;
  }
  ll root(ll x){
    while(par[x]!=x){
      x=par[x]=par[par[x]];
    }
    return x;
  }
  bool merge(ll x,ll y){
    x=root(x);
    y=root(y);
    if(x==y) return false;
    if(siz[x]<siz[y]) swap(x,y);
    siz[x]+=siz[y];
    par[y]=x;
    return true;
  }
  bool issame(ll x,ll y){
    return root(x)==root(y);
  }
  ll size(ll x){
    return siz[root(x)];
  }
};
int main() {
  /* mod は 1e9+7 */
  ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
  cout<< fixed << setprecision(10);
  map<pair<ll,ll>,ll> starti;
  cin>>n>>k;
  ll u=k;
  assert(3<=n&&n<=100000&&2<=k&&k<=6);
  k=modPow(1-k+mod,mod-2,mod);
  UnionFind T(n);
  for(int i=0;i<n;i++){
    ll x,y;
    cin>>x>>y;
    x--;
    y--;
    if(starti[{x,y}]!=0){
      cout<<"NO"<<endl;
    }
    starti[{x,y}]++;
    starti[{y,x}]++;
    assert(0<=x&&x<n&&0<=y&&y<n&&x!=y);
    graph[x].push_back(y);
    graph[y].push_back(x);
    T.merge(x,y);
  }
  for(int i=1;i<n;i++){
    assert(T.issame(0,i));
  }
  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