結果

問題 No.2588 Increasing Record
ユーザー 👑 potato167potato167
提出日時 2023-12-16 04:00:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 371 ms / 3,000 ms
コード長 1,452 bytes
コンパイル時間 2,519 ms
コンパイル使用メモリ 218,680 KB
実行使用メモリ 44,784 KB
最終ジャッジ日時 2023-12-16 04:00:50
合計ジャッジ時間 13,629 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 2 ms
6,548 KB
testcase_04 AC 1 ms
6,548 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 2 ms
6,548 KB
testcase_07 AC 2 ms
6,548 KB
testcase_08 AC 2 ms
6,548 KB
testcase_09 AC 2 ms
6,548 KB
testcase_10 AC 2 ms
6,548 KB
testcase_11 AC 2 ms
6,548 KB
testcase_12 AC 103 ms
15,488 KB
testcase_13 AC 102 ms
15,488 KB
testcase_14 AC 104 ms
14,848 KB
testcase_15 AC 151 ms
15,872 KB
testcase_16 AC 218 ms
21,888 KB
testcase_17 AC 273 ms
27,536 KB
testcase_18 AC 295 ms
31,852 KB
testcase_19 AC 293 ms
33,364 KB
testcase_20 AC 298 ms
32,188 KB
testcase_21 AC 274 ms
32,180 KB
testcase_22 AC 270 ms
32,188 KB
testcase_23 AC 274 ms
32,192 KB
testcase_24 AC 292 ms
32,188 KB
testcase_25 AC 207 ms
23,488 KB
testcase_26 AC 220 ms
27,020 KB
testcase_27 AC 240 ms
31,252 KB
testcase_28 AC 264 ms
31,120 KB
testcase_29 AC 240 ms
31,332 KB
testcase_30 AC 278 ms
24,720 KB
testcase_31 AC 317 ms
30,216 KB
testcase_32 AC 339 ms
35,524 KB
testcase_33 AC 348 ms
36,664 KB
testcase_34 AC 356 ms
36,788 KB
testcase_35 AC 345 ms
36,796 KB
testcase_36 AC 371 ms
36,796 KB
testcase_37 AC 259 ms
32,736 KB
testcase_38 AC 236 ms
26,040 KB
testcase_39 AC 246 ms
35,536 KB
testcase_40 AC 246 ms
36,448 KB
testcase_41 AC 241 ms
38,548 KB
testcase_42 AC 267 ms
40,060 KB
testcase_43 AC 324 ms
44,784 KB
testcase_44 AC 171 ms
23,092 KB
testcase_45 AC 198 ms
26,128 KB
testcase_46 AC 195 ms
31,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
using namespace std;
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<int> dp(N,1);
    int ans=0;
    vector<int> par(N);
    vector<set<int>> s(N);
    rep(i,0,N) par[i]=i;
    int 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]){
            dp[x]=(dp[x]+dp[b])%mod;
            dp[x]=(dp[x]-(s[a].count(x)?val: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