Submission #867812

# Submission time Handle Problem Language Result Execution time Memory
867812 2023-10-29T13:59:05 Z epicci23 Šarenlist (COCI22_sarenlist) C++17
35 / 110
2 ms 500 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){
  if(a*b>=MOD) return (a*b)%MOD;
  return a*b;
}

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

int add(int a,int b){
  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){
  	if(!vis[a]){
  	  vis[a]=1;
  	  comp2++;
  	}
  	if(!vis[b]){
  	  vis[b]=1;
  	  comp2++;
  	}
    a=find(a),b=find(b);
    if(a==b) return;
    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);

  	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);
        while(depth[a]>depth[b]){
          dsu.unite(a,par[a]);
          a=par[a];
        }
        while(a!=b){
          dsu.unite(a,par[a]);
          a=par[a];
          dsu.unite(b,par[b]);
          b=par[b];
        }
      }
  	}

  	int cur=1; 
    cur=mult(cur,pre[dsu.comp2]); 
    cur=mult(cur,pre[dsu.comp-1]);

  	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;
}
# Verdict Execution time Memory 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 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 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 348 KB Output is correct
5 Correct 1 ms 500 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory 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 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 500 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 2 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Incorrect 0 ms 348 KB Output isn't correct
22 Halted 0 ms 0 KB -