鱼C论坛

 找回密码
 立即注册
查看: 3335|回复: 5

有没有大佬帮忙看看这个在考什么,今天的面试题没写出来

[复制链接]
发表于 2019-4-3 19:56:48 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
   1.小Q最近在玩一个嵌入式开发板,看着板子上的一堆电容,他想比较板子上电容数量的多少。第一行给定比较次数n,
     接下来n行,每行给定两个板子的编号a1,a2,编号从1开始。每行第一个元素a1为第一次比较中板子上电容数量较多的
     板子编号,第二个元素a2为板子上电容数量较少的板子编号。最后一行为要比较的两块板子的编号a,b.请返回这两块
     板子上电容数量大小的关系,若a更多返回1,b更多返回-1,无法判断返回0。输入数据保证合法,不会有矛盾的情况出现。


     样例输入如下:

     4                   //比较次数

     1  2

     2  4

     1  3

     4  3

     2  3


    样例输出:

    1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-4-3 20:41:09 | 显示全部楼层
本帖最后由 Croper 于 2019-4-3 21:41 编辑

有向无环图的顶点连通性问题,或者说对有向无环图上的两顶点进行拓扑定序
思路:拓扑排序,确定先后,然后从拓扑次序靠前的点开始进行深度遍历,如果不能遍历到靠后的点说明不能确定,否则就能确定
一会来上代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-4-3 20:46:01 | 显示全部楼层
Croper 发表于 2019-4-3 20:41
有向无环图的顶点连通性问题,或者说对有向无环图上的两顶点进行拓扑定序
思路:拓扑排序,确定先后,然后 ...

唉,拓扑排序没怎么看,不会,只能去补补了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-4-3 21:38:55 | 显示全部楼层
本帖最后由 Croper 于 2019-4-3 21:47 编辑
  1. #include <unordered_map>
  2. #include <unordered_set>
  3. #include <vector>
  4. #include <stack>
  5. #include <iostream>

  6. using namespace std;

  7. struct node {
  8.         int order;
  9.         int indegree;
  10.         vector<node*> nexts;
  11.         node() :order(-1),indegree(0) {};
  12. };

  13. int isPre(vector<pair<int, int>> givenorder, pair<int, int> question) {  //主函数
  14.         unordered_map<int, node*> map;          //这里使用map来记录节点是为了防止题目直接出现65536号节点之类的奇葩输入,导致建立了65536个节点而浪费大量内存
  15.         unordered_set<node*> roots;            

  16.         //遍历给出的路径,生成图,并将入度为零的节点放入root中
  17.         for (auto path : givenorder) {         
  18.                 node*& pre = map[path.first];
  19.                 node*& next = map[path.second];
  20.                 if (pre == nullptr) {               //如果路径上的节点第一次出现则创建节点
  21.                         pre = new node;               
  22.                         roots.insert(pre);               //新创建的前方节点入度一定为0
  23.                 }
  24.                 if (next==nullptr) {
  25.                         next = new node;
  26.                         roots.insert(next);
  27.                 }
  28.                
  29.                 roots.erase(next);                 //位于后方节点当然不可能入度为0了
  30.                 next->indegree++;
  31.                 pre->nexts.emplace_back(next);      
  32.         }

  33.        //如果询问的节点根本没有在条件中出现过那么一定无法判断先后
  34.         node* p1 = map[question.first];
  35.         node* p2 = map[question.second];
  36.         if (p1 == nullptr || p2 == nullptr) return 0;  
  37.        
  38.         //拓扑排序
  39.         //遍历方式:检查每个root中的节点,为其赋予唯一的顺序(因为没有节点比它更靠前了)
  40.         //将其移除root,并将其子节点的indegree-1,如果indegree为0,则将其加入root
  41.          //此方法将遍历所有非环上的节点,
  42.         int order = 0;
  43.         while (!roots.empty()){                     
  44.                 node* pre = *(roots.begin());                                                                                                                                                                
  45.                 roots.erase(pre);
  46.                 pre->order = order++;
  47.                 for (node* next : pre->nexts) {
  48.                         next->indegree--;
  49.                         if (next->indegree == 0) {
  50.                                 roots.insert(next);
  51.                         }
  52.                 }
  53.         };

  54.         if (p1->order == -1 || p2->order == -1) return 0;   //这句可以不要,如果order==-1说明节点在环上,但题目明确了这是无环图
  55.        
  56.          //将p1重新定向为拓扑排序靠前的节点,p2为靠后的节点
  57.         //flg为是否经过换位的标志,也是预定的输出;
  58.         int flg = 1;                                       
  59.         if (p1->order > p2->order) {            
  60.                 auto tmp = p1;
  61.                 p1 = p2;
  62.                 p2 = tmp;
  63.                 flg = -1;
  64.         }

  65.         //将p1视为根节点,从p1开始进行深度优先遍历
  66.         stack<node*> stk;
  67.         p1->indegree = 1;
  68.         stk.push(p1);
  69.         while (!stk.empty()) {                     
  70.                 node* pre = stk.top();
  71.                 stk.pop();
  72.                 for (node* next : pre->nexts) {
  73.                         if (next == p2) return flg;
  74.                         if (next->indegree == 1) continue;          //如果遍历遇到p2,返回flg值
  75.                         next->indegree = 1;
  76.                         stk.push(next);
  77.                 }
  78.         }

  79.         return 0;                                    //没有遍历到p2,返回0
  80. }

  81. int main() {
  82.         //读取数据
  83.         int N;   
  84.         vector<pair<int, int>> givenorder;   
  85.         pair<int, int> question;
  86.         cin >> N;
  87.         for (int i = 0; i < N; ++i) {        
  88.                 pair<int, int> path;
  89.                 cin >> path.first >> path.second;
  90.                 givenorder.push_back(path);
  91.         }
  92.         cin >> question.first >> question.second;

  93.         //计算
  94.         int ret = isPre(givenorder, question);

  95.         //输出结构
  96.         cout << ret;

  97.         system("pause");
  98. }
