111

· · 个人记录

#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <deque>
#include <queue>
#include <vector>
#include <set>
const int N=210000;
const int mo=998244353;
const int w1=(mo+1)/3;
int n,m,father[N],w[N];
int quick(int k1,int k2){
    int k3=1;
    while (k2){
        if (k2&1) k3=1ll*k3*k1%mo;
        k2>>=1; k1=1ll*k1*k1%mo;
    }
    return k3;
}
void merge(int k1,int k2){
    father[k1]=k2; w[k1]=1ll*w[k1]*quick(w[k2],mo-2)%mo;
}
int findfather(int k1){
    if (father[k1]==k1) return k1;
    int f=findfather(father[k1]);
    if (f!=father[k1]) w[k1]=1ll*w[k1]*w[father[k1]]%mo;
    return father[k1]=f;
}
int main(){
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++) father[i]=i,w[i]=1;
    int tot=1;
    for (int i=1;i<=n;i++) tot=3ll*tot%mo;
    for (int i=1;i<=m;i++){
        int ty,k1,k2;
        scanf("%d%d",&ty,&k1);
        if (ty==1){
            scanf("%d",&k2);
            k1=findfather(k1); k2=findfather(k2);
            w[k1]=2ll*w[k1]*w1%mo; w[k2]=1ll*w[k2]*w1%mo;
            merge(k1,k2);
        } else {
            int k2=findfather(k1);
            int ans=1ll*tot*w[k1]%mo;
            if (k1!=k2) ans=1ll*ans*w[k2]%mo;
            printf("%d\n",ans);
        }
    }
    return 0;
}