Comments

my submission

can someone hacks me with my submission? I first find all points with at least 4 degrees and use union-find set to find the maximum number with 4 degrees in the subgraph and then calculate the ans.

#include<bits/stdc++.h>
using namespace std;
using u32 = unsigned;
using ll = long long;
using u64 = unsigned long long;
using ld = long double;

#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define fill(x, y) memset(x, y, sizeof(x))
#define si(x) ((int)(x).size())
#define dbg(x) cerr << #x << ": " << (x) << " "
#define debug(x) cerr << #x << ": " << (x) << endl

#define PI (ld)2 * acos(0.0)
#define MOD 998'244'353
#define MOD2 1'000'000'007

#define endl '\n'
#define YES cout << "YES" << endl; return
#define Yes cout << "Yes" << endl; return
#define yes cout << "yes" << endl; return
#define YE cout << "YES" << endl
#define Ye cout << "Yes" << endl
#define NO cout << "NO" << endl; return
#define No cout << "No" << endl; return
#define no cout << "no" << endl; return
#define IPSB cout << -1 << endl; return

const int inf = 0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fll;

#define int ll

class DSU {
  public:
	vector<int> parent, rank, size;

	DSU(int n) {
		parent.resize(n);
		rank.resize(n, 1);
		size.resize(n, 1);
		for (int i = 0; i < n; i++)
			parent[i] = i;
	}

	int find(int x) {
		if (parent[x] != x) {
			parent[x] = find(parent[x]);
		}
		return parent[x];
	}

	void unite(int x, int y) {
		int xr = find(x), yr = find(y);
		if (xr == yr)
			return;
		if (rank[xr] > rank[yr]) {
			parent[yr] = xr;
			size[xr] += size[yr];
		} else if (rank[xr] < rank[yr]) {
			parent[xr] = yr;
			size[yr] += size[xr];
		} else {
			parent[yr] = xr;
			rank[xr]++;
			size[xr] += size[yr];
		}
	}

	int get_size(int x) {
		return size[find(x)];
	}
};

void solve() {
	int N;
	cin >> N;

	vector<int> degree(N, 0);
	vector<pair<int, int>> edges;

	for (int i = 0; i < N - 1; i++) {
		int A, B;
		cin >> A >> B;
		A--;
		B--; // 0-based index
		edges.emplace_back(A, B);
		degree[A]++;
		degree[B]++;
	}

	vector<bool> is_high_degree(N, false);
	int flag = 0;
	for (int i = 0; i < N; i++) {
		if (degree[i] >= 4) {
			is_high_degree[i] = true;
			flag = 1;
		}
	}

	DSU dsu(N);
	bool has_cluster = false;

	for (auto [A, B] : edges) {
		if (is_high_degree[A] && is_high_degree[B]) {
			dsu.unite(A, B);
			has_cluster = true;
		}
	}

	if (!has_cluster) {
		if (flag == 0) {
			cout << -1 << endl;
		} else {
			cout << 5 << endl;
		}
	} else {
		int max_size = 1;
		map<int, int> component_size;
		for (int i = 0; i < N; i++) {
			if (is_high_degree[i]) {
				int root = dsu.find(i);
				component_size[root] = dsu.get_size(root);
				max_size = max(max_size, component_size[root]);
			}
		}
		int ans = max_size * 3 + 2;
		assert(ans <= N && max_size >= 2);
		cout << ans << endl;
	}

	return;
}

signed main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
/*
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	freopen("error.txt", "w", stderr);
	#endif
*/
	int ttest = 1;
//	cin >> ttest;
	while (ttest--) {
		solve();
	}
//	cerr << "Time:" << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << " ms" << endl;
	return 0;
}

Can anyone help me find out why my 273201975 on 1991E - Coloring Game received a WA verdict, Checker comment is "wrong answer Integer 3 violates the range [1, 1] (test case 19)" :)