Submission #73704948


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
#define int long long

const int kMod = 998'244'353;

struct DisjointSet {
    vector<int> parent;
    vector<int> sz;
    int curr;

    DisjointSet(int n) {
        parent.assign(n, 0);
        sz.assign(n, 1);

        for (int i = 0; i < n; ++i) {
            parent[i] = i;
        }
        curr = n - 1;
    }

    int find(int x) {
        if (x != parent[x]) {
            parent[x] = find(parent[x]);
        }

        return parent[x];
    }

    void unite(int a, int b) {
        a = find(a);
        b = find(b);

        if (a == b) {
            return;
        }

        if (sz[a] > sz[b]) {
            swap(a, b);
        }

        parent[a] = b;
        sz[b] += sz[a];
    }
};

int power(int a, int b) {
    int res = 1;

    while (b) {
        if (b & 1) {
            res = (res * a) % kMod;
        }

        a = (a * a) % kMod;
        b >>= 1;
    }

    return res;
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(nullptr);

    int N, M;
    cin >> N >> M;

    int base = 0;
    int res = 0;
    int cnt = 0;
    DisjointSet ds(N + 1);
    vector<pair<int, int>> edges(M + 1);

    for (int i = 1; i <= M; ++i) {
        cin >> edges[i].first >> edges[i].second;
    }

    for (int i = M; i >= 1; --i) {
        auto [u, v] = edges[i];
        if (ds.find(u) != ds.find(v)) {
            if (ds.curr > 2) {
                ds.unite(u, v);
                --ds.curr;
            }

            else {
                res = (res + power(2, i)) % kMod;
            }
        }
        
    }

    cout << res;
}

Submission Info

Submission Time
Task E - Divide Graph
User bluerini
Language C++23 (GCC 15.2.0)
Score 475
Code Size 1696 Byte
Status AC
Exec Time 26 ms
Memory 9540 KiB

Compile Error

./Main.cpp: In function 'int main()':
./Main.cpp:69:9: warning: unused variable 'base' [-Wunused-variable]
   69 |     int base = 0;
      |         ^~~~
./Main.cpp:71:9: warning: unused variable 'cnt' [-Wunused-variable]
   71 |     int cnt = 0;
      |         ^~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 475 / 475
Status
AC × 3
AC × 36
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 02_perfect_00.txt, 02_perfect_01.txt, 02_perfect_02.txt, 02_perfect_03.txt, 02_perfect_04.txt, 02_perfect_05.txt, 02_perfect_06.txt, 02_perfect_07.txt, 02_perfect_08.txt, 03_tree_00.txt, 03_tree_01.txt, 03_tree_02.txt, 03_tree_03.txt, 03_tree_04.txt, 03_tree_05.txt, 03_tree_06.txt, 04_handmade_00.txt, 04_handmade_01.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3608 KiB
00_sample_01.txt AC 1 ms 3480 KiB
00_sample_02.txt AC 1 ms 3604 KiB
01_random_00.txt AC 11 ms 6168 KiB
01_random_01.txt AC 11 ms 5708 KiB
01_random_02.txt AC 9 ms 5432 KiB
01_random_03.txt AC 2 ms 3820 KiB
01_random_04.txt AC 12 ms 5948 KiB
01_random_05.txt AC 9 ms 5688 KiB
01_random_06.txt AC 8 ms 5268 KiB
01_random_07.txt AC 8 ms 5060 KiB
01_random_08.txt AC 12 ms 6084 KiB
01_random_09.txt AC 9 ms 5180 KiB
01_random_10.txt AC 19 ms 9040 KiB
01_random_11.txt AC 20 ms 8912 KiB
01_random_12.txt AC 21 ms 9096 KiB
01_random_13.txt AC 13 ms 7192 KiB
01_random_14.txt AC 18 ms 8640 KiB
02_perfect_00.txt AC 12 ms 6484 KiB
02_perfect_01.txt AC 13 ms 6476 KiB
02_perfect_02.txt AC 12 ms 6548 KiB
02_perfect_03.txt AC 17 ms 6540 KiB
02_perfect_04.txt AC 12 ms 6456 KiB
02_perfect_05.txt AC 18 ms 6408 KiB
02_perfect_06.txt AC 13 ms 6388 KiB
02_perfect_07.txt AC 13 ms 6552 KiB
02_perfect_08.txt AC 13 ms 6468 KiB
03_tree_00.txt AC 26 ms 9508 KiB
03_tree_01.txt AC 26 ms 9540 KiB
03_tree_02.txt AC 19 ms 9536 KiB
03_tree_03.txt AC 20 ms 9536 KiB
03_tree_04.txt AC 25 ms 9532 KiB
03_tree_05.txt AC 20 ms 9536 KiB
03_tree_06.txt AC 21 ms 9540 KiB
04_handmade_00.txt AC 1 ms 3528 KiB
04_handmade_01.txt AC 16 ms 9424 KiB