列表

详情


NC232090. Klee and Bomb

描述

Klee is developing a new type of bomb called Peng Peng Bomb!
There are n bombs numbered from 1 to n, bomb i has color c_i. The n bombs are connected with m links, each link connects two different bombs. Bomb x will explode if meets one of the following conditions:
Klee can choose exact 1 bomb to be on fire. You can change at most 1 bomb x and recolor it (i.e. change c_x to any color you want).
Please help Klee find the maximum number of bombs can be exploded.


输入描述

The first line of input contains two integers  and  --- the number of bombs and links respectively.
The second line contains n integers --- the color of each bomb.
In the next m lines, each line contains two integers --- two bombs which is connected by the link.
It is guaranteed that for any pair of bombs (x, y), there are at most 1 link between them.

输出描述

You should output an integer --- the maximum number of bombs can be exploded.

示例1

输入:

5 3
1 1 2 1 2
1 2
2 3
3 4

输出:

4

说明:

In the example 1, if you change c_3 to 1 and Klee choose any one of [1,2,3,4] to be on fire, 4 bombs will explode.

示例2

输入:

4 6
1 2 1 2
1 2
2 3
3 4
4 1
1 3
2 4

输出:

3

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 236ms, 内存消耗: 22312K, 提交时间: 2021-12-18 14:53:43

#include <bits/stdc++.h>
using namespace std;
using LL = long long;
constexpr LL mod = 998244353;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n, m;
	cin >> n >> m;
	vector<int> c(n + 1), p(n + 1, -1);
	for (int i = 1; i <= n; i += 1)
		cin >> c[i];
	function<int(int)> fp = [&](int u) {
		return p[u] < 0 ? u : p[u] = fp(p[u]);
	};
	vector<vector<int>> G(n + 1);
	int ans = 1;
	for (int i = 0, u, v; i < m; i += 1) {
		cin >> u >> v;
		G[u].push_back(v);
		G[v].push_back(u);
		if (c[u] == c[v]) {
			int pu = fp(u), pv = fp(v);
			if (pu != pv) {
				p[pv] += p[pu];
				p[pu] = pv;
				ans = max(ans, -p[pv]);
			}
		}
	}
	for (int i = 1; i <= n; i += 1) {
		set<int> s;
		map<int, int> mp;
		for (int v : G[i]) if (c[v] != c[i]) {
			if (not s.count(fp(v))) {
				s.insert(fp(v));
				mp[c[v]] += -p[fp(v)];
			}
		}
		for (auto [x, y] : mp) ans = max(ans, y + 1);
	}
	cout << ans;
	return 0;
}

上一题