E.cpp
Anonymous
c_cpp
03/01/2026 2:25 AM
1.5 KB
35
Indexable
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 998244353;
// Time Complexity: O(n + m)
// Memory Complexity: O(n + m)
struct dsu {
int c=0;
vector<int> p, sz;
dsu() {}
dsu(int n):c(n),p(n),sz(n,1){ iota(p.begin(),p.end(),0); }
int make_set(){
int i=p.size();
p.push_back(i); sz.push_back(1); c++;
return i;
}
int find(int x){ return x==p[x]?x:p[x]=find(p[x]); }
bool unite(int a,int b){
a=find(a), b=find(b);
if(a==b) return 0;
if(sz[a]<sz[b]) swap(a,b);
p[b]=a; sz[a]+=sz[b]; c--;
return 1;
}
int size(int x){ return sz[find(x)]; }
int count(){ return c; }
int nodes(){ return p.size(); }
};
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
int n, m; cin >> n >> m;
vector<ll> pow2(m+1); pow2[0] = 1;
for(int i = 1; i <= m; i++) pow2[i] = (pow2[i-1]*2)%mod;
vector<pair<int, int>> e(m);
for(auto &[x, y] : e) {
cin >> x >> y; x--; y--;
}
dsu DSU(n);
ll ans = 0;
for(int i = m-1; i >= 0; i--) {
auto [x, y] = e[i];
if(DSU.count() > 2) DSU.unite(x, y);
else {
if(DSU.find(x) == DSU.find(y)) continue;
ans += pow2[i+1]; ans %= mod;
}
}
cout << ans;
return 0;
}Editor is loading...
Leave a Comment