Submission #542518

# Submission time Handle Problem Language Result Execution time Memory
542518 2022-03-26T20:04:07 Z inksamurai Šarenlist (COCI22_sarenlist) C++17
110 / 110
435 ms 436 KB
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,x,n) for(int i=x;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3wByxeT ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e

//snuke's mod int
template <ll mod>
struct modint{
	ll x; // typedef long long ll;
	modint(ll x=0):x((x%mod+mod)%mod){}
	modint operator-()const{return modint(-x);}
	modint& operator+=(const modint a){if((x+=a.x)>=mod) x-=mod; return *this;}
	modint& operator-=(const modint a){if((x+=mod-a.x)>=mod) x-=mod; return *this;}
	modint& operator*=(const modint a){(x*=a.x)%=mod; return *this;}
	modint operator+(const modint a)const{modint res(*this); return res+=a;}
	modint operator-(const modint a)const{modint res(*this); return res-=a;}
	modint operator*(const modint a)const{modint res(*this); return res*=a;}
	modint pow(ll n)const{
		modint res=1,x(*this);
		while(n){
			if(n&1)res*=x;
			x*=x;
			n>>=1;
		}
		return res;
	}
	modint inv()const{return pow(mod-2);}
};
using mint=modint<1000'000'007>;

signed main(){
_3wByxeT;
	int n,m,k;
	cin>>n>>m>>k;

	vec(vi) adj(n);
	rep(i,n-1){
		int u,v;
		cin>>u>>v;
		u-=1,v-=1;
		adj[u].pb(v);
		adj[v].pb(u);
	}

	using vp=vec(pii);
	vp es;
	rep(i,m){
		int u,v;
		cin>>u>>v;
		u-=1,v-=1;
		es.pb({u,v});
	}

	mint ans=0;

	rng(msk,1,(1<<m)){
		std::map<pii,int> mp,admp;

		auto dfs=[&](auto self,int v,int par,int nd)->bool{
			if(v==nd) return 1;
			bool sok=0;
			for(auto u:adj[v]){
				if(u==par) continue;
				bool pok=self(self,u,v,nd);
				if(pok){
					admp[minmax(u,v)]=1;
					mp[minmax(u,v)]=1;
				}
				sok=sok or pok;
			}
			return sok;
		};

		vec(std::map<pii,int>) rbts;
		auto add=[&](int u,int v){
			mp.clear();
			dfs(dfs,v,-1,u);
			rbts.pb(mp);
		};

		rep(i,m){
			if(msk>>i&1){
				auto [u,v]=es[i];
				add(u,v);
			}
		}

		mint now=1;
		{
			int nen=sz(rbts);
			vec(vi) g(nen);
			rep(i,nen){
				rng(j,i+1,nen){
					bool pok=0;
					for(auto p:rbts[i]){
						if(rbts[j].find(p.fi)!=rbts[j].end()){
							pok=1;
							break;
						}
					}
					if(pok){
						g[i].pb(j);
						g[j].pb(i);
					}
				}
			}
			vi usd(nen,0);
			auto rfs=[&](auto self,int v)->void{
				for(auto u:g[v]){
					if(!usd[u]){
						usd[u]=1;
						self(self,u);
					}
				}
			};
			rep(i,nen){
				if(usd[i]) continue;
				now*=mint(k);
				rfs(rfs,i);
			}
		}

		if(__builtin_popcount(msk)%2){
			ans-=now*mint(k).pow(n-1-sz(admp));
		}else{
			ans+=now*mint(k).pow(n-1-sz(admp));
		}
	}

	ans+=mint(k).pow(n-1);
	print(ans.x);
//
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 2 ms 212 KB Output is correct
5 Correct 25 ms 324 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 42 ms 212 KB Output is correct
6 Correct 51 ms 312 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 2 ms 212 KB Output is correct
17 Correct 25 ms 324 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 320 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 42 ms 212 KB Output is correct
25 Correct 51 ms 312 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 195 ms 328 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 221 ms 332 KB Output is correct
31 Correct 58 ms 212 KB Output is correct
32 Correct 19 ms 332 KB Output is correct
33 Correct 3 ms 212 KB Output is correct
34 Correct 20 ms 328 KB Output is correct
35 Correct 102 ms 436 KB Output is correct
36 Correct 435 ms 332 KB Output is correct
37 Correct 210 ms 332 KB Output is correct