QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#456926#856. CactusKevin5307AC ✓1198ms45184kbC++231.9kb2024-06-28 17:23:442024-06-28 17:23:44

Judging History

你现在查看的是最新测评结果

  • [2024-06-28 17:23:44]
  • 评测
  • 测评结果:AC
  • 用时:1198ms
  • 内存:45184kb
  • [2024-06-28 17:23:44]
  • 提交

answer

//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define ll long long
#define ull unsigned ll
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
#define longer __int128_t
void die(string S){puts(S.c_str());exit(0);}
const ll mod=1e9+7;
vector<int> G[300300];
ll dp[300300][2];
int f[300300];
int nxt[300300];
int depth[300300];
inline int anc(int x)
{
	while(nxt[x]!=x) x=nxt[x]=nxt[nxt[x]];
	return x;
}
void dfs(int u,int fa)
{
	depth[u]=depth[fa]+1;
	f[u]=fa;
	for(auto v:G[u])
		if(v!=fa)
			dfs(v,u);
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin>>t;
	while(t--)
	{
		int n,m;
		cin>>n>>m;
		ll c;
		cin>>c;
		for(int i=0;i<=n;i++)
			dp[i][0]=dp[i][1];
		dp[1][0]=1;
		for(int i=2;i<=n;i++)
		{
			dp[i][0]=dp[i-1][1];
			dp[i][1]=(dp[i-1][0]*(c-1)+dp[i-1][1]*(c-2))%mod;
		}
		ll ans=1;
		for(int i=1;i<=n;i++)
			G[i].clear();
		for(int i=1;i<=n;i++)
			nxt[i]=i;
		vector<pii> ve;
		for(int i=1;i<=m;i++)
		{
			int u,v;
			cin>>u>>v;
			if(anc(u)!=anc(v))
			{
				G[u].pb(v);
				G[v].pb(u);
				nxt[anc(u)]=anc(v);
			}
			else ve.pb(u,v);
		}
		dfs(1,0);
		int tot=n;
		for(auto pr:ve)
		{
			int u=pr.first,v=pr.second;
			int cnt=1;
			while(u!=v)
			{
				if(depth[u]<depth[v]) swap(u,v);
				u=f[u];
				cnt++;
			}
			tot-=cnt-1;
			ans=ans*dp[cnt][1]%mod;
		}
		for(int i=1;i<tot;i++)
			ans=ans*(c-1)%mod;
		ans=ans*c%mod;
		cout<<ans<<'\n';
	}
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3688kb

input:

2
2 1 100
1 2
6 7 3
1 2
2 3
3 1
4 5
5 6
6 4
1 4

output:

9900
24

result:

ok 2 number(s): "9900 24"

Test #2:

score: 0
Accepted
time: 53ms
memory: 5648kb

input:

50000
9 10 4
4 7
5 2
1 5
7 3
9 6
8 3
3 2
9 1
4 8
6 2
10 11 4
4 1
1 3
5 1
10 9
8 4
1 6
7 9
7 10
8 1
1 9
10 2
10 10 4
7 5
6 9
5 1
9 7
10 9
4 9
5 10
2 3
3 7
3 8
9 10 4
3 9
3 7
5 4
6 2
1 9
6 5
4 2
9 8
5 1
7 8
9 9 4
9 4
4 1
6 3
8 7
2 9
6 7
1 8
6 9
5 2
10 11 4
7 8
6 2
9 10
7 2
2 4
4 7
3 7
3 1
10 6
6 9
5 1...

output:

15120
34992
61236
15876
19764
34992
19692
34992
52488
19440
19764
34992
19692
13608
13608
52488
19692
13608
40824
34992
17496
40824
19656
52488
19764
13176
34992
59040
19692
34992
52488
13176
19656
34992
19680
599760
52488
34992
61236
19440
58320
11664
34992
40824
20412
34992
20412
34992
61236
34992...

result:

ok 50000 numbers

Test #3:

score: 0
Accepted
time: 1198ms
memory: 45184kb

input:

10
300000 344504 711589813
136663 59111
262959 256239
220957 296457
132870 53422
167951 237433
252790 102337
18228 30482
162993 268323
127652 185288
133496 174755
122093 241171
165750 24398
4524 236165
261647 83998
127329 221453
263837 257156
263948 122651
142880 167089
203580 26970
4992 84305
11692...

output:

46959312
961402883
2
219976660
721840853
507095342
747233052
107251856
932546015
975072100

result:

ok 10 numbers