QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#71336#5160. Kebab PizzaGreenscreen23RE 2ms3316kbC++173.1kb2023-01-09 18:44:112023-01-09 18:44:13

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-01-09 18:44:13]
  • 评测
  • 测评结果:RE
  • 用时:2ms
  • 内存:3316kb
  • [2023-01-09 18:44:11]
  • 提交

answer

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

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
#define rep(i, a, b) for(ll i = (a); i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (ll)(x).size()
int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin.exceptions(cin.failbit);

    ll n, k; cin >> n >> k;
    set<pair<ll, ll>> edges;
    rep(i, 0, n) {
        ll a, b; cin >> a >> b; a--; b--;

        if (a > b) swap(a, b);
        edges.insert({a, b});
    }

    vector<ll> deg(k, 0);
    for(auto& [u, v] : edges) {
        if (u == v) continue;
        deg[u]++;
        deg[v]++;
    }

    vector<bool> required(k, false);
    vector<vector<ll>> adj(k);
    ll nodeWithEdge = -1;
    for(auto& [u, v]: edges) {
        if (u == v) {
            required[u] = true;
            continue;
        }

        if (deg[u] == 1 && !required[u]) {
            required[v] = true;
            continue;
        }

        if (deg[v] == 1 && !required[v]) {
            required[u] = true;
            continue;
        }

        adj[u].push_back(v);
        adj[v].push_back(u);
        nodeWithEdge = u;
    }
    if (nodeWithEdge == -1) {
        cout << "possible" << endl;
        return 0;
    }

    bool tryCircle = true;
    rep(i, 0, k) {
        if (sz(adj[i]) >= 3) {
            cout << "impossible" << endl;
            return 0;
        }
        if (required[i] && deg[i] == 0) {
            tryCircle = false;
        }
    }

    vector<bool> used(k, false);
    bool hasCircle = true;
    if (tryCircle) {
        // check connected
        queue<ll> todo;
        todo.push(nodeWithEdge);
        used[nodeWithEdge] = true;
        while(!todo.empty()) {
            ll v = todo.front();
            todo.pop();

            for(auto u : adj[v]) {
                if (used[u]) continue;

                todo.push(u);
                used[u] = true;
            }
        }

        rep(i, 0, k) {
            if (deg[i] == 1) hasCircle = false;
            if (deg[i] == 2 && !used[i]) hasCircle = false;
        }

        if (hasCircle) {
            cout << "possible" << endl;
            return 0;
        }
    }

    ll countDeg2 = 0;
    vector<ll> deg1Nodes;
    rep(i, 0, k) {
        if (deg[i] == 1) {
            deg1Nodes.push_back(i);
        }

        if (deg[i] == 2) countDeg2++;
    }

    if (countDeg2 == 0) {
        cout << "possible" << endl;
        return 0;
    }

    used.assign(k, false);
    for(auto deg1Node : deg1Nodes) {
        if (used[deg1Node]) continue;
        ll currNode = deg1Node;
        ll prevNode = -1;
        do {
            if (*adj[currNode].begin() != prevNode) {
                prevNode = currNode;
                currNode = *adj[currNode].begin();
            }
            else {
                prevNode = currNode;
                currNode = *next(adj[currNode].begin());
            }
            countDeg2--;
        } while(sz(adj[currNode]) == 2);
        countDeg2++;
        if (countDeg2 == 0) {
            cout << "possible" << endl;
            return 0;
        }
        used[currNode] = true;
    }

    cout << "impossible" << endl;
}

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 3316kb

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: -100
Runtime Error

input:

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

output:


result: