30分求调
  • 板块P1347 排序
  • 楼主Wyy_w123
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/7/17 22:28
  • 上次更新2024/7/18 08:40:29
查看原帖
30分求调
819362
Wyy_w123楼主2024/7/17 22:28

悬关

#include <bits/stdc++.h>
using namespace std;

const int MAXC = 26;

int n, m, f[MAXC], F[MAXC];
vector<int> s[MAXC];

string S(){
  for(int i = 0; i < n; i++){
    f[i] = F[i];
  }
  queue<int> g;
  for(int i = 0; i < n; i++){
    if(!f[i]){
      g.push(i);
    }
  }
  string ans = "";
  int kkk = 0;
  while(!g.empty()){
    if(g.size() != 1){
      return "z";
    }
    int x = g.front();g.pop();
    ans += x + 'A';++kkk;
    for(int i = 0; i < s[x].size(); i++){
      --f[s[x][i]];
      if(!f[s[x][i]]){
        g.push(s[x][i]);
      }
    }
  }
  if(kkk != n) {return "y";}
  return ans;
}

int main(){
  cin >> n >> m;
  for(int i = 1; i <= m; i++){
    char a, b;cin >> a >> b >> b;
    s[a - 'A'].push_back(b - 'A');
    ++F[b - 'A'];string x = S();
    if(x == "y") {
      cout << "Inconsistency found after " << i << " relations.";
      return 0;
    }
    if(x != "z"){
      cout << "Sorted sequence determined after " << to_string(i) << " relations: " << x << ".";
      return 0;
    }
  }
  cout << "Sorted sequence cannot be determined.";
  return 0;
}
2024/7/17 22:28
加载中...