QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#745141#5221. 小星星hos_lyric100 ✓416ms300032kbC++144.7kb2024-11-14 07:01:232024-11-14 07:01:24

Judging History

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

  • [2024-11-14 07:01:24]
  • 评测
  • 测评结果:100
  • 用时:416ms
  • 内存:300032kb
  • [2024-11-14 07:01:23]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


constexpr int MAX_N = 17;

int N, M;
vector<int> U, V;
vector<int> A, B;

int xss[MAX_N + 1][2];
bool cond[MAX_N][2];

bool adjG[MAX_N][MAX_N];
Int dp[1 << MAX_N][MAX_N][MAX_N];

int main() {
  for (; ~scanf("%d%d", &N, &M); ) {
    U.resize(M);
    V.resize(M);
    for (int i = 0; i < M; ++i) {
      scanf("%d%d", &U[i], &V[i]);
      --U[i];
      --V[i];
    }
    A.resize(N - 1);
    B.resize(N - 1);
    for (int i = 0; i < N - 1; ++i) {
      scanf("%d%d", &A[i], &B[i]);
      --A[i];
      --B[i];
    }
    
    {
      vector<int> adjT(N, 0);
      for (int i = 0; i < N - 1; ++i) {
        adjT[A[i]] |= 1 << B[i];
        adjT[B[i]] |= 1 << A[i];
      }
      vector<int> adjTSum(1 << N, 0);
      for (int u = 0; u < N; ++u) {
        for (int h = 0; h < 1 << u; ++h) adjTSum[h | 1 << u] = adjTSum[h] | adjT[u];
      }
      vector<int> can(1 << N, 0);
      for (int h = 0; h < 1 << N; ++h) if (__builtin_popcount(adjTSum[h] & ~h) <= 2) can[h] = 1;
      vector<int> prv(1 << N, -1);
      prv[0] = -2;
      for (int h = 0; h < 1 << N; ++h) if (can[h] && ~prv[h]) {
        for (int u = 0; u < N; ++u) if (!(h & 1 << u)) prv[h | 1 << u] = u;
      }
      vector<int> us;
      for (int h = (1 << N) - 1; h; ) {
        const int u = prv[h];
        assert(~u);
        us.push_back(u);
        h ^= 1 << u;
      }
cerr<<us<<endl;
      memset(xss, ~0, sizeof(xss));
      memset(cond, 0, sizeof(cond));
      for (int k = 0; k <= N; ++k) {
        vector<int> xs;
        for (int x = 0; x < k; ++x) for (int y = k; y < N; ++y) if (adjT[us[x]] >> us[y] & 1) {
          xs.push_back(x);
        }
        sort(xs.begin(), xs.end());
        xs.erase(unique(xs.begin(), xs.end()), xs.end());
        const int xsLen = xs.size();
cerr<<k<<": "<<xs<<endl;
        assert(xsLen <= 2);
        for (int t = 0; t < xsLen; ++t) xss[k][t] = xs[t];
        if (k < N) {
          for (int t = 0; t < xsLen; ++t) if (adjT[us[xs[t]]] >> us[k] & 1) {
            cond[k][t] = true;
          }
        }
      }
    }
    
    memset(adjG, 0, sizeof(adjG));
    for (int i = 0; i < M; ++i) {
      adjG[U[i]][V[i]] = true;
      adjG[V[i]][U[i]] = true;
    }
    memset(dp, 0, sizeof(dp));
    dp[0][0][0] = 1;
    for (int k = 0; k < N; ++k) {
      for (int h = 0; h < 1 << N; ++h) if (__builtin_popcount(h) == k) {
        int usLen = 0;
        int us[MAX_N];
        for (int u = 0; u < N; ++u) if (u == 0 || (h & 1 << u)) us[usLen++] = u;
        for (int i = 0; i < usLen; ++i) for (int j = 0; j < usLen; ++j) {
          const int u = us[i];
          const int v = us[j];
          const int uu0 = (xss[k + 1][0] == xss[k][0]) ? u : (xss[k + 1][0] == xss[k][1]) ? v : (xss[k + 1][0] == k) ? -1 : 0;
          const int vv0 = (xss[k + 1][1] == xss[k][0]) ? u : (xss[k + 1][1] == xss[k][1]) ? v : (xss[k + 1][1] == k) ? -1 : 0;
          if (dp[h][u][v]) {
            for (int w = 0; w < N; ++w) if (!(h & 1 << w)) {
              if (cond[k][0] && !adjG[u][w]) continue;
              if (cond[k][1] && !adjG[v][w]) continue;
              const int uu = (~uu0) ? uu0 : w;
              const int vv = (~vv0) ? vv0 : w;
              dp[h | 1 << w][uu][vv] += dp[h][u][v];
            }
          }
        }
      }
    }
    Int ans = 0;
    for (int u = 0; u < N; ++u) for (int v = 0; v < N; ++v) {
      ans += dp[(1 << N) - 1][u][v];
    }
    printf("%lld\n", ans);
  }
  return 0;
}

