题目见前文:DS实验题 Old_Driver UnionFindSet结构

这里使用邻接表存储敌人之间的关系,邻接表用指针实现:

//
// main.cpp
// Old_Driver3
//
// Created by wasdns on 16/12/18.
// Copyright © 2016年 wasdns. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; struct edge
{
edge *next;
int num;
//int len;
}eg[105]; struct head
{
edge *next;
int num;
}h[105]; void IniList(int n)
{
int i; for (i = 1; i <= n; i++)
{
h[i].next = NULL;
h[i].num = i;
}
} void CreatList(int x, int y)
{
edge *p1, *p2; p1 = new edge;
p1 -> next = NULL;
p1 -> num = y; p2 = new edge;
p2 -> next = NULL;
p2 -> num = x;
//p2 -> len = leng; edge *p3, *p4; p3 = h[x].next; if (p3 == NULL) {
h[x].next = p1;
} else
{
while (p3 -> next != NULL) {
p3 = p3 -> next;
} p3 -> next = p1;
} p4 = h[y].next;
if (p4 == NULL) {
h[y].next = p2;
} else
{
while (p4 -> next != NULL) {
p4 = p4 -> next;
} p4 -> next = p2;
}
} bool isAnamy(int x, int y)
{
bool flag = false; edge* p = new edge; p = h[x].next; while (p != NULL)
{
if (p -> num == y)
{
flag = true;
break;
} p = p -> next;
} return flag;
} int fa[105]; void IniFUS(int n)
{
int i; for (i = 1; i <= n; i++)
{
fa[i] = i;
}
} int Find(int x)
{
int f = x; while (f != fa[f])
{
f = fa[f];
} int i = x, j; while (i != f)
{
j = fa[i]; fa[i] = f; i = j;
} return f;
} void Union(int x, int y)
{
int xfa = Find(x);
int yfa = Find(y); if (xfa != yfa) {
fa[yfa] = xfa;
}
} void Query(int k)
{
int i; int x, y; for (i = 1; i <= k; i++)
{
cin >> x >> y; if (!isAnamy(x, y))
{
if (Find(x) == Find(y)) {
cout << "Good job" << endl;
} else {
cout << "No problem" << endl;
}
} else
{
if (Find(x) == Find(y)) {
cout << "OK but..." << endl;
} else {
cout << "No way" << endl;
}
}
}
} int main()
{
int n, m, k; cin >> n >> m >> k; IniFUS(n);
IniList(n); int i, x, y, z; for (i = 1; i <= m; i++)
{
cin >> x >> y >> z; if (z == 1) Union(x, y); else if (z == -1)
{
CreatList(x, y);
CreatList(y, x);
}
} Query(k); return 0;
}

2016/12/18

