1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| #include <iostream> #include <cstring> #include <cstdio> #include <cctype>
using namespace std;
inline int read() { int x=0,f=1; char ch=getchar(); while (!isdigit(ch)) f=ch=='-'?-1:f,ch=getchar(); while (isdigit(ch)) x=x*10+ch-'0',ch=getchar(); return x*f; }
const int P=998244353; const int N=300005; const int E=N<<1;
int last[N],cir[N],pts[N],used[N]; int tov[E],nxt[E],len[E]; bool vis[N]; int T,n,tot,ans;
inline void insert(int x,int y,int z){tov[++tot]=y,len[tot]=z,nxt[tot]=last[x],last[x]=tot;}
int dfs(int x,int e=0) { int ret=0; vis[cir[++cir[0]]=pts[++pts[0]]=x]=1; for (int i=last[x],y,tmp;i;i=nxt[i]) if (i-1>>1!=e) { if (vis[y=tov[i]]) return y; tmp=dfs(y,i-1>>1); if (tmp) ret=tmp; } if (!ret) cir[cir[0]--]=0; return ret; }
void calc(int x) { vis[x]=1; for (int i=last[x],y;i;i=nxt[i]) if (!vis[y=tov[i]]) ans=1ll*ans*len[i]%P,calc(y); }
void go(int x,int &ret,int cur) { for (int i=last[x],y;i;i=nxt[i]) if (used[i-1>>1]!=cur&&!vis[y=tov[i]]) used[i-1>>1]=cur,go(y,ret=1ll*ret*len[i]%P,cur); }
void clear(){memset(last,0,sizeof last),tot=0;}
int main() { freopen("match.in","r",stdin),freopen("match.out","w",stdout); for (T=read();T--;clear()) { n=read(); for (int i=1,x,l,y,r;i<=n;++i) x=read(),l=read()%P,y=read(),r=read()%P,insert(x,y,r),insert(y,x,l),used[i]=vis[i]=0; ans=1; for (int p=1,cp,a,b;p<=n;++p) { if (vis[p]) continue; cir[0]=pts[0]=0,cp=dfs(p); for (int i=1;i<=pts[0];++i) vis[pts[i]]=0; cir[0]=pts[0]=0,dfs(cp); for (int i=1;i<=pts[0];++i) vis[pts[i]]=0; for (int i=1;i<=cir[0];++i) vis[cir[i]]=1; for (int i=1;i<=cir[0];++i) calc(cir[i]); a=b=0; for (int i=1;i<=cir[0];++i) vis[cir[i]]=0; for (int i=last[cp],y;i;i=nxt[i]) if (!vis[y=tov[i]]) if (!a) go(y,a=len[i],used[i-1>>1]=(p<<1)-1); else go(y,b=len[i],used[i-1>>1]=p<<1); for (int i=1;i<=cir[0];++i) vis[cir[i]]=1; ans=1ll*ans*((a+b)%P)%P; } printf("%d\n",ans); } fclose(stdin),fclose(stdout); return 0; }
|