PAT 天梯赛 L2-010. 排座位 【并查集】

mac2022-06-30  31

题目链接

https://www.patest.cn/contests/gplt/L2-010

思路

因为 题意中 朋友的朋友 就是朋友 那么 朋友的关系 用 并查集 保存 但是 敌对关系 只有直接的敌对关系才是具有敌对关系 所以直接用结构体保存就好

AC代码

#include <cstdio> #include <cstring> #include <ctype.h> #include <cstdlib> #include <iostream> #include <algorithm> #include <cmath> #include <deque> #include <vector> #include <queue> #include <string> #include <map> #include <stack> #include <set> #include <numeric> #include <sstream> #include <iomanip> using namespace std; typedef long long LL; const double PI = 3.14159265358979323846264338327; const double E = 2.718281828459; const double eps = 1e-6; const int MAXN = 0x3f3f3f3f; const int MINN = 0xc0c0c0c0; const int maxn = 1e2 + 5; const int MOD = 1e9 + 7; int pre[maxn]; struct Node { vector <int> v; }; map <int, Node> M; int find(int x) { int r = x; while (pre[r] != r) r = pre[r]; pre[x] = r; return r; } void join(int x, int y) { int fx = find(x), fy = find(y); if (x != fy) pre[fx] = fy; } bool Hos(int x, int y) { vector <int>::iterator it; for (it = M[x].v.begin(); it != M[x].v.end(); it++) { if ((*it) == y) return true; } return false; } int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < 2; i++) { for (int j = 0; j <= n; j++) pre[j] = j; } int x, y, flag; for (int i = 0; i < m; i++) { scanf("%d%d%d", &x, &y, &flag); if (flag == 1) join(x, y); else { M[x].v.push_back(y); M[y].v.push_back(x); } } for (int i = 0; i < k; i++) { scanf("%d%d", &x, &y); if (find(x) == find(y)) { if (Hos(x, y) == true) printf("OK but...\n"); else printf("No problem\n"); } else { if (Hos(x, y) == true) printf("No way\n"); else printf("OK\n"); } } }

转载于:https://www.cnblogs.com/Dup4/p/9433262.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)