DS实验题 Old_Driver UnionFindSet结构 指针实现邻接表存储的更多相关文章

  1. DS实验题 Old_Driver UnionFindSet结构

    题目 思路 很典型的并查集问题,朋友A和B要合并到一个统一的集合中,也就是Union(A, B)操作,在Union操作中需要先找到A所属的集合的代表元和B所属集合的代表元,也就是使用Find(A)和F ...

  2. DS实验题 融合软泥怪-2 Heap实现

    题目和STL实现:DS实验题 融合软泥怪-1 用堆实现优先队列 引言和堆的介绍摘自:Priority Queue(Heaps)--优先队列(堆) 引言: 优先队列是一个至少能够提供插入(Insert) ...

  3. 图的存储结构(邻接矩阵与邻接表)及其C++实现

    一.图的定义 图是由顶点的有穷非空集合和顶点之间边的集合组成,通常表示为: G=(V,E) 其中:G表示一个图,V是图G中顶点的集合,E是图G中顶点之间边的集合. 注: 在线性表中,元素个数可以为零, ...

  4. DS实验题 Dijkstra算法

    参考:Dijkstra算法 数据结构来到了图论这一章节,网络中的路由算法基本都和图论相关.于是在拿到DS的实验题的时候,决定看下久负盛名的Dijkstra算法. Dijkstra的经典应用是开放最短路 ...

  5. DS实验题 sights

    算法与数据结构实验题 6.3 sights ★实验任务 美丽的小风姑娘打算去旅游散心,她走进了一座山,发现这座山有 n 个景点, 由于山路难修,所以施工队只修了最少条的路,来保证 n 个景点联通,娇弱 ...

  6. DS实验题 order

    算法与数据结构 实验题 6.4 order ★实验任务 给出一棵二叉树的中序遍历和每个节点的父节点,求这棵二叉树的先序和后序遍历. ★数据输入 输入第一行为一个正整数n表示二叉树的节点数目,节点编号从 ...

  7. DS实验题 Searchname

    题目: 思路: 如果直接暴力搜索的话,时间复杂度为O(n*m),在n为百万量级的情况下,必然是T. 所以,这里通过hash函数,将字符串转换为对应的hash值:同时利用邻接表避免了hash冲突,方法是 ...

  8. typedef struct LNode命名结构指针(线性表的链式存储)

    一.typedef 关键字 1. 简介: typedef工具是一个高级数据特性,利用typedef可以为某一些类型自定义名称. 2. 工作原理: 例如我们定义链表的存储结构时,需要定义结点的存储数据元 ...

  9. c++邻接表存储图(无向),并用广度优先和深度优先遍历(实验)

    一开始我是用c写的,后面才发现广搜要用到队列,所以我就直接使用c++的STL队列来写, 因为不想再写多一个队列了.这次实验写了两个多钟,因为要边写边思考,太菜了哈哈. 主要参考<大话数据结构&g ...

随机推荐

  1. HttpHandler动态生成图片

    1.向服务器请求返回图片,浏览器是不知道服务上有这个图片的存在的,只是发出请求,接收请求,显示图片 string path = context.Server.MapPath("~/1.jpg ...

  2. php 指针遍历、预定义数组和常用函数

    <?php /*//定义 $attr = array(1,2,3); $attr[] = 1; $attr = array("one"=>"hello&quo ...

  3. /bin/dd if=/path/to/source-file of=/path/to/backup-file

    [root@ok virhost]# qemu-img info 05t.img image: 05t.img file format: raw virtual size: 10G (10737418 ...

  4. 无线ap和路由器wifi热点怎么区分和区别

    转自:http://blog.sina.com.cn/s/blog_5a6efa330101yrzh.html 有的人发现无线ap和无线路由器都可以实现无线上网,于是到无线市场买了个相当便宜的无线ap ...

  5. Android安卓知识点

    1 包名是唯一标识apk的记号,相当于公民身份证号. 2 ADB是Android Debug Brigde 的英文缩写,意思是Android程序调试桥,使用SDK自带的工具可以对Android模拟器或 ...

  6. SQL Server 2000 ——系统表和系统视图

    一.系统表 数据字典的详细信息请查SQL SERVER BOL,这里仅列出一部分. 1.1.sysservers 1.查看所有本地服务器及链接服务器 select * from master..sys ...

  7. Loadrunner中web_reg_save_param的使用详解

    [摘要]利用实际案例说明如何使用Mercury LoadRunner提取包含在HTML页内的动态信息并创建参数. [关键词]性能测试,压力测试,Mercury LoadRunner 应用范围 在使用L ...

  8. 数据采集器移动手持打印POS终端(PDA)商超抄单方案-PDA抄单无线开单+进销存软件

    PDA主要应用在业务员外出在超市能及时抄单发送回总公司,总公司办公人员及时在软件里面调出业务员的抄单数量进行配货,大大提供工作效率. 移动数据终端和无线基础架构相结合,面向零售与批发门店店员的解决方案 ...

  9. quick cocos map使用

    '''lua local MainScene = class("MainScene", function() return display.newScene("MainS ...

  10. BZOJ3658 : Jabberwocky

    考虑将某线段下方的点取走: 将所有点从低到高排序 每扫描到一条水平线,对于上面每个点,找到它下面同色的前驱后继,统计中间点的个数 然后再把线上所有点插入数据结构中 最后再统计相邻的同色的点之间的点个数 ...