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 ...
随机推荐
- WebService的缓存机制
分类: .NET ASP 2010-11-13 10:00 849人阅读 评论(0) 收藏 举报 webservicestring数据库nullapplicationweb服务 WebService的 ...
- DialogFragment创建默认dialog
代码地址如下:http://www.demodashi.com/demo/12228.html 记得把这几点描述好咯:代码实现过程 + 项目文件结构截图 + 演示效果 前言 在我们项目的进行中不可避免 ...
- c多线程学习
http://www.ibm.com/developerworks/cn/linux/thread/posix_thread1/index.html http://blog.chinaunix.net ...
- Storm/Cassandra集成错误:NoSuchMethodError: concurrent.Futures.withFallback
本文原文出处: http://blog.csdn.net/bluishglc/article/details/50443205 严禁不论什么形式的转载.否则将托付CSDN官方维护权益. 2015年的最 ...
- CentOS4.5下LVS方案
环境描述:本文在配置LVS时使用三台linux,一台做Directorserver (192.168.0.25) ,两台做realserver(192.168.0.127 192.168.0.12,在 ...
- JS 手势长按代码
同时支持长按和点击事件,无依赖版 <!DOCTYPE html> <html lang="en"> <head> <meta charse ...
- hdu 1598 find the most comfortable road(并查集)
题意:略 分析:多询问问题,利用并查集加速.类似于kruskal对MST的构建:枚举最小的边,逐渐将更大的边加入集合,当查询的点在同一个集合,那么当前最小值,就是所加的最后一条边与第一条只差. 注意: ...
- member access within misaligned address 0x000000000031 for type 'struct ListNode', which requires 8 byte alignment
在做LeetCode的two sum题目的时候,提交代码遇到了如题的runtime error,后来在一篇博客上看到了解决方法. 现有如下结构体: struct ListNode { int val; ...
- 给定一个字符串s,你可以从中删除一些字符,使得剩下的串是一个回文串。如何删除才能使得回文串最长呢? 输出需要删除的字符个数。
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> ...
- elk文件
=================正则匹配 [root@web02 conf.d]# cat apache-grok.conf input{ file { path => "/var/ ...