QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#270944#5160. Kebab PizzargnerdplayerWA 0ms3848kbC++202.3kb2023-12-01 18:32:452023-12-01 18:32:46

Judging History

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

  • [2023-12-01 18:32:46]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3848kb
  • [2023-12-01 18:32:45]
  • 提交

answer

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

using i64 = long long;

struct DSU {
    int n, cnt;
    vector<int> fa, sz;
    DSU(int n = 0) : n(n), cnt(n), fa(n), sz(n, 1) { iota(fa.begin(), fa.end(), 0); }
    int find(int u) { return u == fa[u] ? u : fa[u] = find(fa[u]); }
    bool join(int u, int v) {
        u = find(u), v = find(v);
        if (u != v) {
            // if (sz[u] < sz[v]) { swap(u, v); }
            sz[u] += sz[v];
            fa[v] = u;
            cnt--;
            return true;
        }
        return false;
    }
    bool same(int u, int v) { return find(u) == find(v); }
    int size(int u) { return sz[find(u)]; }
};

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

    auto solve = [&]() {
        int m, n;
        cin >> m >> n;

        vector<pair<int, int>> e;
        while (m--) {
            int u, v;
            cin >> u >> v;
            u--, v--;
            e.push_back(minmax(u, v));
        }

        sort(e.begin(), e.end());
        e.erase(unique(e.begin(), e.end()), e.end());

        vector<vector<int>> adj(n);

        for (auto [u, v] : e) {
            adj[u].push_back(v);
            if (u != v) {
                adj[v].push_back(u);
            }
        }

        for (int i = 0; i < n; i++) {
            if (count_if(adj[i].begin(), adj[i].end(), [&](int u) { return u != i && int(adj[u].size()) > 1; }) >= 3) {
                cout << "impossible\n";
                return;
            }
        }

        vector<int> vis(n);
        int comps = 0, hasCycle = 0;

        for (int i = 0; i < n; i++) {
            if (adj[i].empty()) {
                continue;
            }
            bool cycle = false;
            auto dfs = [&](auto dfs, int u, int p) -> void {
                vis[u] = true;
                for (auto v : adj[u]) {
                    if (v == p) {
                        continue;
                    }
                    if (!vis[v]) {
                        dfs(dfs, v, u);
                    } else {
                        cycle = true;
                    }
                }
            };
            comps++;
            hasCycle += cycle;
        }

        cout << (comps == 1 || !hasCycle ? "possible" : "impossible") << '\n';
    };
    
    solve();
    
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3848kb

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: 3644kb

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: 3580kb

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: 3656kb

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: 3676kb

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: 3580kb

input:

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

output:

possible

result:

ok single line: 'possible'

Test #7:

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

input:

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

output:

impossible

result:

ok single line: 'impossible'

Test #8:

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

input:

4 5
1 2
3 4
4 5
5 3

output:

possible

result:

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