Bài tập tìm chu trình trên đồ thị C++

Tại hạ nhờ các CT fix giúp mình code bài này với.

Mình fix 3 tiếng r vẫn không biết sai format ở đâu

Code : https://ideone.com/RLhBB5

Link bài : https://oj.vnoi.info/problem/pcycle

Ý tưởng: Mình sẽ kiểm tra xem đồ thị có chu trình euler hay không, nếu không thì in ra -1 và kết thúc, ngược lại thì mình sẽ duyệt 1 vòng for(1—>n) rồi tìm chu trình euler với đĩnh bất đầu từ i(Nếu nhứ tổng trọng số > 0 và tại mỗi thời điểm tổng trọng số > 0) thì mình in ra chu trình! Nếu như duyệ hết 1–>n vẫn không tìm thấy thì in ra -1

Sai format gì hả bạn?

Your code is confusing and very untypical for C++. Normally a computer loop starts at 0 and ends at max-1. Your loop starts at 1 and ends at max, and that makes no sense at all. Regardless of the programming language, an array always starts at 0 and ends at length-1. For example, in[100] means that the index of the first element is 0 and that of the last element is 99. You should optimize your algorithm before posting your code and tell this forum WHERE and HOW the problem occurs. A terse statement like “Mình fix 3 tiếng r vẫn không biết sai format ở đâu” is not only rude but also very vague for help. No wonder that @noname00 had to ask you for clarification.

Mình không rõ nữa, mình nộp trên vnoi thì báo lỗi WA kèm dòng này “wrong output format Unexpected end of file - int32 expected”

Sorry about that Sir

Có thể là bạn in thiếu số ở đâu đó. Có 2 khả năng xảy ra:

  1. Ở 1 trường hợp có nghiệm nào đó, bạn không in đủ M+1 số.
  2. Ở 1 trường hợp có nghiệm nào đó, bạn lại in ra -1.

Bạn debug lại code nhé.

Cái mình thắc mắc nữa là sao bạn ghi bug1, bug2, … làm gì v :vv.

I reviewed your code and found that it contains many unnecessary functions. This makes it long and tedious to read and check for correctness. So I gave up. Example:

// whatfor ?
void dfs(int u){
  visited[u] = true;
  for(auto it : adj[u]){
   int v = it.first;
    if(!visited[v]) dfs(v);
  }
}

and without this dfs() the code still work properly

bool check(){
  /*
  for(int x=1; x<=n; x++){
    if(in[x]!=0){
      dfs(x); break;
    } //bug7
  }
  */
  for(int i=1; i<=n; i++){
    //if(!visited[i] && in[i] != 0) return false;// bug4
    if(in[i]==0 || in[i] % 2 != 0) return false;
  }
  return true;
}

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?