QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#747579#5221. 小星星hos_lyric100 ✓368ms4080kbC++142.9kb2024-11-14 17:35:012024-11-14 17:35:11

Judging History

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

  • [2024-11-14 17:35:11]
  • 评测
  • 测评结果:100
  • 用时:368ms
  • 内存:4080kb
  • [2024-11-14 17:35:01]
  • 提交

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> X, Y;
vector<int> U, V;

vector<vector<int>> tree;
vector<int> us;
void dfs(int u, int p) {
  auto it = find(tree[u].begin(), tree[u].end(), p);
  if (it != tree[u].end()) tree[u].erase(it);
  for (const int v : tree[u]) dfs(v, u);
  us.push_back(u);
}

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

int main() {
  for (; ~scanf("%d%d", &N, &M); ) {
    X.resize(M);
    Y.resize(M);
    for (int i = 0; i < M; ++i) {
      scanf("%d%d", &X[i], &Y[i]);
      --X[i];
      --Y[i];
    }
    U.resize(N - 1);
    V.resize(N - 1);
    for (int i = 0; i < N - 1; ++i) {
      scanf("%d%d", &U[i], &V[i]);
      --U[i];
      --V[i];
    }
    
    tree.assign(N, {});
    for (int i = 0; i < N - 1; ++i) {
      tree[U[i]].push_back(V[i]);
      tree[V[i]].push_back(U[i]);
    }
    us.clear();
    dfs(0, -1);
    
    memset(adjG, 0, sizeof(adjG));
    for (int i = 0; i < M; ++i) {
      adjG[X[i]][Y[i]] = true;
      adjG[Y[i]][X[i]] = true;
    }
    
    Int ans = 0;
    for (int h = 0; h < 1 << N; ++h) {
      // hom, ban h
      vector<int> xs;
      for (int x = 0; x < N; ++x) if (!(h >> x & 1)) xs.push_back(x);
      memset(dp, 0, sizeof(dp));
      for (const int u : us) {
        for (const int x : xs) {
          dp[u][x] = 1;
          for (const int v : tree[u]) {
            Int sum = 0;
            for (const int y : xs) if (adjG[x][y]) sum += dp[v][y];
            dp[u][x] *= sum;
          }
        }
      }
      Int here = 0;
      for (const int x : xs) here += dp[0][x];
      ans += (__builtin_parity(h)?-1:+1) * here;
    }
    printf("%lld\n", ans);
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

score: 10
Accepted
time: 1ms
memory: 3832kb

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: 0ms
memory: 3832kb

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: 264ms
memory: 3792kb

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: 170ms
memory: 3836kb

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: 27ms
memory: 3740kb

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: 32ms
memory: 3800kb

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: 114ms
memory: 3856kb

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: 165ms
memory: 4080kb

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: 368ms
memory: 3832kb

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: 168ms
memory: 3788kb

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