并查集模板,就酱紫。
#include<iostream>
using namespace std;
#define mod 998244353
long long ans;
int fa[4000010];
int find(int x){ return fa[x]==x?x:fa[x]=find(fa[x]);}
void merge(int x,int y){x=find(x),y=find(y); if(x!=y)fa[x]=y;}
int main(){
ios::sync_with_stdio(false);
int n, m;
cin>>n>>m;
for(int i = 1; i <= n; i++)fa[i] = i;
for(int i = 1; i <= m; i++){
int op, u, v;
cin>>op>>u>>v;
if(op==0){
merge(u,v);
}else{
if(find(u)==find(v))ans = (ans*2+1)%mod;
else ans = (ans*2)%mod;
}
}
cout<<ans<<"
";
return 0;
}