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
| #include<bits/stdc++.h> #define RG register #define file(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout); #define clear(x, y) memset(x, y, sizeof(x)); using namespace std;
inline int read() { int data=0, w=1; char ch=getchar(); while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar(); if(ch=='-') w=-1, ch=getchar(); while(ch>='0'&&ch<='9') data=(data<<3)+(data<<1)+(ch^48), ch=getchar(); return data*w; }
const int maxn(1e5+10), mod(31011); struct edge { int u, v, w; } e[maxn]; inline bool cmp(const edge &a, const edge &b) { return a.w<b.w; }
int n, m, dis[maxn], len, L[maxn], R[maxn], fa[maxn], pre[maxn], mst, ans=1; inline int find(int x) { return fa[x]==x?x:fa[x]=find(fa[x]); }
int main() { n=read(); m=read(); for(RG int i=1;i<=m;i++) e[i]=(edge){read(), read(), read()}, dis[i]=e[i].w; clear(L, 127); sort(e+1, e+m+1, cmp); sort(dis+1, dis+m+1); len=unique(dis+1, dis+m+1)-dis-1; for(RG int i=1;i<=m;i++) { e[i].w=lower_bound(dis+1, dis+len+1, e[i].w)-dis; L[e[i].w] = min(L[e[i].w], i); R[e[i].w] = max(R[e[i].w], i); } for(RG int i=1;i<=n;i++) fa[i]=i; for(RG int t=1;t<=len;t++) { for(RG int i=1;i<=n;i++) pre[i]=fa[i]; int sum=0, tot=0; for(RG int i=L[t];i<=R[t];i++) if(find(e[i].u)^find(e[i].v)) { sum++; (mst+=dis[t])%=mod; fa[find(e[i].u)]=find(e[i].v); } if(!sum) continue; for(RG int i=1, SET=(1<<(R[t]-L[t]+1))-1;i<=SET;i++) { for(RG int j=1;j<=n;j++) fa[j]=pre[j]; int now=0; bool bj=true; for(RG int j=0;j<=R[t]-L[t];j++) if(i&(1<<j)) { if(find(e[j+L[t]].u)^find(e[j+L[t]].v)) now++, fa[find(e[j+L[t]].u)]=find(e[j+L[t]].v); else bj=false; } if(now==sum&&bj) tot++; } ans=1ll*ans*tot%mod; for(RG int i=L[t];i<=R[t];i++) fa[find(e[i].u)]=find(e[i].v); bool bj=true; for(RG int i=2;i<=n;i++) if(find(i)^find(1)) { bj=false; break; } if(bj) return printf("%d %d %d\n", n-1, mst, ans*n%mod)&0; } return 0; }
|