Submission #1057689

# Submission time Handle Problem Language Result Execution time Memory
1057689 2024-08-14T03:22:47 Z thieunguyenhuy ICC (CEOI16_icc) C++17
100 / 100
74 ms 644 KB
#ifndef hwe
	#include "icc.h"
#endif
 
#include <bits/stdc++.h>
using namespace std;
 
#define popcount(n) (__builtin_popcountll((n)))
#define clz(n) (__builtin_clzll((n)))
#define ctz(n) (__builtin_ctzll((n)))
#define lg(n) (63 - __builtin_clzll((n)))
#define BIT(n, i) (((n) >> (i)) & 1ll)
#define MASK(i) (1ll << (i))
#define FLIP(n, i) ((n) ^ (1ll << (i)))
#define ON(n, i) ((n) | MASK(i))
#define OFF(n, i) ((n) & ~MASK(i))
 
#define Int __int128
#define fi first
#define se second
 
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<long long, int> pli;
typedef pair<int, long long> pil;
typedef vector<pair<int, int>> vii;
typedef vector<pair<long long, long long>> vll;
typedef vector<pair<long long, int>> vli;
typedef vector<pair<int, long long>> vil;
 
template <class T1, class T2>
bool maximize(T1 &x, T2 y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}
template <class T1, class T2>
bool minimize(T1 &x, T2 y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}
 
template <class T>
void remove_duplicate(vector<T> &ve) {
    sort (ve.begin(), ve.end());
    ve.resize(unique(ve.begin(), ve.end()) - ve.begin());
}
 
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
template <class T> T random(T l, T r) {
    return uniform_int_distribution<T>(l, r)(rng);
}
template <class T> T random(T r) {
    return rng() % r;
}
 
const int N = 100 + 5;
const int MOD = 1e9 + 7;
const int inf = 1e9;
const ll INF = 1e18;
 
#ifdef hwe
int n, current_road = 0;
map<int, int> mp[N];
pii roads[N];

void printArr(int sz, int a[]) {
	cerr << "Array: ";
	for (int i = 0; i < sz; ++i) cerr << a[i] << ' ';
	cerr << '\n';
}

int query(int sza, int szb, int a[], int b[]) {
	// cerr << "Query a: ";
	// for (int i = 0; i < sza; ++i) cerr << a[i] << ' ';
	// cerr << '\n';
	// cerr << "Query b: ";
	// for (int i = 0; i < szb; ++i) cerr << b[i] << ' ';
	// cerr << '\n';
 
	for (int i = 0; i < sza; ++i) for (int j = 0; j < szb; ++j) {
		int u = a[i], v = b[j];
		if (u > v) swap(u, v);
		if (mp[u][v] == 1) return 1;
	}
	return 0;
}
void setRoad(int u, int v) {
	if (u > v) swap(u, v);
	cerr << "Set road: " << u << ' ' << v << '\n';
	++current_road;
	mp[roads[current_road].fi][roads[current_road].se] = 1;
}
#endif
 
int a[N], b[N];
vector<int> S[N], tmpS[N];
 
