UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#201626#3506. colorJosephcheng100775ms16276kbC++111.6kb2024-02-04 09:50:362024-02-04 12:11:18

answer

#include<bits/stdc++.h>
#define LL long long
#define MOD 998244353
using namespace std;

LL n,m,k,u,v,pos,ans=1,tmp,numa,numb;
LL head[200005],f[200005],s1[200005];
bool vis[200005];

struct Edge
{
	LL u,v,nxt;
}e[400005];

void addEdge(LL u,LL v)
{
	e[++pos]={u,v,head[u]};
	head[u]=pos;
}

void init()
{
	for(LL i=1;i<=n;i++)
		f[i]=i,s1[i]=1;
}

LL find(LL x)
{
	if(x==f[x]) return x;
	return f[x]=find(f[x]);
}

bool Judge(LL a,LL b)
{
	return find(a)==find(b);
}

void Merge(LL a,LL b)
{
	if(Judge(a,b)) return ;
	s1[find(b)]+=s1[find(a)];
	f[find(a)]=f[find(b)];
}

LL qpow(LL x,LL y)
{
	if(y==1) return x%MOD;
	if(y==0) return 1%MOD;
	LL res=qpow(x,y/2)%MOD;
	res*=res;
	res%=MOD;
	if(y&1) res*=x;
	res%=MOD;
	return res%MOD;
}

void DFS(LL st){
	for(LL i=head[st];i;i=e[i].nxt){
		LL v=e[i].v;
		if(!vis[v]) vis[v]=true,DFS(v);
	}
}

void check(LL st)
{
	DFS(st);
	if(head[st]==0){ans=ans*k%MOD;return;}
	LL tota=find(st),totb=find(e[head[st]].v);
	if(tota==totb) printf("0"),exit(0);
	LL p1=s1[tota],p2=s1[totb];
	LL res=(qpow(numa,p1)*qpow(numb,p2))%MOD+(qpow(numb,p1)*qpow(numa,p2))%MOD;
	ans*=res;
	ans%=MOD;
}

int main()
{
	scanf("%lld%lld%lld",&n,&m,&k);
	init();
	for(LL i=1;i<=m;i++){
		scanf("%lld%lld",&u,&v);
		addEdge(u,v);
		addEdge(v,u);
	}
	for(LL u=1;u<=n;u++)
		for(LL i=head[u];e[i].nxt;i=e[i].nxt)
			Merge(e[i].v,e[e[i].nxt].v);
	for(LL i=1;i<=n;i++)
		if(!vis[find(i)]) tmp++,vis[find(i)]=true;
	memset(vis,false,sizeof vis);
	numa=k/2,numb=k-numa;
	for(LL i=1;i<=n;i++)
		if(!vis[i]) vis[i]=true,check(i);
	printf("%lld",ans);
}

Details

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 0ms
memory: 1408kb

input:

20 20 3
10 3
20 16
2 1
8 9
8 11
12 18
15 12
18 12
8 2
11 16
5 18
17 16
20 19
1 13
17 7
14 12
15 1
12...

output:

9216

result:

ok single line: '9216'

Test #2:

score: 10
Accepted
time: 0ms
memory: 1404kb

input:

20 20 3
8 3
5 7
11 3
11 1
1 5
20 16
9 4
10 9
18 19
15 20
12 6
19 8
8 19
12 1
7 12
15 14
10 13
5 7
6 ...

output:

18432

result:

ok single line: '18432'

Test #3:

score: 10
Accepted
time: 0ms
memory: 1408kb

input:

20 20 3
10 18
3 10
20 11
2 14
17 10
20 7
8 16
8 10
17 7
4 10
10 20
7 4
10 18
10 20
4 11
14 16
5 7
5 ...

output:

93312

result:

ok single line: '93312'

Test #4:

score: 10
Accepted
time: 99ms
memory: 16264kb

input:

200000 200000 418764776
193300 85491
85491 198433
198433 158022
158022 159598
159598 78506
78506 183...

output:

0

result:

ok single line: '0'

Test #5:

score: 10
Accepted
time: 98ms
memory: 16264kb

input:

200000 200000 841681585
171155 133131
171501 142489
163983 91459
40115 64209
50285 13941
157289 1936...

output:

257248274

result:

ok single line: '257248274'

Test #6:

score: 10
Accepted
time: 104ms
memory: 16272kb

input:

200000 200000 951039297
142466 193503
62542 172063
138481 56026
11065 32973
191482 25759
8202 81263
...

output:

740835073

result:

ok single line: '740835073'

Test #7:

score: 10
Accepted
time: 113ms
memory: 16276kb

input:

200000 200000 42510635
56342 164245
124764 37378
76951 14939
100261 83193
20918 173885
50785 47744
1...

output:

114276220

result:

ok single line: '114276220'

Test #8:

score: 10
Accepted
time: 102ms
memory: 16268kb

input:

200000 200000 110134091
139821 97850
55299 79054
14701 156828
70182 108265
182039 40839
112851 61564...

output:

266493565

result:

ok single line: '266493565'

Test #9:

score: 10
Accepted
time: 126ms
memory: 16272kb

input:

200000 200000 25856153
166328 199727
37861 108403
38269 169893
106479 68909
83571 84393
57819 23770
...

output:

893500359

result:

ok single line: '893500359'

Test #10:

score: 10
Accepted
time: 133ms
memory: 16276kb

input:

200000 200000 42220606
28145 44679
166727 56959
56634 91897
17398 56244
121724 38256
12096 86751
448...

output:

111483693

result:

ok single line: '111483693'