unordered_set

基本操作:

 1 #include<iostream>
 2 #include<unordered_set>
 3 using namespace std;
 4 
 5 unordered_set<int> s;
 6 
 7 int main(){
 8     int n = 10;
 9     for(int i = 1 ; i <= n ; i++){
10         s.insert(3);
11     }
12     s.insert(4);
13     cout << (int)s.size() << endl;
14     cout << (int)s.count(3) << endl;
15     cout << *s.find(3) << endl;
16     int sz = s.count(3);
17     while(sz){
18         s.erase(3);
19         sz--;
20     }
21     cout << *s.begin() << endl;
22     
23     if(s.find(3) == s.end()){
24         cout << "Not Found" << endl;
25     }
26     
27     
28     return 0;
29 }

运行结果:

2

1

3

4

Not Found

CF 1349B

  1 #include<iostream>
  2 #include<cstring>
  3 #include<string>
  4 #include<cstdio>
  5 #include<cmath>
  6 #include<queue>
  7 #include<unordered_set>
  8 #include<vector>
  9 #define ll long long
 10 using namespace std;
 11 
 12 const int N = 1e5 + 10;
 13 
 14 int n, m, k;
 15 int deg[N];//度数 
 16 bool used[N];//队列内元素 
 17 unordered_set<int> s[N];
 18 
 19 int read(){
 20     char ch=getchar();int x=0,f=1;
 21     while(ch<'0' || ch>'9')    {if(ch=='-')f=-1;ch=getchar();}
 22     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
 23     return x*f;
 24 }
 25 
 26 void init()
 27 {
 28     for(int i = 1 ; i <= n ; i++){
 29         used[i] = false;
 30         deg[i] = 0;
 31         s[i].clear();
 32     }
 33 } 
 34 
 35 inline bool check(int u)
 36 {
 37     for(register int v : s[u]){
 38         for(register int w : s[u]){
 39             if(v == w)    continue;
 40             if(!s[v].count(w))    return false;
 41         }
 42     }
 43     return true;
 44 }
 45 
 46 int main(){
 47     ios::sync_with_stdio(0); cin.tie(0);
 48     int T = read();
 49     while(T--)
 50     {
 51         n = read();
 52         m = read();
 53         k = read();
 54         init();
 55         
 56         for(int i = 1 ; i <= m ; i++){
 57             int u = read(), v = read();
 58             s[u].insert(v);
 59             s[v].insert(u);
 60             deg[u]++;
 61             deg[v]++;
 62         }
 63         if(1ll * k * (k - 1) > (m << 1)){//边的数量不合法 
 64             cout << -1 << endl;
 65             continue;
 66         }
 67         if(k == 1){//特判 
 68             cout << 2 << endl << 1 << endl;
 69             continue;
 70         }
 71         queue<int> q;
 72         for(int i = 1 ; i <= n ; i++){
 73             if(deg[i] < k){
 74                 q.push(i);
 75                 used[i] = true;
 76             }
 77         }
 78         bool flag = false;
 79         while(!q.empty()){
 80             int u = q.front();
 81             q.pop();
 82             if(deg[u] == k - 1){
 83                 if(check(u)){
 84                     cout << 2 << endl;
 85                     for(auto v : s[u])    cout << v << ' ';
 86                     cout << u << endl;
 87                     flag = true;
 88                     break;
 89                 }
 90             }
 91             while(!s[u].empty()){
 92                 int v = *s[u].begin();
 93                 s[u].erase(v);
 94                 s[v].erase(u);
 95                 deg[u]--;
 96                 deg[v]--;
 97                 if(deg[v] < k && !used[v]){
 98                     q.push(v);
 99                     used[v] = true;//放进队列等待处理 
100                 }
101             }
102         }
103         if(flag)    continue;
104         vector<int> ans;
105         for(register int i = 1 ; i <= n ; i++){
106             if(!used[i])    ans.push_back(i);
107         }
108         if(ans.empty()){
109             cout << -1 << endl;
110         }else{
111             cout << 1 << " " << (int)ans.size() << endl;
112             for(auto u : ans){
113                 cout << u << " ";
114             }
115             cout << endl;
116         }
117     }
118     return 0;
119 }
原文地址:https://www.cnblogs.com/ecustlegendn324/p/14005914.html