詳細信息


Pretests


Final Tests

Test #1:

score: 10
Accepted
time: 3ms
memory: 299820kb

input:

10 19
1 7
7 9
2 1
6 10
3 5
2 10
6 5
2 5
1 3
7 8
8 5
7 3
8 3
4 10
6 2
4 6
2 4
5 9
1 8
2 8
2 9
4 10
3 9
7 2
9 1
10 2
5 7
9 6

output:

336

result:

ok 1 number(s): "336"

Test #2:

score: 10
Accepted
time: 11ms
memory: 299756kb

input:

10 32
8 3
5 4
8 1
6 10
1 5
8 7
5 3
9 5
4 10
8 4
6 7
8 5
6 5
2 7
2 9
10 2
3 1
1 7
4 1
5 10
8 10
6 2
9 4
9 10
10 7
4 7
6 3
3 9
5 2
2 3
2 4
9 1
2 6
1 10
4 3
7 2
8 5
4 10
2 10
9 10
10 5

output:

97536

result:

ok 1 number(s): "97536"

Test #3:

score: 10
Accepted
time: 416ms
memory: 299728kb

input:

17 72
14 3
5 14
7 15
2 8
12 8
14 8
16 4
1 10
3 4
12 1
17 2
16 17
11 13
15 5
10 16
3 15
2 13
14 7
3 7
8 17
15 1
3 16
8 9
16 8
16 9
6 5
8 15
16 6
8 3
3 9
12 15
5 9
11 6
9 14
9 15
7 12
9 17
4 13
9 10
8 13
16 7
10 5
4 9
12 6
2 6
10 17
3 12
16 5
11 15
4 14
2 1
7 1
9 13
12 4
16 1
13 7
4 15
3 5
14 10
11 16...

output:

6222109568

result:

ok 1 number(s): "6222109568"

Test #4:

score: 10
Accepted
time: 370ms
memory: 299768kb

input:

17 108
12 6
16 5
2 9
1 9
5 7
17 6
6 11
7 4
4 6
8 5
14 17
8 3
15 4
4 1
17 8
15 8
17 1
12 14
14 9
7 2
8 4
6 16
17 9
8 2
7 15
11 1
10 15
16 12
5 17
16 17
11 13
16 2
12 3
5 9
17 3
8 7
2 12
13 12
14 5
10 1
4 10
6 3
12 9
4 17
13 7
8 14
11 10
14 7
10 3
11 2
17 10
13 15
10 12
8 10
16 10
16 3
9 4
8 6
7 12
16...

output:

7131017558340

result:

ok 1 number(s): "7131017558340"

Test #5:

score: 10
Accepted
time: 44ms
memory: 299872kb

input:

14 67
12 11
5 6
5 12
3 5
2 8
9 8
4 5
11 1
6 7
3 8
11 4
1 3
13 1
4 8
3 7
14 10
1 7
12 1
11 6
12 8
9 13
12 14
7 2
3 13
4 6
8 1
11 7
5 13
10 8
11 8
1 4
13 4
8 13
6 14
3 11
7 14
12 4
6 1
4 10
1 10
12 9
8 6
10 11
4 2
3 10
6 9
5 11
9 3
7 10
6 3
6 10
12 10
9 4
10 5
11 2
7 4
7 8
11 9
14 5
12 6
12 7
14 4
3 1...

