当前位置:网站首页>E. Split into two sets
E. Split into two sets
2022-07-27 08:40:00 【Cherry blossom with two petals and Seven Mile fragrance】
Topic link : Portal

The main idea of the topic : Yes n Set dominoes , Each set of cards has two numbers a,b,(1<=a,b<=n), Ask if you can put this n Group dominoes are divided into two groups , Make the numbers on each group of dominoes different , And all dominoes can be assigned ( That is, the number of all dominoes in a group is 1~n).
Ideas : There are many ways to solve this problem , Here are two , One is to use Type and search do , One is dyeing Bipartite graph Composition
One 、 Type and search
The basic query set maintenance is One Relationship , If a friend's friend is a friend , And category and search set can be maintained Multiple Relationship , If the enemy of the enemy is a friend , There is a more hostile relationship , Generally, it can be achieved by expanding and querying the scale ;
Such as : 1 2 3 4, Four people , 1,2 2,3 It's a friend. , Then we can know through the joint search set connection 1,3 It's a friend.
If we want to add the enemy relationship , We can Double the size of the search set , Then for 1 2 3 4 1+4 2+4 3+4 4+4
namely The first four 1 2 3 4 Keep friends , The last four 5 6 7 8 For the enemy relationship , So if 1,2 For the sake of hostility , Even 1,2+4 and 1+4, 2 Two sides , To build a hostile relationship , such , When 2,3 When it's a hostile relationship , You will find 1,3 Connected indirectly , That is, the enemy of the enemy is a friend , It completes two kinds of joint search

Then for this problem, we can establish two categories and search the set , A category is a group to carry out and search set construction , For example 5
2 1 1 2 4 3 4 3 5 6 5 7 8 6 7 8
Put one domino into a set at a time , Such as 1,2 Put in collection 1, 1+n, 2+n Then put into the set 2, This appears for the second time 1,2 When You can judge , 1,2 Appear repeatedly in the same set
The code is as follows :
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <iterator>
#include <unordered_set>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <sstream>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <iomanip>
#include <bitset>
#include <unordered_map>
using namespace std;
stringstream ss;
#define endl "\n"
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<pair<int, int>, int> PIII;
const int N = 3e5+10, M = 20, mod = 1e9+7;
const int INF = 0x3f3f3f3f;
int n,q;
int p[2*N]; // Double the size of search set
int find(int x)
{
if(x != p[x]) p[x] = find(p[x]);
return p[x];
}
void merge(int x,int y)
{
int px = find(x);
int py = find(y);
if(px != py) p[px] = py;
}
void solve()
{
cin>>n;
map<int, int> mp; // Record the number of times
for(int i = 1; i<=2*n; i++) p[i] = i;
int f = 1;
for(int i = 1; i<=n; i++)
{
int a,b;
cin>>a>>b;
mp[a]++, mp[b]++;
if(mp[a]>2 || mp[b]>2) f = 0;
if(a == b) f = 0;
int x = a, y = b, dx = a+n, dy = b+n; // x,y In a collection , dx,dy In another set
if(find(x) == find(y)) f = 0;
else
{
merge(x, dy);
merge(y, dx);
}
}
if(f) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
int main()
{
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
// solve();
int T;
cin>>T;
while(T -- )
{
solve();
}
}
Two 、 Bipartite graph
Think of each domino as a two-way side connection , For example 5
2 1 1 2 4 3 4 3 5 6 5 7 8 6 7 8
The picture completed after the connection is {1,2}-{2,1}, {3,4}-{3,4}, {5,6}-{6,8}-{8,7}-{7,5} Three rings It is not difficult to find that when the ring is even , We can put it in a set through taking , Such as {5,6}-{6,8}-{8,7}-{7,5}, take {5,6},{8,7} Put it in a set , The other two are placed in another set ( That is to build a bipartite graph ), In this way, there will be no repetition in two sets , And if there are odd rings , There must be repetition when taking cards , So after building the map , Judge whether there is an odd ring .
The code is as follows :
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <iterator>
#include <unordered_set>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <sstream>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <iomanip>
#include <bitset>
#include <unordered_map>
using namespace std;
stringstream ss;
#define endl "\n"
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<pair<int, int>, int> PIII;
const int N = 3e5+10, M = 20, mod = 1e9+7;
const int INF = 0x3f3f3f3f;
int n,q;
int col[N];
vector<int> v[N];
int ok = 1;
void dfs(int u)
{
for(auto t : v[u])
{
if(!col[t])
{
col[t] = -col[u];
dfs(t);
}else if(col[t] == col[u]) ok = 0;
}
}
void solve()
{
cin>>n;
map<int, int> mp; // Record the number of times
for(int i = 1; i<=n; i++) v[i].clear(), col[i] = 0;
int f = 1;
for(int i = 1; i<=n; i++)
{
int a,b;
cin>>a>>b;
mp[a]++, mp[b]++;
if(mp[a]>2 || mp[b]>2) f = 0;
if(a == b) f = 0;
v[a].push_back(b);
v[b].push_back(a);
}
if(!f)
{
cout<<"NO"<<endl;
return;
}
ok = 1;
for(int i = 1; i<=n; i++)
{
if(!col[i]) col[i] = 1, dfs(i); // Dyeing to judge parity
if(!ok)
{
cout<<"NO"<<endl;
return;
}
}
cout<<"YES"<<endl;
}
int main()
{
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
// solve();
int T;
cin>>T;
while(T -- )
{
solve();
}
}
边栏推荐
- 海量数据肖枫:共建共治openGauss根社区,共享欣欣向荣新生态
- Oppo self-developed large-scale knowledge map and its application in digital intelligence engineering
- Use of elastic box / expansion box (Flex)
- 3311. Longest arithmetic
- ROS2安装时出现Connection failed [IP: 91.189.91.39 80]
- JS advanced knowledge - function
- 691. 立方体IV
- 4278. 峰会
- [penetration test tool sharing] [dnslog server building guidance]
- View 的滑动冲突
猜你喜欢

Minio installation and use
![[BJDCTF2020]EasySearch 1](/img/ea/90ac6eab32c28e09bb1fab62b6b140.png)
[BJDCTF2020]EasySearch 1

MySQL Express

Interviewer: what is scaffolding? Why do you need scaffolding? What are the commonly used scaffolds?

Eval and assert execute one sentence Trojan horse

如何在qsim查看软件对象的实例?

Flink1.15 source code reading Flink clients client execution process (reading is boring)

OSI seven layer model and tcp/ip four layer (TCP and UDP) (notes)

Oppo self-developed large-scale knowledge map and its application in digital intelligence engineering

Chapter 2 foreground data display
随机推荐
带宽 与 货币
开怀一笑
Background image related applications - full, adaptive
Have a good laugh
4276. Good at C
海关总署:这类产品暂停进口
Flutter 渲染机制——GPU线程渲染
Realize SPU management in the background
Using ecological power, opengauss breaks through the performance bottleneck
691. Cube IV
Binglog backup data
Background coupon management
说透缓存一致性与内存屏障
Implementation of registration function
杭州电子商务研究院发布“数字化存在”新名词解释
Realize SKU management in the background
List delete collection elements
How to view instances of software objects in QSIM?
Day4 --- flask blueprint and rest ful
Introduction to depth first search (DFS)