RE报0,求条
  • 板块P1347 排序
  • 楼主yinjunrui
  • 当前回复1
  • 已保存回复1
  • 发布时间2025/6/18 15:57
  • 上次更新2025/6/19 15:58:47
查看原帖
RE报0,求条
1234463
yinjunrui楼主2025/6/18 15:57

本地测没有问题,但交到上面全部RE

#include <bits/stdc++.h>
using namespace std;
int n, m, cnt, now, rd[30], d[30], head[30], vis[30];
char c;
struct edge {
  int to, nxt;
} e[605];
int add(int u, int v) {
  cnt++;
  e[cnt].to = v, e[cnt].nxt = head[u], head[u] = cnt;
}
void top_sort(int x) {
  int tot = 0, sum = 0;
  queue<int> q, ans;
  for (int i = 1; i <= n; i++)
    for (int j = head[i]; j; j = e[j].nxt)
      d[e[j].to]++;
  for (int i = 1; i <= n; i++) {
    if (!d[i] && vis[i]) {
      q.push(i);
      sum++;
    }
  }
  while (!q.empty()) {
    int u = q.front();
    tot++;
    ans.push(u);
    q.pop();
    for (int j = head[u]; j; j = e[j].nxt) {
      int v = e[j].to;
      if (!(--d[v]))
        sum++, q.emplace(v);
    }
  }
  if (tot == n) {
    printf("Sorted sequence determined after %d relations: ", x);
    while (!ans.empty()) {
      printf("%c", ans.front() + 'A' - 1);
      ans.pop();
    }
    putchar('.');
    exit(0);
  }
  if (sum != now) {
    printf("Inconsistency found after %d relations.", x);
    exit(0);
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1, u, v; i <= m; i++) {
    string s;
    cin >> s;
    u = s[0] - 'A' + 1, v = s[2] - 'A' + 1;
    now += (!vis[u] + !vis[v]);
    vis[u]++, vis[v]++;
    add(u, v);
    top_sort(i);
  }
  printf("Sorted sequence cannot be determined.");
  return 0;
}
2025/6/18 15:57
加载中...