QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#121683#856. Cactuskaruna#AC ✓1231ms60632kbC++172.0kb2023-07-08 17:35:442023-07-08 17:35:48

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-08 17:35:48]
  • 评测
  • 测评结果:AC
  • 用时:1231ms
  • 内存:60632kb
  • [2023-07-08 17:35:44]
  • 提交

answer

#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 4e5;
const ll MOD = 1e9+7;

int TC, N, M;
ll K;
vector<int> adj[MAXN+10];

ll dp[MAXN+10], dp2[MAXN+10];

int cnt;
int dfn[MAXN+10], low[MAXN+10];

void dfs(int now, int bef)
{
    dfn[now]=++cnt;
    low[now]=dfn[now];
    for(int nxt : adj[now])
    {
        if(nxt==bef) continue;
        if(dfn[nxt]==0)
        {
            dfs(nxt, now);
            low[now]=min(low[now], low[nxt]);
        }
        else
        {
            low[now]=min(low[now], dfn[nxt]);
        }
    }
}

int bcnt;
bool vis[MAXN+10];
int A[MAXN+10];
void bcc(int now, int bef, int col)
{
    vis[now]=true;
    A[col]++;
    for(int nxt : adj[now])
    {
        if(nxt==bef) continue;
        if(vis[nxt]) continue;
        if(low[nxt]>=dfn[nxt]) bcc(nxt, now, ++bcnt);
        else bcc(nxt, now, col);
    }
}

int main() {
    cin.tie(0); ios_base::sync_with_stdio(0);

    scanf("%d", &TC);
    while(TC--)
    {
        scanf("%d%d%lld", &N, &M, &K);
        for(int i=1; i<=M; i++)
        {
            int u, v;
            scanf("%d%d", &u, &v);
            adj[u].push_back(v);
            adj[v].push_back(u);
        }
        
        dp[1]=K; dp2[1]=K-1;
        dp[2]=K*(K-1)%MOD; dp2[2]=(K-1)*(K-1)%MOD;
        ll t=K-1;
        for(int i=3; i<=N; i++)
        {
            t=t*(K-1)%MOD;
            dp[i]=K*t%MOD-dp[i-1];
            dp2[i]=(K-1)*t%MOD-dp2[i-1];
            dp[i]=(dp[i]%MOD+MOD)%MOD;
            dp2[i]=(dp2[i]%MOD+MOD)%MOD;
        }

        dfs(1, 1);
        bcc(1, 1, ++bcnt);

        ll ans=dp[A[1]];
        for(int i=2; i<=bcnt; i++) ans=ans*dp2[A[i]]%MOD;
        printf("%lld\n", ans);

        for(int i=1; i<=N; i++)
        {
            adj[i].clear();
            dfn[i]=low[i]=vis[i]=0;
            A[i]=0;
            dp[i]=0; dp2[i]=0;
        }

        cnt=0; bcnt=0;
    }
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 21724kb

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: 65ms
memory: 20368kb

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: 1231ms
memory: 60632kb

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