当前位置:网站首页>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;
}
边栏推荐
- FFT is used to estimate the image resampling factor after interpolation
- Overview of database stress testing methods
- Is it safe to buy funds on e fund? Professional answers
- Is it safe for Huatai Securities to open an account online? How to handle it?
- The work of robot engineering and the puzzle of postgraduate entrance examination "volume" supplement
- 【独立站建设】shopify卖家:学会这几点,网上商店销量翻倍!
- Dataframe modifies the value of a row or column position
- y77.第四章 Prometheus大厂监控体系及实战 -- prometheus的服务发现机制(八)
- Zombie's treasure test (enumeration)
- How to do Taobao live broadcast and how to do the anchor to drain and sell products
猜你喜欢

怎么使用宝塔面板把node全栈项目部署到服务器上

flutter 下 grpc list没有Setter 方法 ,如何使用相关属性

4QAM、16QAM 调制与解调仿真电路,观察并分析QAM星座图和误码率曲线【matlab代码】

IP address of the network

Fiddler5+ lightning simulator 4.0 settings for app packet capturing

Prime Ring Problem

销量连连夺冠,五菱的成功秘诀只有低价吗?

P3166 number triangle (tolerance and exclusion +gcd)

Go operation excel library excel use

Three modes of CPU
随机推荐
SVN version control branch and merge function use
2022 love analysis ― bank digitalization practice report
【深入浅出玩转FPGA学习11----Testbench书写技巧2】
旅行 (拆点分层)
Go operation excel library excel use
Digital transformation behind the reshaping growth of catering chain stores
推荐⼀款超好⽤的UI⾃动化⼯具: UiAutomator2!
Overview of database stress testing methods
学习笔记:原码, 反码, 补码
What is a test case? How to design?
“蔚来杯“2022牛客暑期多校训练营2 个人题解集合
B - Krypton Factor (dfs+ backtracking method)
Stack Title: basic calculator
The work of robot engineering and the puzzle of postgraduate entrance examination "volume" supplement
Big view +500 cases, software teams should improve R & D efficiency in this way
leetcode/只出现一次的数字
“蔚来杯“2022牛客暑期多校训练营2 H.[Take the Elevator] 维护线段
大咖观点+500强案例,软件团队应该这样提升研发效能
poj1521
Huawei wireless device WDS configuration command