复制代码


实验了lz的用例和一个自己编的用例,没有问题
但不保证一定正确,如果发现什么问题,告诉我一下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-4-3 22:14:33 | 显示全部楼层
本帖最后由 Croper 于 2019-4-3 22:25 编辑

还有一个方法就是不进行拓扑排序,直接建立图后就以给出的两个节点开始进行遍历,如果能遍历到对方,就为1或-1,否则等于0
这个方法写起来应该还简单一些,

。。。似乎也要快一些,因为拓扑排序是遍历了所有节点,而这个方法不需要遍历所有节点

。。嗯。。。不过最好的方法似乎应该是在拓扑排序的时候遇到任意一个节点直接中断拓扑排序。。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-4-3 22:46:22 | 显示全部楼层
修改如下
  1. int isPre(vector<pair<int, int>> givenorder, pair<int, int> question) {  //主函数
  2.         unordered_map<int, node*> map;          //这里使用map来记录节点是为了防止题目直接出现65536号节点之类的奇葩输入,导致建立了65536个节点而浪费大量内存
  3.         unordered_set<node*> roots;

  4.         //遍历给出的路径,生成图,并将入度为零的节点放入root中
  5.         for (auto path : givenorder) {
  6.                 node*& pre = map[path.first];
  7.                 node*& next = map[path.second];
  8.                 if (pre == nullptr) {               //如果路径上的节点第一次出现则创建节点
  9.                         pre = new node;
  10.                         pre->id = path.first;
  11.                         roots.insert(pre);               //新创建的前方节点入度一定为0
  12.                 }
  13.                 if (next == nullptr) {
  14.                         next = new node;
  15.                         next->id = path.second;
  16.                         roots.insert(next);
  17.                 }

  18.                 roots.erase(next);                 //位于后方节点当然不可能入度为0了
  19.                 next->indegree++;
  20.                 pre->nexts.emplace_back(next);
  21.         }

  22.         //如果询问的节点根本没有在条件中出现过那么一定无法判断先后
  23.         node* p1 = map[question.first];
  24.         node* p2 = map[question.second];
  25.         if (p1 == nullptr || p2 == nullptr) return 0;

  26.         //拓扑排序
  27.         //遍历方式:检查每个root中的节点,为其赋予唯一的顺序(因为没有节点比它更靠前了)
  28.         //将其移除root,并将其子节点的indegree-1,如果indegree为0,则将其加入root
  29.          //此方法将遍历所有非环上的节点,
  30.         int flg = 1;
  31.         while (!roots.empty()) {
  32.                 node* pre = *(roots.begin());
  33.                 roots.erase(pre);
  34.                 for (node* next : pre->nexts) {
  35.                         next->indegree--;
  36.                         if (next->indegree == 0) {
  37.                                 if (next == p1 || next == p2) {
  38.                                         if (next == p2) flg = -1;
  39.                                         break;
  40.                                 }
  41.                                 roots.insert(next);
  42.                         }
  43.                 }
  44.         };

  45.         //将p1重新定向为拓扑排序靠前的节点,p2为靠后的节点
  46.    //flg为是否经过换位的标志,也是预定的输出;
  47.         if (flg == -1) {
  48.                 auto tmp = p1;
  49.                 p1 = p2;
  50.                 p2 = tmp;
  51.         }

  52.         //将p1视为根节点,从p1开始进行深度优先遍历
  53.         stack<node*> stk;
  54.         unordered_map<node*, bool> hascheck;
  55.         hascheck[p1] = true;
  56.         stk.push(p1);
  57.         while (!stk.empty()) {
  58.                 node* pre = stk.top();
  59.                 stk.pop();
  60.                 for (node* next : pre->nexts) {
  61.                         if (next == p2) return flg;
  62.                         if (hascheck[next]) continue;          //如果遍历遇到p2,返回flg值
  63.                         hascheck[next] = true;
  64.                         stk.push(next);
  65.                 }
  66.         }

  67.         return 0;                                    //没有遍历到p2,返回0
  68. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-13 01:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表