Submission #405096

# Submission time Handle Problem Language Result Execution time Memory
405096 2021-05-15T17:13:36 Z ryansee Capital City (JOI20_capital_city) C++14
0 / 100
1020 ms 43324 KB
#include "bits/stdc++.h"
using namespace std;

#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr<<"hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x,y) (lower_bound(all(x),y)-x.begin())
#define ubd(x,y) (upper_bound(all(x),y)-x.begin())
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline long long rand(long long x, long long y) { return rng() % (y+1-x) + x; } //inclusive
string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?llabs(b):gcd(b%a,a); }

using ll=long long;
using ld=long double;
#define FOR(i,s,e) for(ll i=s;i<=ll(e);++i)
#define DEC(i,s,e) for(ll i=s;i>=ll(e);--i)
using pi=pair<ll,ll>; using spi=pair<ll,pi>; using dpi=pair<pi,pi>; 

long long LLINF = 1e18;
int INF = 1e9+1e6;
#define MAXN (200006)
ll n, k, C[MAXN], ans, legit[MAXN];
vector<int> v[MAXN];

int total, sz[MAXN];
bitset<MAXN> done;
void dfs1(int x,int p) {
	++ total, sz[x] = 1;
	for(auto i:v[x]) if(i^p && !done[i]) dfs1(i, x), sz[x] += sz[i];
}
int dfs2(int x,int p) {
	for(auto i:v[x]) if(i^p && !done[i] && sz[i]>total/2) return dfs2(i, x);
	return x;
}
vector<int> ppl[MAXN];
bitset<MAXN> vis;
int P[MAXN];
void solve(int x) {
	vector<int> colours;
	function<void(int,int)>dfs=[&](int x,int p) {
		P[x]=p, ppl[C[x]].eb(x), colours.eb(C[x]);
		for(auto i:v[x]) if(i^p && !done[i]) dfs(i, x);
	};
	dfs(x, x);
	sort(all(colours)), colours.resize(unique(all(colours))-colours.begin());
	
	queue<int> q;
	ll sum = 0;
	if(ppl[C[x]].size() != legit[C[x]]) sum = LLINF;
	for(auto i:ppl[C[x]]) vis[i]=1, q.emplace(i);
	ppl[C[x]].clear();
	while(q.size()) {
		int x = q.front();
		q.pop();
		sum += !ppl[C[x]].empty();
		if(ppl[C[x]].size() && ppl[C[x]].size() != legit[C[x]]) sum = LLINF;
		while(ppl[C[x]].size()) {
			if(!vis[ppl[C[x]].back()]) vis[ppl[C[x]].back()]=1, q.emplace(ppl[C[x]].back());
			ppl[C[x]].pop_back();
		}
		if(!vis[P[x]]) vis[P[x]]=1, q.emplace(P[x]);
	}
	
	for(auto i:colours) ppl[i].clear();
	function<void(int,int)>dfs2=[&](int x,int p) {
		vis[x] = 0;
		for(auto i:v[x]) if(i^p && !done[i]) dfs2(i, x);
	};
	
	ans = min(ans, sum);
}
void dfs3(int x) {
	total = 0;
	dfs1(x, x);
	x = dfs2(x, x);
	solve(x);
	done[x] = 1;
	for(auto i:v[x]) if(!done[i]) dfs3(i);
}

int main() {
	FAST
	cin>>n>>k, ans=k-1;
	FOR(i,1,n-1) {
		int a, b;
		cin>>a>>b;
		v[a].eb(b), v[b].eb(a);
	}
	FOR(i,1,n) cin>>C[i], ++legit[C[i]];
	dfs3(1);
	cout<<ans<<'\n';
}

Compilation message

capital_city.cpp: In function 'void solve(int)':
capital_city.cpp:56:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
   56 |  if(ppl[C[x]].size() != legit[C[x]]) sum = LLINF;
      |     ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
capital_city.cpp:63:43: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
   63 |   if(ppl[C[x]].size() && ppl[C[x]].size() != legit[C[x]]) sum = LLINF;
      |                          ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 9808 KB Output is correct
2 Correct 6 ms 9736 KB Output is correct
3 Correct 6 ms 9732 KB Output is correct
4 Correct 6 ms 9676 KB Output is correct
5 Correct 6 ms 9804 KB Output is correct
6 Correct 6 ms 9676 KB Output is correct
7 Incorrect 7 ms 9676 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 9808 KB Output is correct
2 Correct 6 ms 9736 KB Output is correct
3 Correct 6 ms 9732 KB Output is correct
4 Correct 6 ms 9676 KB Output is correct
5 Correct 6 ms 9804 KB Output is correct
6 Correct 6 ms 9676 KB Output is correct
7 Incorrect 7 ms 9676 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 903 ms 42788 KB Output is correct
2 Correct 429 ms 43288 KB Output is correct
3 Correct 1020 ms 42560 KB Output is correct
4 Correct 423 ms 43324 KB Output is correct
5 Incorrect 961 ms 39524 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 9808 KB Output is correct
2 Correct 6 ms 9736 KB Output is correct
3 Correct 6 ms 9732 KB Output is correct
4 Correct 6 ms 9676 KB Output is correct
5 Correct 6 ms 9804 KB Output is correct
6 Correct 6 ms 9676 KB Output is correct
7 Incorrect 7 ms 9676 KB Output isn't correct
8 Halted 0 ms 0 KB -