結果

問題 No.1483 Many Graph in Namori
ユーザー 👑 PCTprobabilityPCTprobability
提出日時 2021-03-07 16:08:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 570 ms / 2,000 ms
コード長 5,419 bytes
コンパイル時間 6,239 ms
コンパイル使用メモリ 302,104 KB
実行使用メモリ 95,684 KB
最終ジャッジ日時 2024-04-25 14:44:12
合計ジャッジ時間 16,495 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,816 KB
testcase_01 AC 3 ms
6,816 KB
testcase_02 AC 3 ms
6,948 KB
testcase_03 AC 3 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 3 ms
6,944 KB
testcase_06 AC 3 ms
7,364 KB
testcase_07 AC 3 ms
6,940 KB
testcase_08 AC 3 ms
6,944 KB
testcase_09 AC 2 ms
6,948 KB
testcase_10 AC 154 ms
19,272 KB
testcase_11 AC 82 ms
13,896 KB
testcase_12 AC 116 ms
16,708 KB
testcase_13 AC 103 ms
15,944 KB
testcase_14 AC 129 ms
17,224 KB
testcase_15 AC 241 ms
22,388 KB
testcase_16 AC 77 ms
14,532 KB
testcase_17 AC 31 ms
10,088 KB
testcase_18 AC 143 ms
17,480 KB
testcase_19 AC 138 ms
16,864 KB
testcase_20 AC 76 ms
14,148 KB
testcase_21 AC 35 ms
10,564 KB
testcase_22 AC 143 ms
17,860 KB
testcase_23 AC 158 ms
19,400 KB
testcase_24 AC 78 ms
14,020 KB
testcase_25 AC 87 ms
15,424 KB
testcase_26 AC 264 ms
23,876 KB
testcase_27 AC 273 ms
24,772 KB
testcase_28 AC 41 ms
10,112 KB
testcase_29 AC 100 ms
15,044 KB
testcase_30 AC 272 ms
25,284 KB
testcase_31 AC 279 ms
24,644 KB
testcase_32 AC 253 ms
25,156 KB
testcase_33 AC 228 ms
24,488 KB
testcase_34 AC 241 ms
24,772 KB
testcase_35 AC 119 ms
19,140 KB
testcase_36 AC 141 ms
21,572 KB
testcase_37 AC 267 ms
30,912 KB
testcase_38 AC 47 ms
12,628 KB
testcase_39 AC 374 ms
38,384 KB
testcase_40 AC 62 ms
14,204 KB
testcase_41 AC 167 ms
24,380 KB
testcase_42 AC 118 ms
20,444 KB
testcase_43 AC 165 ms
22,868 KB
testcase_44 AC 185 ms
24,628 KB
testcase_45 AC 12 ms
7,752 KB
testcase_46 AC 86 ms
15,312 KB
testcase_47 AC 269 ms
29,996 KB
testcase_48 AC 299 ms
31,816 KB
testcase_49 AC 302 ms
32,584 KB
testcase_50 AC 217 ms
26,048 KB
testcase_51 AC 225 ms
26,824 KB
testcase_52 AC 226 ms
26,184 KB
testcase_53 AC 221 ms
26,004 KB
testcase_54 AC 215 ms
26,048 KB
testcase_55 AC 233 ms
26,568 KB
testcase_56 AC 570 ms
95,684 KB
testcase_57 AC 182 ms
32,580 KB
testcase_58 AC 158 ms
27,460 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