結果

問題 No.2588 Increasing Record
ユーザー 👑 potato167potato167
提出日時 2023-12-16 03:37:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 362 ms / 3,000 ms
コード長 1,548 bytes
コンパイル時間 2,507 ms
コンパイル使用メモリ 220,180 KB
実行使用メモリ 45,576 KB
最終ジャッジ日時 2023-12-16 03:37:46
合計ジャッジ時間 13,187 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 93 ms
15,488 KB
testcase_13 AC 92 ms
15,488 KB
testcase_14 AC 94 ms
14,848 KB
testcase_15 AC 110 ms
15,872 KB
testcase_16 AC 197 ms
22,144 KB
testcase_17 AC 245 ms
27,936 KB
testcase_18 AC 316 ms
32,508 KB
testcase_19 AC 289 ms
34,084 KB
testcase_20 AC 275 ms
32,972 KB
testcase_21 AC 265 ms
32,972 KB
testcase_22 AC 308 ms
32,980 KB
testcase_23 AC 265 ms
32,984 KB
testcase_24 AC 268 ms
32,980 KB
testcase_25 AC 195 ms
23,888 KB
testcase_26 AC 216 ms
27,548 KB
testcase_27 AC 256 ms
32,044 KB
testcase_28 AC 234 ms
31,896 KB
testcase_29 AC 233 ms
32,124 KB
testcase_30 AC 235 ms
25,120 KB
testcase_31 AC 277 ms
30,872 KB
testcase_32 AC 339 ms
36,244 KB
testcase_33 AC 316 ms
37,448 KB
testcase_34 AC 342 ms
37,580 KB
testcase_35 AC 362 ms
37,588 KB
testcase_36 AC 327 ms
37,588 KB
testcase_37 AC 232 ms
33,528 KB
testcase_38 AC 218 ms
26,440 KB
testcase_39 AC 226 ms
36,328 KB
testcase_40 AC 250 ms
37,240 KB
testcase_41 AC 205 ms
39,340 KB
testcase_42 AC 208 ms
40,852 KB
testcase_43 AC 249 ms
45,576 KB
testcase_44 AC 147 ms
23,532 KB
testcase_45 AC 203 ms
26,728 KB
testcase_46 AC 181 ms
32,280 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
using namespace std;
using ll=long long;
const int mod=998244353;
#define rep(i,a,b) for (int i=(int)(a);i<(int)(b);i++)

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int N,M;
    cin>>N>>M;
    vector<vector<int>> G(N);
    rep(i,0,M){
        int a,b;
        cin>>a>>b;
        a--,b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    vector<ll> dp(N,1);
    ll ans=0;
    vector<int> par(N);
    vector<set<int>> s(N);
    rep(i,0,N) par[i]=i;
    ll val=0;
    auto root=[&](auto self,int var) -> int {
        if(var==par[var]) return var;
        par[var]=self(self,par[var]);
        return par[var];
    };
    auto unite=[&](int a,int b) -> void {
        a=root(root,a);
        b=root(root,b);
        if(s[a].size()<s[b].size()) swap(a,b);
        for(auto x:s[b]){
            if(s[a].count(x)){
                dp[x]=(dp[x]+dp[b]-val)%mod;
            }
            else{
                dp[x]=(dp[x]+dp[b]-dp[a])%mod;
                s[a].insert(x);
            }
        }
        par[b]=a;
    };
    rep(i,0,N){
        set<int> tmp;
        for(auto x:G[i]){
            if(x<i) tmp.insert(root(root,x));
            else s[i].insert(x);
        }
        for(auto x:tmp) dp[i]=(dp[i]+dp[x])%mod;
        for(auto x:tmp) dp[x]=(dp[x]+dp[i])%mod;
        ans=(ans+dp[i])%mod;
        val=dp[i];
        for(auto x:tmp){
            s[x].erase(i);
            unite(i,x);
        }
    }
    cout<<(ans+mod)%mod<<"\n";
}
0