output:

1105959198

result:

ok 1 number(s): "1105959198"

Test #6:

score: 10
Accepted
time: 35ms
memory: 299912kb

input:

14 60
6 13
10 7
13 14
2 11
3 5
5 8
5 10
9 11
7 12
4 2
1 5
5 7
10 4
3 6
2 13
3 12
4 1
1 9
4 3
9 2
9 13
9 6
14 9
7 9
11 4
1 13
1 12
4 9
6 7
1 11
9 3
2 6
7 8
5 6
2 14
4 14
11 6
8 14
10 3
8 1
10 14
3 1
12 5
5 13
2 7
11 13
3 7
8 4
5 9
1 2
10 11
5 14
10 6
6 4
4 12
9 12
14 11
14 6
14 3
3 13
14 3
10 5
11 3
...

output:

223999622

result:

ok 1 number(s): "223999622"

Test #7:

score: 10
Accepted
time: 60ms
memory: 300032kb

input:

16 40
16 13
5 10
14 7
5 13
9 6
7 12
7 6
10 11
15 1
9 15
5 1
11 4
10 15
11 9
15 13
4 3
9 12
3 6
7 5
12 3
12 4
9 16
13 12
4 10
8 10
15 14
7 15
2 8
4 16
10 3
3 9
16 15
13 7
13 6
3 11
5 4
14 13
14 16
7 10
13 1
13 3
16 8
15 4
8 13
12 4
6 9
16 9
13 1
11 10
12 14
7 3
1 2
5 3
15 11
10 5

output:

61422

result:

ok 1 number(s): "61422"

Test #8:

score: 10
Accepted
time: 171ms
memory: 300028kb

input:

16 82
2 14
10 9
1 6
2 11
2 3
8 14
6 16
12 9
16 2
8 3
1 4
15 16
12 11
3 5
15 7
1 10
10 4
10 15
9 15
13 1
7 1
3 12
13 7
2 1
10 13
1 11
5 1
4 16
10 16
15 1
2 12
12 13
9 1
6 14
7 2
3 7
14 4
7 9
7 10
4 9
11 4
7 16
12 6
1 14
13 6
16 12
13 11
10 3
13 9
6 11
12 10
8 4
11 3
3 16
3 13
3 9
5 6
13 16
7 8
7 5
16...

output:

45556441120

result:

ok 1 number(s): "45556441120"

Test #9:

score: 10
Accepted
time: 393ms
memory: 299796kb

input:

17 72
4 17
12 14
1 6
16 17
14 1
8 14
12 2
3 8
5 10
15 8
13 16
2 6
1 17
5 2
17 14
6 3
11 15
5 8
4 16
6 17
16 1
2 16
7 5
16 6
8 2
14 2
4 6
14 11
10 6
7 10
9 7
17 10
16 14
7 16
11 5
1 4
17 11
3 9
15 4
5 12
10 12
10 9
11 12
15 5
12 15
12 7
16 15
7 1
14 15
5 6
3 17
10 1
16 9
9 17
4 11
11 1
9 13
13 2
15 1...

output:

7321962238

result:

ok 1 number(s): "7321962238"

Test #10:

score: 10
Accepted
time: 392ms
memory: 300028kb

input:

17 124
7 4
9 6
14 15
10 14
9 11
5 13
8 10
17 8
7 2
3 8
11 12
15 4
17 7
7 9
10 17
3 14
16 10
6 4
10 15
15 9
7 12
1 16
14 6
10 1
14 11
10 9
7 15
2 4
3 4
13 8
2 13
8 16
7 5
7 11
13 15
4 10
5 8
7 3
6 1
14 4
16 7
17 1
9 2
1 8
17 15
8 15
16 12
1 11
11 2
15 5
2 16
7 14
4 12
16 15
12 15
16 13
16 3
17 5
8 9
...

output:

73882036916736

result:

ok 1 number(s): "73882036916736"

Extra Test:

score: 0
Extra Test Passed