题号1575 C.难度排名 (并查集知识点)
原创 于 2023-11-02 17:46:46 发布 · 粉丝可见 · 274 阅读 · 0 · 0 · 本内容遵循CC 4.0 BY-SA版权协议 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 GEO检测 · 编辑
文章链接:https://blog.csdn.net/hacker_51/article/details/134187728
题目:

样例1:
cobol<br/>1<br/>4 3<br/>1 4<br/>2 4<br/>3 4<br/> |
样例2:
cobol<br/>1<br/>4 2<br/>1 3<br/>2 3<br/> |
思路:
这题,有两种情况是由矛盾的。
第一种情况:当前题号存在大于两个题号的相连,情况是矛盾的,输出No
第二种情况:出现了 环的形式相连,情况是矛盾的,输出 No
其余都可以蒙混过关。
代码详解如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| #include <iostream> #include <vector> #include <queue> #include <cstring> #include <algorithm> #include <unordered_set> #include <unordered_map> #define endl '\n' #define Yes puts("Yes") #define No puts("No") #define umap unordered_map #define uset unordered_set #define All(x) x.begin(),x.end() #pragma GCC optimize(3,"Ofast","inline") #define IOS std::ios::sync_with_stdio(false),cin.tie(0), cout.tie(0) using namespace std; const int N = 2e6 + 10; int n,m; umap<int,int>p;
inline int Find(int x) { int t = x; while(x != p[x])x = p[x]; p[t] = x; return x; }
inline void Init() { for(int i = 1;i <= n;++i) p[i] = i; } inline void solve() { umap<int,uset<int>>v; cin >> n >> m; Init(); bool st = false; while(m--) { int a,b; cin >> a >> b; v[a].emplace(b); v[b].emplace(a); a = Find(a),b = Find(b); if(a != b) p[a] = b; else { st = true; } } if(st) { No; return ; } for(int i = 1;i <= n;++i) { if(v[i].size() > 2) { No; return ; } } Yes; } int main() {
IOS; int _t = 1; cin >> _t; while (_t--) { solve(); } return 0; }
|
最后提交:
