-
Notifications
You must be signed in to change notification settings - Fork 2
/
HDU-jinhua-1009.cpp
136 lines (130 loc) · 3.04 KB
/
HDU-jinhua-1009.cpp
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//#pragma comment(linker, "/STACK:65536000")
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<deque>
#include<iomanip>
#include<cmath>
#include<set>
#define sf scanf
#define pf printf
#define llg long long
using namespace std;
//ifstream inf("ti.in");
//ofstream ouf("ti.out");
const int maxlongint=2147483647;
#define N 105
#define M 1005
#define E
#define inf 0x3f3f3f3f
#define dinf 1e10
#define linf (LL)1<<60
#define LL long long
#define clr(a,b) memset(a,b,sizeof(a))
struct Edge
{
LL a,b,c;
bool operator<(const Edge & t)const
{
return c<t.c;
}
}edge[M];
LL ans;
LL fa[N],ka[N],vis[N];
LL gk[N][N],tmp[N][N];
vector<LL>gra[N];
LL i,j,k,t,n,m,mod;
LL fffa(LL a,LL b[]){return a==b[a]?a:b[a]=fffa(b[a],b);}
LL ddt(LL a[][N],LL n)
{
for(LL i=0;i<n;i++)for(LL j=0;j<n;j++)a[i][j]%=mod;
LL ret=1;
for(LL i=1;i<n;i++)
{
for(LL j=i+1;j<n;j++)
while(a[j][i])
{
LL t=a[i][i]/a[j][i];
for(LL k=i;k<n;k++)
a[i][k]=(a[i][k]-a[j][k]*t)%mod;
for(LL k=i;k<n;k++)
swap(a[i][k],a[j][k]);
ret=-ret;
}
if(a[i][i]==0)return 0;
ret=ret*a[i][i]%mod;
}
return (ret+mod)%mod;
}
int main()
{
freopen("ti.in","r",stdin);
while (sf("%I64d%I64d%I64d",&n,&m,&mod)!=EOF)
{
for (LL i=0;i<=n;i++)
gra[i].clear();
memset(ka,0,sizeof(ka));
memset(gk,0,sizeof(gk));
if (n==0&&m==0&&mod==0)
break;
for(LL i=0;i<m;i++)
sf("%I64d%I64d%I64d",&edge[i].a,&edge[i].b,&edge[i].c);
sort(edge,edge+m);
for(LL i=1;i<=n;i++)fa[i]=i,vis[i]=0;
LL pre=-1;ans=1;
for(LL h=0;h<=m;h++)
{
if(edge[h].c!=pre||h==m)
{
for(LL i=1;i<=n;i++)
if(vis[i])
{
LL u=fffa(i,ka);
gra[u].push_back(i);
vis[i]=0;
}
for(LL i=1;i<=n;i++)
if(gra[i].size()>1)
{
for(LL a=1;a<=n;a++)
for(LL b=1;b<=n;b++)
tmp[a][b]=0;
LL len=gra[i].size();
for(LL a=0;a<len;a++)
for(LL b=a+1;b<len;b++)
{
LL la=gra[i][a],lb=gra[i][b];
tmp[a][b]=(tmp[b][a]-=gk[la][lb]);
tmp[a][a]+=gk[la][lb];tmp[b][b]+=gk[la][lb];
}
LL ret=ddt(tmp,len);
ans=(ans*ret)%mod;
for(LL a=0;a<len;a++)fa[gra[i][a]]=i;
}
for(LL i=1;i<=n;i++)
{
ka[i]=fa[i]=fffa(i,fa);
gra[i].clear();
}
if(h==m)break;
pre=edge[h].c;
}
LL a=edge[h].a,b=edge[h].b;
LL pa=fffa(a,fa),pb=fffa(b,fa);
if(pa==pb)continue;
vis[pa]=vis[pb]=1;
ka[fffa(pa,ka)]=fffa(pb,ka);
gk[pa][pb]++;gk[pb][pa]++;
}
LL flag=0;
for(LL i=2;i<=n&&!flag;i++)if(ka[i]!=ka[i-1])flag=1;
printf("%I64d\n",flag?0:ans);
}
return 0;
}