当前位置:网站首页>E. Split into two sets
E. Split into two sets
2022-07-26 01:48:00 【to cling】
Codeforces Round #805 (Div. 3)
Problem
give n A domino , Each domino has two numbers . Ask if you can divide all dominoes into two sets , So that there are no duplicate numbers in each set .
Solution
Bipartite graph :
Definition : Nodes consist of two sets , And there are no edges in two sets .
nature :
- If the dots in two sets are dyed red and blue respectively , It can be found that every edge in the bipartite graph must be connected with a red dot and a blue dot
- There is no ring with odd length in bipartite graph ( Only walk an even number of times can you return to a set )
Type and search ( Extended domain )
Extended domain can be referred to 《 The food chain 》 This kind of problem . Portal
Generally, the maintenance of joint search sets is only connectivity and transitivity ( A friend's friend is a friend )
Categories and search sets are used to maintain , The enemy of the enemy is a friend .Suppose you want to maintain a union search set with only four elements (n = 4):
- Double the array :1-4 What we maintain is the relationship of friends ,5-8 Maintain a hostile relationship

- If 1,2 It's the enemy , be :
merge(1, 2 + n), merge(2, 1 + n);
If 2, 4 It's the enemy , be :merge(2, 4 + n), merge(4, 2 + n);
- Looking at the picture above, we can see :1 and 4 adopt 2 + n This point is connected , Form a friendship
- Double the array :1-4 What we maintain is the relationship of friends ,5-8 Maintain a hostile relationship
Code
const int N = 4e5 + 5, M = 1e6 + 7;
int f[N], a[N];
int fd(int x)
{
if (x == f[x]) return x;
return f[x] = fd(f[x]);
}
void merge(int x, int y)
{
int fx = fd(x), fy = fd(y);
if (fx != fy) f[fx] = fy;
}
int main()
{
IOS;
int T; cin >> T;
while (T--)
{
int n; cin >> n;
for (int i = 1; i <= n * 2; i++) f[i] = i, a[i] = 0;
int flag = 1;
for (int i = 1, x, y; i <= n; i++)
{
cin >> x >> y;
a[x]++; a[y]++;
if (x == y) flag = 0;
if (fd(x) == fd(y)) flag = 0;
else
{
int dx = x + n, dy = y + n;
merge(x, dy);
merge(dx, y);
}
}
for (int i = 1; i <= n && flag; i++)
if (a[i] != 2) flag = 0;
if (flag) cout << "YES\n";
else cout << "NO\n";
}
return 0;
}
边栏推荐
- Cross Site Request Forgery (CSRF): impact, examples, and Prevention
- Fiddler5+ lightning simulator 4.0 settings for app packet capturing
- Speech comprehension center comprehension summary
- [Unity] 二维洞穴地图随机生成
- How does Flink SQL configure to print the insert parameter log
- 【深入浅出玩转FPGA学习11----Testbench书写技巧1】
- FFT is used to estimate the image resampling factor after interpolation
- Mysql_ Note1
- Zombie's treasure test (enumeration)
- “蔚来杯“2022牛客暑期多校训练营2 个人题解集合
猜你喜欢

What should I do when my blog is attacked by hackers?

3、 Pinda general permission system__ pd-tools-swagger2

Speech comprehension - structural analysis exercise of fragment reading

Overview of database stress testing methods

网络之二三层转发

Digital transformation behind the reshaping growth of catering chain stores

How to use the pagoda panel to deploy the full stack project of node to the server

推荐系统-协同过滤在Spark中的实现

Typora expiration solution, what if typora can't open
![Niuke - bm39 serialized binary tree [hard]](/img/c4/f14fe8488bbf28689fa3f02cdf4dae.png)
Niuke - bm39 serialized binary tree [hard]
随机推荐
The SQL script generated by powerdispatcher model runs incorrectly
4QAM, 16QAM modulation and demodulation simulation circuit, observe and analyze QAM constellation and bit error rate curve [matlab code]
"Weilai Cup" 2022 Niuke summer multi school training camp 2 g.[link with monotonic subsequence] block structure
Cross Site Request Forgery (CSRF): impact, examples, and Prevention
leetcode/只出现一次的数字
【Verilog数字系统设计(夏宇闻)3-----Verilog语法的基本概念1】
How to modify Oracle functions?
“蔚来杯“2022牛客暑期多校训练营2 H.[Take the Elevator] 维护线段
Leetcode/ numbers that appear only once
Analysis of zeromq
The e-commerce project is written in the resume. How to answer it during the interview
Speech comprehension - structural analysis exercise of fragment reading
Pt onnx ncnn conversion problem record (followed by yolov5 training)
Quickly create a topic folder
Big view +500 cases, software teams should improve R & D efficiency in this way
大咖观点+500强案例,软件团队应该这样提升研发效能
IP address of the network
图像批处理高斯滤波降噪+峰值信噪比计算
In spark SQL, date is used to display the day of the week according to the year, month and day_ format(date,‘u‘)
Mapbox GL JS active map refresh method