void run(int n) {
	int num_components = n;
	for (int i = 0; i < num_components; ++i) S[i] = {i + 1};
 
	int sza = 0, szb = 0;
	for (int _ = 1; _ < n; ++_) {
		// for (int i = 0; i < num_components; ++i) swap(tmpS[i], S[i]);
		// vector<int> perm(num_components); iota(perm.begin(), perm.end(), 0);
		// shuffle(perm.begin(), perm.end(), rng);
		// for (int i = 0; i < num_components; ++i) swap(S[perm[i]], tmpS[i]);
		sort (S, S + num_components, [&](const vector<int> &A, const vector<int> &B) {
			return A.size() < B.size();
		});

		int Xor = 0, diff = -1;
		for (int bit = 0; MASK(bit) < num_components; ++bit) {
			vector<int> ones, zeros;
			for (int i = 0; i < num_components; ++i) {
				if (BIT(i, bit)) ones.emplace_back(i);
				else zeros.emplace_back(i);
			}
 
 			sza = szb = 0;
			for (auto &x : zeros) for (auto &y : S[x]) {
				a[sza++] = y;
			}
			for (auto &x : ones) for (auto &y : S[x]) {
				b[szb++] = y;
			}
 
			int tmp = query(sza, szb, a, b);
			if (tmp == 1) Xor = ON(Xor, bit), diff = bit;
		}
		
		int cA = 0, cB = MASK(diff);
		for (int bit = 0; MASK(bit) < num_components; ++bit) {
			if (bit == diff) continue;
			if (BIT(Xor, bit)) {
				vector<int> cmp1, cmp2;
				for (int i = 0; i < num_components; ++i) {
					if (!BIT(i, diff) && !BIT(i, bit)) cmp1.emplace_back(i);
					if (BIT(i, diff) && BIT(i, bit)) cmp2.emplace_back(i);
				}

				sza = szb = 0;
				for (auto &x : cmp1) for (auto &y : S[x]) {
					a[sza++] = y;
				}
				for (auto &x : cmp2) for (auto &y : S[x]) {
					b[szb++] = y;
				}

				if (query(sza, szb, a, b)) cB = ON(cB, bit);
				else cA = ON(cA, bit);
			}
			else {
				vector<int> cmp1, cmp2;
				for (int i = 0; i < num_components; ++i) {
					if (!BIT(i, diff)  && BIT(i, bit)) cmp1.emplace_back(i);
					if (BIT(i, diff) && BIT(i, bit)) cmp2.emplace_back(i);
				}

				sza = szb = 0;
				for (auto &x : cmp1) for (auto &y : S[x]) {
					a[sza++] = y;
				}
				for (auto &x : cmp2) for (auto &y : S[x]) {
					b[szb++] = y;
				}

				if (query(sza, szb, a, b)) cA = ON(cA, bit), cB = ON(cB, bit);
			}
		}

		sza = szb = 0;
		for (auto &x : S[cA]) a[sza++] = x;
		for (auto &x : S[cB]) b[szb++] = x;

		shuffle(a, a + sza, rng);
		shuffle(b, b + szb, rng);

		int low = 1, high = sza - 1, pu = sza, pv = szb, u = -1, v = -1;
		while (low <= high) {
			int mid = low + high >> 1;
			if (query(mid, szb, a, b)) high = (pu = mid) - 1;
			else low = mid + 1;
		}
		u = a[pu - 1];

		low = 1, high = szb - 1;
		while (low <= high) {
			int mid = low + high >> 1;
			if (query(sza, mid, a, b)) high = (pv = mid) - 1;
			else low = mid + 1;
		}
		v = b[pv - 1];
 
		setRoad(u, v);
 
		int p = -1, q = -1;
		for (int i = 0; i < num_components; ++i) for (auto &x : S[i]) {
			if (x == u) p = i;
			if (x == v) q = i;
		}
 
		for (auto &x : S[q]) S[p].emplace_back(x);
		swap(S[q], S[num_components - 1]); --num_components;
	}
}
 
#ifdef hwe
signed main() {
	cin >> n;
 
	for (int i = 0; i < n - 1; ++i) {
		int u, v; cin >> u >> v;
		if (u > v) swap(u, v);
		roads[i] = make_pair(u, v);
	}
 
	current_road = 0; mp[roads[0].fi][roads[0].se] = 1;
	run(n);
 
    cerr << '\n'; return 0;
}
#endif

Compilation message

icc.cpp: In function 'void run(int)':
icc.cpp:190:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  190 |    int mid = low + high >> 1;
      |              ~~~~^~~~~~
icc.cpp:198:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  198 |    int mid = low + high >> 1;
      |              ~~~~^~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 604 KB Ok! 117 queries used.
2 Correct 3 ms 604 KB Ok! 117 queries used.
# Verdict Execution time Memory Grader output
1 Correct 21 ms 604 KB Ok! 648 queries used.
2 Correct 17 ms 644 KB Ok! 539 queries used.
3 Correct 20 ms 644 KB Ok! 557 queries used.
# Verdict Execution time Memory Grader output
1 Correct 69 ms 640 KB Ok! 1560 queries used.
2 Correct 60 ms 604 KB Ok! 1330 queries used.
3 Correct 65 ms 616 KB Ok! 1613 queries used.
4 Correct 61 ms 604 KB Ok! 1460 queries used.
# Verdict Execution time Memory Grader output
1 Correct 63 ms 644 KB Ok! 1573 queries used.
2 Correct 65 ms 624 KB Ok! 1477 queries used.
3 Correct 65 ms 640 KB Ok! 1485 queries used.
4 Correct 65 ms 600 KB Ok! 1604 queries used.
# Verdict Execution time Memory Grader output
1 Correct 59 ms 600 KB Ok! 1411 queries used.
2 Correct 60 ms 600 KB Ok! 1428 queries used.
3 Correct 58 ms 604 KB Ok! 1388 queries used.
4 Correct 60 ms 604 KB Ok! 1425 queries used.
5 Correct 63 ms 600 KB Ok! 1538 queries used.
6 Correct 74 ms 600 KB Ok! 1376 queries used.
# Verdict Execution time Memory Grader output
1 Correct 59 ms 600 KB Ok! 1461 queries used.
2 Correct 61 ms 604 KB Ok! 1415 queries used.