ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

CF1228D Complete Tripartite

2019-12-10 15:53:04  阅读:239  来源: 互联网

标签:begin end Complete int res CF1228D back Tripartite 集合


思路:

任选一点a,和a没有边直接相连的点一定和a在同一个集合,由此构造得到一个集合A。用类似的方法再构造一个集合B,并将剩下的点放在集合C中,就得到了三个集合A,B,C。再检查A,B,C是否符合要求即可。

实现:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N = 100005;
 4 vector<int> G[N];
 5 int res[N];
 6 int main()
 7 {
 8     int n, m;
 9     while (cin >> n >> m)
10     {
11         for (int i = 1; i <= n; i++) { res[i] = 0; G[i].clear(); }
12         int a, b;
13         for (int i = 0; i < m; i++)
14         {
15             cin >> a >> b;
16             G[a].push_back(b);
17             G[b].push_back(a);
18         }
19         res[1] = 1;
20         set<int> st{G[1].begin(), G[1].end()};
21         for (int i = 2; i <= n; i++)
22         {
23             if (!st.count(i)) res[i] = 1;
24         }
25         int i = 2;
26         for ( ; i <= n; i++)
27         {
28             if (res[i] != 1) break;
29         }
30         res[i] = 2;
31         set<int> st2{G[i].begin(), G[i].end()};
32         for (int j = 2; j <= n; j++)
33         {
34             if (j == i || res[j] == 1) continue;
35             if (st2.count(j)) res[j] = 3;
36             else res[j] = 2;
37         }
38         vector<int> c(4, 0);
39         for (int i = 1; i <= n; i++) c[res[i]]++;
40         bool flg = true;
41         if (!c[1] || !c[2] || !c[3]) flg = false;
42         if (m != c[1] * c[2] + c[2] * c[3] + c[3] * c[1]) flg = false;
43         for (int i = 1; i <= n; i++)
44         {
45             int p = res[i];
46             for (auto it: G[i])
47             {
48                 if (res[it] == p) { flg = false; break; }
49             }
50             if (!flg) break;
51         }
52         if (!flg) { cout << -1 << endl; continue; }
53         for (int i = 1; i <= n; i++) cout << res[i] << " ";
54         cout << endl;
55     }
56     return 0;
57 }

标签:begin,end,Complete,int,res,CF1228D,back,Tripartite,集合
来源: https://www.cnblogs.com/wangyiming/p/12017119.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有