結果

問題 No.1116 Cycles of Dense Graph
ユーザー DriceDrice
提出日時 2020-07-18 15:26:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,832 bytes
コンパイル時間 339 ms
コンパイル使用メモリ 36,052 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-20 10:02:10
合計ジャッジ時間 2,522 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 17 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 5 ms
4,376 KB
testcase_06 AC 7 ms
4,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 6 ms
4,380 KB
testcase_12 AC 4 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 4 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 3 ms
4,376 KB
testcase_20 AC 13 ms
4,376 KB
testcase_21 AC 7 ms
4,376 KB
testcase_22 AC 37 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 83 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 92 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 11 ms
4,376 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 100 ms
4,376 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 23 ms
4,380 KB
testcase_40 AC 23 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<cstdio>
//can be done by prefix sum
const long long mod = 998244353;
struct Edge{
    int u,v;
};
Edge e[35];
long long f[100005], invf[100005];
//sum[i][j]: i segment & j node used
long long sum[40][40];
Edge edge[35];
int in[100005];
int queue[50];

long long power(long long a,long long b){
    long long res = 1;
    while(b){
        if(b&1) res = res*a%mod;
        a = a*a%mod;
        b /= 2;
    }
    return res;
}

long long inv(long long u){ return power(u,mod-2); }

long long comb(int n,int m){
    if(m>n || m<0) return 0;
    else{
        return f[n]*invf[n-m]%mod*invf[m]%mod;
    }
}

int min(int a,int b){ return a>b?b:a; }

int max(int a,int b){ return a>b?a:b; }

void preWork(int n,int m){
    f[0] = invf[0] = 1;
    for(long long i = 1; i <= n; i++){ 
        f[i] = f[i-1]*i%mod;
        invf[i] = inv(f[i]);
    }
    for(int i = 1; i <= m; i++){
        for(int j = 2; j <= 2*m; j++){
            sum[i][j] = 0;
            int start = -1;
            if(i==1 && j==2) start = 1;
            else start = 0;
            for(int k = start; k <= n-j; k++){
                long long add = power(2,i-1)%mod*comb(n-j,k)%mod*f[i-1+k]%mod;
                sum[i][j] = (sum[i][j]+add)%mod;
            }
        }
    }
    for(int i = 1; i <= m; i++){
        //for(int j = i+1; j <= 2*i; j++) printf("sum[%d][%d] = %lld\n",i,j,sum[i][j]);
    }
}

int p[100005];
int vis[100005];

int find(int u){
    if(p[u]==u) return u;
    else return p[u] = find(p[u]);
}

struct Graph{
    int x,y;
    //for i,j
    int ok;
    Graph(int x=0,int y=0,int ok=0):x(x),y(y),ok(ok){}
};

Graph getGraph(int n){
    Graph res;
    int ok = 1, cycle = 0, node = 0;
    for(int i = 1; i <= n; i++){
        int u = edge[i].u, v = edge[i].v;
        if(!vis[u]) vis[u] = 1, queue[++node] = u;
        if(!vis[v]) vis[v] = 1, queue[++node] = v;
        in[u]++; in[v]++;
        if(in[u]>2) ok = 0;
        int x = find(u), y = find(v);
        if(x==y) cycle++;
        p[x] = y;
    }
    if(!ok || cycle>1) res = Graph(0,0,0);
    if(cycle==1){
        res = Graph(0,0,-1);
        int root = find(queue[1]);
        for(int i = 2; i <= node; i++){
            int u = find(queue[i]);
            if(u!=root) res = Graph(0,0,0);
        }
    }
    else{
        int seg = 0;
        for(int i = 1; i <= node; i++){
            int u = find(queue[i]), eq = 0;
            for(int j = i+1; j <= node; j++){
                if(find(queue[j])==u) eq = 1;
            }
            if(!eq) seg++;
        }
        res = Graph(seg,node,ok);
    } 
    for(int i = 1; i <= node; i++){
        int u = queue[i];
        in[u] = 0, p[u] = u;
        vis[u] = 0;
    }
    return res;
}

int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    preWork(n,m);
    for(int i = 1; i <= m; i++){
        scanf("%d%d",&e[i].u,&e[i].v);
    }
    long long answer = 0;
    for(int i = 3; i <= n; i++){
        long long cur = comb(n,i)*f[i-1]%mod*inv(2)%mod;
        //printf("i = %d, cur = %lld\n",i,cur);
        answer = (answer+cur)%mod;
    }
    //printf("answer = %lld\n",answer);
    long long sign = mod-1;
    for(int i = 1; i <= n; i++) p[i] = i, vis[i] = 0;
    for(int i = 1; i < (1<<m); i++){
        int cnt = 0, mask = i;
        for(int j = 1; j <= m; j++){
            if(mask%2) edge[++cnt] = e[j];
            mask /= 2;
        }
        Graph g = getGraph(cnt);
        //for(int j = 1; j <= cnt; j++) printf("(%d, %d), ",edge[j].u,edge[j].v);
        //printf("\n");
        //printf("i = %d: %d %d %d\n",i,g.x,g.y,g.ok);
        long long cur = -1;
        if(g.ok==0) cur = 0;
        else if(g.ok==-1) cur = 1;
        else cur = sum[g.x][g.y];
        //printf("cur = %lld\n",cur);
        if(cnt%2) cur = cur*sign%mod;
        answer = (answer+cur)%mod;
    }
    printf("%lld\n",answer);
    return 0;
}
0