Codeforces 468 B Two Sets

Two Sets

题意:就是将一对数放进setA, setB中, 如果放进setA的话要求满足 x与a-x都在这个集合里面, 如果放进setB中要求满足x与b-x都在这个集合中。

题解:我们将能放进B的元素优先放在B中,如果能放进B就直接将2个元素放进B中。 然后如果能放进A中就放进A中, 如果在放入A过程中发现对应元素在setB中,那么就找对应元素的对应元素能不能放进A中。如果在元素放入B的过程中发现对应元素在A中,那么就无解了,因为优先放B中。

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define LL long long
 4 #define ULL unsigned LL
 5 #define fi first
 6 #define se second
 7 #define lson l,m,rt<<1
 8 #define rson m+1,r,rt<<1|1
 9 #define max3(a,b,c) max(a,max(b,c))
10 const int INF = 0x3f3f3f3f;
11 const LL mod = 1e9+7;
12 typedef pair<int,int> pll;
13 const int N = 1e5+5;
14 int vis[N], pi[N];
15 map<int, int> m;
16 int n, a, b;
17 bool dfs(int v, int c){
18     if(c == 2){
19         if(vis[m[b-v]] == -1){
20             vis[m[b-v]] = vis[m[v]] = 1;
21             return true;
22         }
23         if(vis[m[a-v]] == -1){
24             vis[m[a-v]] = vis[m[v]] = 0;
25             return true;
26         }
27         if(vis[m[a-v]] == 1){
28             vis[m[a-v]] = vis[m[v]] = 0;
29             return dfs(b-a+v,1);
30         }
31         return false;
32     }
33     else if(c == 1){
34         if(vis[m[a-v]] == -1 || vis[m[a-v]] == 0){
35             vis[m[a-v]] = vis[m[v]] = 0;
36             return true;
37         }
38         if(vis[m[a-v]] == 1){
39             vis[m[a-v]] = vis[m[v]] = 0;
40             return dfs(b-a+v,1);
41         }
42         return false;
43     }
44     return false;
45 }
46 int main(){
47     ios::sync_with_stdio(false);
48     cin.tie(0);
49     cout.tie(0);
50     cin >> n >> a >> b;
51     vis[0] = 3;
52     for(int i = 1; i <= n; i++){
53         vis[i] = -1;
54         cin >> pi[i];
55         m[pi[i]] = i;
56     }
57     for(int i = 1; i <= n; i++){
58         if(vis[i] == -1){
59             if(dfs(pi[i],2)) ;
60             else {
61             cout << "NO
";
62             return 0;
63             }
64         }
65     }
66     cout << "YES
";
67     for(int i = 1; i < n; i++)
68         cout << vis[i] << ' ';
69     cout << vis[n] << endl;
70     return 0;
71 }
原文地址:https://www.cnblogs.com/MingSD/p/8470786.html