Submission #73712567


Source Code Expand

#include <iostream>
#include <vector>
using namespace std;

const int MOD = 998244353;

struct DSU 
{
    vector<int> parent;
    vector<int> rank;
    DSU (int n) 
    {
        parent.resize(n + 1);
        rank.resize(n + 1, 0);
        for(int i = 1;i <= n;i++)parent[i] = i;
    }
    int find(int x) 
    {
        if(parent[x] != x) parent[x] = find(parent[x]);
        return parent[x];
    }
    void unite(int x, int y) 
    {
        x = find(x);
        y = find(y);
        if(x == y) return;
        if(rank[x] < rank[y]) 
        {
            parent[x] = y;
        } 
        else 
        {
            parent[y] = x;
            if(rank[x] == rank[y]) 
            {
                rank[x]++;
            }
        }
    }
};

int main() 
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N,M;
    cin >> N >> M;
    vector<int> U(M + 1),V(M + 1);
    for(int i = 1;i <= M;i++) cin >> U[i] >> V[i];
    vector<long long> pow2(M + 1);
    pow2[0] = 1;
    for(int i = 1;i <= M;i++) pow2[i] = (pow2[i - 1] * 2) % MOD;
    DSU dsu1(N);
    int cnt = N;
    int last_edge = -1;
    for(int i = M;i >= 1;i--) 
    {
        if(cnt == 1) break;
        int u = U[i];
        int v = V[i];
        if(dsu1.find(u) != dsu1.find(v)) 
        {
            dsu1.unite(u,v);
            cnt--;
            if(cnt == 1) 
            {
                last_edge = i;
                break;
            }
        }
    }
    DSU dsu2(N);
    for(int i = M;i >= last_edge + 1;i--) 
    {
        int u = U[i];
        int v = V[i];
        if(dsu2.find(u) != dsu2.find(v)) dsu2.unite(u, v);
    }
    long long ans = 0;
    for(int i = 1;i <= last_edge;i++) 
    {
        int u = U[i];
        int v = V[i];
        if(dsu2.find(u) != dsu2.find(v)) ans = (ans + pow2[i]) % MOD;
    }
    cout << ans << endl;
    return 0;
}

Submission Info

Submission Time
Task E - Divide Graph
User rgr2025
Language C++23 (GCC 15.2.0)
Score 475
Code Size 1932 Byte
Status AC
Exec Time 22 ms
Memory 9664 KiB

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 3624 KiB
00_sample_01.txt AC 1 ms 3644 KiB
00_sample_02.txt AC 1 ms 3460 KiB
01_random_00.txt AC 11 ms 6092 KiB
01_random_01.txt AC 10 ms 5720 KiB
01_random_02.txt AC 9 ms 5416 KiB
01_random_03.txt AC 2 ms 3772 KiB
01_random_04.txt AC 11 ms 5964 KiB
01_random_05.txt AC 9 ms 5712 KiB
01_random_06.txt AC 8 ms 5348 KiB
01_random_07.txt AC 8 ms 5096 KiB
01_random_08.txt AC 12 ms 5988 KiB
01_random_09.txt AC 9 ms 5220 KiB
01_random_10.txt AC 20 ms 9280 KiB
01_random_11.txt AC 20 ms 9064 KiB
01_random_12.txt AC 22 ms 9180 KiB
01_random_13.txt AC 14 ms 7140 KiB
01_random_14.txt AC 19 ms 8652 KiB
02_perfect_00.txt AC 12 ms 6352 KiB
02_perfect_01.txt AC 13 ms 6504 KiB
02_perfect_02.txt AC 12 ms 6516 KiB
02_perfect_03.txt AC 12 ms 6440 KiB
02_perfect_04.txt AC 12 ms 6500 KiB
02_perfect_05.txt AC 12 ms 6440 KiB
02_perfect_06.txt AC 12 ms 6432 KiB
02_perfect_07.txt AC 12 ms 6500 KiB
02_perfect_08.txt AC 12 ms 6524 KiB
03_tree_00.txt AC 19 ms 9520 KiB
03_tree_01.txt AC 22 ms 9548 KiB
03_tree_02.txt AC 19 ms 9664 KiB
03_tree_03.txt AC 20 ms 9512 KiB
03_tree_04.txt AC 21 ms 9588 KiB
03_tree_05.txt AC 22 ms 9576 KiB
03_tree_06.txt AC 22 ms 9596 KiB
04_handmade_00.txt AC 1 ms 3612 KiB
04_handmade_01.txt AC 16 ms 9520 KiB