QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#270940#5160. Kebab PizzargnerdplayerWA 1ms3456kbC++202.3kb2023-12-01 18:28:442023-12-01 18:28:44

Judging History

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

  • [2023-12-01 18:28:44]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3456kb
  • [2023-12-01 18:28:44]
  • 提交

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--;
            if (u == v) {
                continue;
            }
            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);
            adj[v].push_back(u);
        }

        for (int i = 0; i < n; i++) {
            if (count_if(adj[i].begin(), adj[i].end(), [&](int u) { return 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;
}

详细

Test #1:

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

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

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

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

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

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

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: 1ms
memory: 3404kb

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'