Codeforces Round 805 (Div. 3)
原题链接:Codeforces Round 805 (Div. 3)
A
题意:
思路:
B
题意:
思路:
C
题意:
思路:
D
题意:
思路:
E
题意:判定二部图,并且\(deg \le 2\)
思路:BFS DSU jiangly判断奇环(仅限于\(deg \le 2\) )
#include <bits/stdc++.h>
using i64 = long long;
void solve() {
int n;
std::cin >> n;
std::vector<std::vector<int>> adj(n);
for (int i = 0, u, v; i < n; ++i) {
std::cin >> u >> v;
--u, --v;
adj[u].push_back(v);
adj[v].push_back(u);
}
std::vector<int> f(n, -1);
for (int i = 0; i < n; ++i) {
if (f[i] != -1) {
continue;
}
std::queue<int> q;
q.push(i);
f[i] = 0;
while (!q.empty()) {
auto u = q.front();
q.pop();
if (adj[u].size() > 2) {
std::cout << "NO\n";
return;
}
for (auto v : adj[u]) {
if (f[v] == -1) {
f[v] = 1 ^ f[u];
q.push(v);
} else if (f[v] == f[u]) {
std::cout << "NO\n";
return;
}
}
}
}
std::cout << "YES\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
solve();
}
}
#include <bits/stdc++.h>
using i64 = long long;
struct DSU {
std::vector<int> f, siz;
DSU() {}
DSU(int n) {
init(n);
}
void init(int n) {
f.resize(n);
std::iota(f.begin(), f.end(), 0);
siz.assign(n, 1);
}
int find(int x) {
while (x != f[x]) {
x = f[x] = f[f[x]];
}
return x;
}
bool same(int x, int y) {
return find(x) == find(y);
}
bool merge(int x, int y) {
x = find(x);
y = find(y);
if (x == y) {
return false;
}
siz[x] += siz[y];
f[y] = x;
return true;
}
int size(int x) {
return siz[find(x)];
}
};
void solve() {
int n;
std::cin >> n;
DSU dsu(2 * n);
std::vector<int> deg(n);
for (int i = 0, u, v; i < n; ++i) {
std::cin >> u >> v;
--u, --v;
++deg[u], ++deg[v];
dsu.merge(u, v + n);
dsu.merge(v, u + n);
}
for (int i = 0; i < n; ++i) {
if (deg[i] > 2 || dsu.same(i, i + n)) {
std::cout << "NO\n";
return;
}
}
std::cout << "YES\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
solve();
}
}
#include <bits/stdc++.h>
using i64 = long long;
void solve() {
int n;
std::cin >> n;
std::vector<std::vector<int>> adj(n);
for (int i = 0; i < n; i++) {
int u, v;
std::cin >> u >> v;
u--;
v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 0; i < n; i++) {
if (adj[i].size() > 2) {
std::cout << "NO\n";
return;
}
}
std::vector<bool> vis(n);
for (int i = 0; i < n; i++) {
if (!vis[i] && adj[i].size() == 2) {
int j = i, k = adj[i][0];
int len = 0;
while (!vis[j] && adj[j].size() == 2) {
vis[j] = true;
len++;
k ^= adj[j][0] ^ adj[j][1];
std::swap(j, k);
}
if (j == i && len % 2 == 1) {
std::cout << "NO\n";
return;
}
}
}
std::cout << "YES\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}
F
题意:容器题,B中元素每次可以 乘除2,问B能否等于A
思路:从二进制视角看,1是只减不增的。AB相等保证高位的1相等
消去末尾的0,AB比配,匹配不上的B消去末尾一个1,接着向下匹配。
#include <bits/stdc++.h>
using i64 = long long;
void solve() {
int n;
std::cin >> n;
std::map<int, int> cnt;
std::vector<int> a(n), b(n);
for (int i = 0; i < n; ++i) {
std::cin >> a[i];
int x = a[i] >> __builtin_ctz(a[i]);
--cnt[x];
}
for (int i = 0; i < n; ++i) {
std::cin >> b[i];
int x = b[i] >> __builtin_ctz(b[i]);
++cnt[x];
}
while (not cnt.empty()) {
auto [k, v] = *cnt.rbegin();
cnt.erase(cnt.rbegin()->first);
if (v == 0) {
continue;
}
if (v < 0) {
std::cout << "NO\n";
return;
}
int x = k >> (__builtin_ctz(k >> 1) + 1);
cnt[x] += v;
}
std::cout << "YES\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
solve();
}
}
G
题意:
思路:

浙公网安备 33010602011771号