답안 #867938

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
867938 2023-10-29T21:41:37 Z epicci23 Šarenlist (COCI22_sarenlist) C++17
110 / 110
141 ms 604 KB
#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define endl "\n" 
#define int long long
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()

constexpr int MOD = 1e9 + 7;

int mult(int a,int b){
  a%=MOD;b%=MOD;
  if(a*b>=MOD) return (a*b)%MOD;
  return a*b;
}

int eks(int a,int b){
  a%=MOD;b%=MOD;
  if(a<b) return (a-b)+MOD;
  return a-b;
}

int add(int a,int b){
  a%=MOD;b%=MOD;
  if(a+b>=MOD) return (a+b)%MOD;
  return a+b;
}



struct DSU{
  int comp,comp2;
  vector<int> par;
  vector<int> siz;
  vector<int> vis;
  DSU(int n){
    comp=n;
    comp2=0;
    par.resize(n+5);
    vis.assign(n+5,0);
    siz.assign(n+5,1);
    iota(all(par),0);
  }

  int find(int a){
  	if(par[a]==a) return a;
  	return par[a]=find(par[a]);
  }

  void unite(int a,int b){
    a=find(a),b=find(b);
    if(a==b) return;
    if(!vis[a]){
  	  vis[a]=1;
  	  comp2++;
  	}
  	if(!vis[b]){
  	  vis[b]=1;
  	  comp2++;
  	}
    comp--;
    comp2--;
    if(siz[a]>siz[b]) swap(a,b);
    siz[b]+=siz[a];
    par[a]=b;
  }
};

vector<int> v[65];
int par[65];
int depth[65];

void dfs(int c,int p,int d){
  par[c]=p;
  depth[c]=d;
  for(int x:v[c]){
  	if(x==p) continue;
  	dfs(x,c,d+1);
  }
} 



void solve(){
  int n,m,k;
  cin >> n >> m >> k;

  int ans=0;
  
  int pre[n+5];
  pre[0]=1;
  for(int i=1;i<n+5;i++) pre[i]=mult(pre[i-1],k);

  array<int,2> ar[m+5];

  for(int i=1;i<n;i++){
  	int a,b;
  	cin >> a >> b;
  	v[a].pb(b);
  	v[b].pb(a);
  }
  
  dfs(1,0,0);

  for(int i=0;i<m;i++){
  	int a,b;
  	cin >> a >> b;
  	ar[i]={a,b};
  }
 
  for(int mask=0;mask<(1LL<<m);mask++){

    DSU dsu(n);
    set<int> s;
  	for(int j=0;j<m;j++){
  	  if(mask>>j&1){
        int a = ar[j][0];
        int b = ar[j][1];
        if(depth[a]<depth[b]) swap(a,b);
        set<int> cur_s;
        while(depth[a]>depth[b]){
          s.insert(a);
          cur_s.insert(a);
          a=par[a];
        }
        while(a!=b){
          s.insert(a);
          s.insert(b);
          cur_s.insert(a);
          cur_s.insert(b);
          a=par[a];
          b=par[b];
        }
        int xd = *cur_s.begin();
        for(auto x:cur_s) dsu.unite(xd,x);
      }
  	}

  	int cur=1; 
    for(int i=2;i<=n;i++) if(!s.count(i)) cur=mult(cur,k);
    cur=mult(cur,pre[dsu.comp2]);
    //cout << mask << " " << cur << " " << dsu.comp2 << endl;

  	if(__builtin_popcountll(mask)&1) ans=eks(ans,cur);
  	else ans=add(ans,cur);
  }

  cout << ans << endl;
}

int32_t main(){

  cin.tie(0); ios::sync_with_stdio(0);
  
  int t=1;//cin >> t;
  while(t--) solve();

  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 9 ms 464 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 11 ms 460 KB Output is correct
6 Correct 13 ms 456 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 9 ms 464 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 600 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 11 ms 460 KB Output is correct
25 Correct 13 ms 456 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 60 ms 348 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 59 ms 456 KB Output is correct
31 Correct 16 ms 348 KB Output is correct
32 Correct 7 ms 464 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 6 ms 460 KB Output is correct
35 Correct 30 ms 348 KB Output is correct
36 Correct 141 ms 348 KB Output is correct
37 Correct 78 ms 436 KB Output is correct