답안 #796998

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
796998 2023-07-29T03:42:15 Z cig32 Šarenlist (COCI22_sarenlist) C++17
110 / 110
58 ms 468 KB
#include "bits/stdc++.h"
using namespace std;
#define int long long
const int MAXN = 3e5 + 10;
const int MOD = 1e9 + 7;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
  int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int bm(int b, int p) {
  if(p==0) return 1 % MOD;
  int r = bm(b, p >> 1);
  if(p&1) return (((r*r) % MOD) * b) % MOD;
  return (r*r) % MOD;
}
int inv(int b) { 
  return bm(b, MOD-2);
}
int fastlog(int x) {
  return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void printcase(int i) { cout << "Case #" << i << ": "; }
int n, m, k;
pair<int, int> edges[69];
int dsu[69];
int set_of(int u) {
  if(dsu[u] == u) return u;
  return dsu[u] = set_of(dsu[u]);
}
void union_(int u, int v) {
  dsu[set_of(u)] = set_of(v);
}
vector<pair<int,int> >adj[69];
int bruh[69][69];
bool dfs(int node, int prv, int tar, int id) {
  if(tar == node) return 1;
  for(pair<int,int>x:adj[node]){
    if(x.first==prv) continue;
    bool d=dfs(x.first,node,tar,id);
    if(d) {
      bruh[id][x.second]=1;
      return 1;
    }
  }
  return 0;
}
void solve(int tc) {
  cin >> n >> m >> k;
  for(int i=1; i<n; i++) {
    int u, v;
    cin >> u >> v;
    edges[i] = {u, v};
    adj[u].push_back({v, i});
    adj[v].push_back({u, i});
  }
  for(int i=1; i<=m; i++) {
    int a, b;
    cin >> a >> b;
    dfs(a, -1, b, i-1);
  }
  int ans = 0;
  for(int i=0; i<(1<<m); i++) {
    for(int j=1; j<n; j++) dsu[j] = j;
    for(int j=0; j<m; j++) {
      if(!(i & (1<<j))) continue;
      vector<int> v;
      for(int k=1; k<n; k++) if(bruh[j][k]) v.push_back(k);
      for(int k=1; k<v.size(); k++) union_(v[k-1], v[k]);
    }
    set<int>st;
    for(int j=1;j<n;j++)st.insert(set_of(j));
    int bruh = bm(k, st.size());
    if(__builtin_popcountll(i) & 1) ans = (ans+MOD-bruh) % MOD;
    else ans = (ans+bruh) % MOD;
  }
  cout << ans << '\n';
}
int32_t main() {
  ios::sync_with_stdio(0); cin.tie(0);
  int t = 1; //cin >> t;
  for(int i=1; i<=t; i++) solve(i);
}
// 勿忘初衷

Compilation message

Main.cpp: In function 'void solve(long long int)':
Main.cpp:68:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |       for(int k=1; k<v.size(); k++) union_(v[k-1], v[k]);
      |                    ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 9 ms 336 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 7 ms 212 KB Output is correct
6 Correct 8 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 9 ms 336 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 7 ms 212 KB Output is correct
25 Correct 8 ms 212 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 46 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 46 ms 340 KB Output is correct
31 Correct 11 ms 340 KB Output is correct
32 Correct 7 ms 468 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 6 ms 340 KB Output is correct
35 Correct 24 ms 340 KB Output is correct
36 Correct 58 ms 340 KB Output is correct
37 Correct 27 ms 340 KB Output is correct