Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么数组中所有值的输出都为零?_C++_Graph_Input_Matching - Fatal编程技术网

C++ 为什么数组中所有值的输出都为零?

C++ 为什么数组中所有值的输出都为零?,c++,graph,input,matching,C++,Graph,Input,Matching,我得到这个实现是为了在网外进行最大匹配,并试图通过主类提供它的输入。但我在比赛中所有的位置都得零分。我做错了什么 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> #include <queue> using namespace std; void add_edge(int u, int v); void edmonds();

我得到这个实现是为了在网外进行最大匹配,并试图通过主类提供它的输入。但我在比赛中所有的位置都得零分。我做错了什么

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream> 
#include <queue>
using namespace std;
void add_edge(int u, int v);
void edmonds();
struct edge {
   int v, nx;
};
const int MAXN = 1000, MAXE = 2000;
edge graph[MAXE];
int last[MAXN], match[MAXN], px[MAXN], base[MAXN], N, M, edges;

bool used[MAXN], blossom[MAXN], lused[MAXN];

int main ()
{
//  return 0;
   add_edge(1,4);
   add_edge(1,5);
   add_edge(1,6);
   add_edge(2,5);
   add_edge(2,7);
   add_edge(3,4);
   add_edge(4,1);
   add_edge(4,3);
   add_edge(5,1);
   add_edge(5,2);
   add_edge(6,1);
   add_edge(7,2);
   edmonds();
   cout << match[0];
   cout << match[1];
   cout << match[2];
   cout << match[3];
   cout << match[4];
   cout << match[5];
   cout << match[6];
}

inline void add_edge(int u, int v) {
   graph[edges] = (edge) {v, last[u]};
   last[u] = edges++;
   graph[edges] = (edge) {u, last[v]};
   last[v] = edges++;
}

void mark_path(int v, int b, int children) {
   while (base[v] != b) {
      blossom[base[v]] = blossom[base[match[v]]] = true;
      px[v] = children;
      children = match[v];
      v = px[match[v]];
   }
}

int lca(int a, int b) {
   memset(lused, 0, N);
   while (1) {
      lused[a = base[a]] = true;
      if (match[a] == -1)
         break;
      a = px[match[a]];
   }
   while (1) {
      b = base[b];
      if (lused[b])
         return b;
      b = px[match[b]];
   }
}
int find_path(int root) {
   memset(used, 0, N);
   memset(px, -1, sizeof(int) * N);
   for (int i = 0; i < N; ++i)
      base[i] = i;
   used[root] = true;
   queue<int> q;
   q.push(root);
   register int v, e, to, i;
   while (!q.empty()) {
      v = q.front(); q.pop();
      for (e = last[v]; e >= 0; e = graph[e].nx) {
         to = graph[e].v;
         if (base[v] == base[to] || match[v] == to)
            continue;
         if (to == root || (match[to] != -1 && px[match[to]] != -1)) {
            int curbase = lca(v, to);
            memset(blossom, 0, N);
            mark_path(v, curbase, to);
            mark_path(to, curbase, v);
            for (i = 0; i < N; ++i)
               if (blossom[base[i]]) {
                  base[i] = curbase;
                  if (!used[i]) {
                     used[i] = true;
                     q.push(i);
                  }
               }
         } else if (px[to] == -1) {
            px[to] = v;
            if (match[to] == -1)
               return to;
            to = match[to];
            used[to] = true;
            q.push(to);
         }
      }
   }
   return -1;
}
void build_pre_matching() {
   register int u, e, v;
   for (u = 0; u < N; ++u)
      if (match[u] == -1)
         for (e = last[u]; e >= 0; e = graph[e].nx) {
            v = graph[e].v;
            if (match[v] == -1) {
               match[u] = v;
               match[v] = u;
               break;
            }
         }
}

void edmonds() {
   memset(match, 0xff, sizeof(int) * N);
   build_pre_matching();
   register int i, v, pv, ppv;
   for (i = 0; i < N; ++i)
      if (match[i] == -1) {
         v = find_path(i);
         while (v != -1) {
            pv = px[v], ppv = match[pv];
            match[v] = pv, match[pv] = v;
            v = ppv;
         }
      }
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
无效添加_边(int u,int v);
使爱德蒙无效();
结构边{
int v,nx;
};
常量int MAXN=1000,MAXE=2000;
边图[MAXE];
int last[MAXN],match[MAXN],px[MAXN],base[MAXN],N,M,边;
布尔使用[MAXN],布鲁姆使用[MAXN],卢塞德使用[MAXN];
int main()
{
//返回0;
添加_边(1,4);
添加_边(1,5);
添加_边(1,6);
添加_边(2,5);
添加_边(2,7);
添加_边(3,4);
添加_边(4,1);
添加_边(4,3);
添加_边(5,1);
添加_边(5,2);
添加_边(6,1);
添加_边(7,2);
爱德蒙();

您可以在两个位置设置
match
的元素:在
build\u pre\u matching()
edmonds()
中。在这两种情况下,如果
match[x],则不会发生任何更改
对于某些索引
x
不是
-1
匹配的唯一其他位置元素
获取值是在静态初始化期间,其中值被初始化为零。由于初始值为零,并且值只有在其中至少一个恰好为
-1
时才会更改,因此我希望该值s保留值
0

您可能需要使用类似

std::fill(std::begin(match), std::end(match), -1);

在一个战略位置,因为你似乎假定值最初是代码>1 < /代码>。当然,你也应该考虑使用全局变量的“<强>不/强”的概念,因为这在多线程程序中是不可缩放的,并且工作得不好。

我没有读到整个东西,但是边应该被初始化为0,而n是a。ndm似乎是常量,但也未初始化。请尝试从该常量开始。