QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#270886#5160. Kebab Pizzackiseki#WA 1ms3456kbC++202.3kb2023-12-01 17:13:042023-12-01 17:13:04

Judging History

你现在查看的是最新测评结果

  • [2023-12-01 17:13:04]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3456kb
  • [2023-12-01 17:13:04]
  • 提交

answer

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

#define all(x) begin(x), end(x)
#ifdef CKISEKI
#define safe cerr << __PRETTY_FUNCTION__ << " line " << __LINE__ << " safe\n"
#define debug(a...) debug_(#a, a)
#define orange(a...) orange_(#a, a)
#include <experimental/iterator>
void debug_(auto s, auto ...a) {
  cerr << "\e[1;32m(" << s << ") = (";
  int f = 0;
  (..., (cerr << (f++ ? ", " : "") << a));
  cerr << ")\e[0m\n";
}
void orange_(auto s, auto L, auto R) {
  cerr << "\e[1;33m[ " << s << " ] = [ ";
  using namespace experimental;
  copy(L, R, make_ostream_joiner(cerr, ", "));
  cerr << " ]\e[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) safe
#define orange(...) safe
#endif

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int n, k;
  cin >> n >> k;

  vector<set<int>> st(k);
  vector<int> self_loop;

  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    --a, --b;

    if (a == b) {
      self_loop.emplace_back(a);
      continue;
    }

    st[a].emplace(b);
    st[b].emplace(a);
  }


  vector<int> cover, covered(k);

  for (int i = 0; i < k; i++) {
    if (st[i].empty()) continue;

    if (st[i].size() == 1) {
      covered[i] = true;
      cover.emplace_back(i);
    }
  }

  vector<pair<int,int>> lonely_pair;
  for (int i : cover) {
    debug(i);
    if (st[i].empty()) continue;
    int j = *st[i].begin();
    if (covered[j]) {
      lonely_pair.emplace_back(i, j);
    }
    st[j].erase(i);
    st[i].clear();
  }

  int tot_edge = 0;
  for (int i = 0; i < k; i++) {
    debug(st[i].size());
    if (st[i].size() > 2) {
      cout << "impossible\n";
      return 0;
    }
    tot_edge += st[i].size();
  }
  assert (tot_edge % 2 == 0);
  tot_edge /= 2;
  debug(tot_edge);


  bool cycle = false;
  int cycle_size = 0;
  vector<int> vis(k);
  const auto dfs = [&](auto self, int i, int f) -> void {
    ++cycle_size;
    vis[i] = true;
    for (int j : st[i]) {
      if (j == f) continue;
      if (!vis[j]) {
        self(self, j, i);
      } else {
        cycle = true;
      }
    }
    return;
  };

  for (int i = 0; i < k; i++) {
    if (!vis[i]) {
      cycle = false;
      cycle_size = 0;
      dfs(dfs, i, -1);

      if (cycle) {
        if (cycle_size != tot_edge) {
          cout << "impossible\n";
          return 0;
        }
      }
    }
  }

  cout << "possible\n";


  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3416kb

input:

7 6
2 2
3 6
1 1
1 5
4 5
6 6
6 5

output:

possible

result:

ok single line: 'possible'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3432kb

input:

5 5
1 3
1 5
2 3
2 5
3 4

output:

possible

result:

ok single line: 'possible'

Test #3:

score: 0
Accepted
time: 0ms
memory: 3440kb

input:

6 7
1 2
2 3
3 4
4 5
3 6
6 7

output:

impossible

result:

ok single line: 'impossible'

Test #4:

score: 0
Accepted
time: 0ms
memory: 3456kb

input:

8 4
1 1
1 2
2 1
2 2
3 3
3 4
4 3
4 4

output:

possible

result:

ok single line: 'possible'

Test #5:

score: 0
Accepted
time: 0ms
memory: 3424kb

input:

4 4
1 2
2 1
3 4
4 3

output:

possible

result:

ok single line: 'possible'

Test #6:

score: 0
Accepted
time: 0ms
memory: 3432kb

input:

5 4
1 1
1 4
2 2
2 4
3 4

output:

possible

result:

ok single line: 'possible'

Test #7:

score: -100
Wrong Answer
time: 0ms
memory: 3436kb

input:

6 4
1 1
1 4
2 2
2 4
3 3
3 4

output:

possible

result:

wrong answer 1st lines differ - expected: 'impossible', found: 'possible'