PAT 天梯赛 L2-010. 排座位 【并查集】
题目链接
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");
}
}
}
PAT 天梯赛 L2-010. 排座位 【并查集】的更多相关文章
- PAT 天梯赛 L2-007. 家庭房产 【并查集】
题目链接 https://www.patest.cn/contests/gplt/L2-007 思路 将一个家庭里的所有人都并进去 然后最后查找的时候 找到所有同一个家庭的人,计算出人数,人均房产套数 ...
- 天梯赛决赛 L2-1.红色警报 并查集
L2-013. 红色警报 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 战争中保持各个城市间的连通性非常重要.本题要求你编写一 ...
- PAT天梯赛 L1-049 天梯赛座位分配
题目链接:点击打开链接 天梯赛每年有大量参赛队员,要保证同一所学校的所有队员都不能相邻,分配座位就成为一件比较麻烦的事情.为此我们制定如下策略:假设某赛场有 N 所学校参赛,第 i 所学校有 M[i] ...
- PAT天梯赛L3-007 天梯地图
题目链接:点击打开链接 本题要求你实现一个天梯赛专属在线地图,队员输入自己学校所在地和赛场地点后,该地图应该推荐两条路线:一条是最快到达路线:一条是最短距离的路线.题目保证对任意的查询请求,地图上都至 ...
- PAT天梯赛练习题——L3-007. 天梯地图(多边权SPFA)
L3-007. 天梯地图 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 本题要求你实现一个天梯赛专属在线地图,队员输入自己学校 ...
- PTA天梯赛L2
L2-001 紧急救援 题意:就是给你一张n<500的图:让你求最短路径,最短路条数,以及路径: 做法,先用dijkstra求最短路,然后dfs找最短路条数,以及点权的最大值: 一般dfs不就可 ...
- PAT 天梯赛 L1-049. 天梯赛座位分配 【循环】
题目链接 https://www.patest.cn/contests/gplt/L1-049 思路 用一个二维数组来保存一个学校每个队员的座位号 然后需要判断一下 目前的座位号 与该学校当前状态下最 ...
- PAT天梯赛练习题 L3-010. 是否完全二叉搜索树(完全二叉树的判断)
L3-010. 是否完全二叉搜索树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 将一系列给定数字顺序插入一个初始为空的二叉搜 ...
- PAT 天梯赛练习集 L2-022. 重排链表
题目链接:https://www.patest.cn/contests/gplt/L2-022 给定一个单链表 L1→L2→...→Ln-1→Ln,请编写程序将链表重新排列为 Ln→L1→Ln-1→L ...
随机推荐
- 【VAB】获取库文件地址
如何获取Excle库文件地址呢?具体代码如下: Public Sub 获取Excel库文件夹的路径() MsgBox "库文件夹的路径是: " & Application. ...
- 安装Linux centos 时编辑选项
将第上一步选择编辑之后出来的文字修改为:>vmlinuz initrd=initrd.img linux dd quiet 这里注意了:网上很多文章都说这一步改成“>vmlinuz ini ...
- charles用法详解
Charles是目前最强大的http调试工具,在界面和功能上远强于Fiddler,同时是全平台支持,堪称圣杯级工具,唯一的缺陷是这货是收费的,而且是要¥50美元大洋…当然网上是有破解版的,鄙视下自己, ...
- Spring获取HttpServletRequest
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest()
- spring异常 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderServlet
spring异常 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderServlet 情况 ...
- Windows键
Windows键+D: 最小化或恢复windows窗口 Windows键+U: 打开“辅助工具管理器” Windows键+CTRL+M 重新将恢复上一项操作前窗口的大小和位置 Windows ...
- c#调用WinRAR软件压缩和解压文件
using System; using System.Collections.Generic; using System.Web; using System.IO; using System.Linq ...
- MySQL优化时可以设置的几个参数
back_log:back_log值指出在MySQL暂时停止回答新请求之前的短时间内多少个请求可以被存在堆栈中.也就是说,如果MySql的连接数据达到max_connections时,新来的请求将会被 ...
- 机器学习13—PCA学习笔记
主成分分析PCA 机器学习实战之PCA test13.py #-*- coding:utf-8 import sys sys.path.append("pca.py") impo ...
- saltstack内置state模块file之append
添加文件文本在文件尾部 salt.states.file.append(name, text=None, makedirs=False, source=None, source_hash=None, ...