QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#227062#5160. Kebab PizzaFyindWA 7ms28456kbC++232.5kb2023-10-26 20:41:502023-10-26 20:41:50

Judging History

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

  • [2023-10-26 20:41:50]
  • 评测
  • 测评结果:WA
  • 用时:7ms
  • 内存:28456kb
  • [2023-10-26 20:41:50]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define _ <<" "<<
#define sz(x) ((int) (x).size())
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<vector<int>> Graph;
typedef vector<vector<pair<int,ll>>> Graphl;
const int maxn = 5e5 + 5;

set<int> G[maxn];
int n, m;
int deg[maxn];


struct DSU {
    vector<int> fa, s;
    int cnt; // connect area number
    DSU(int n=1) : fa(n+1,-1), s(n+1,1), cnt(n) {}
    int size(int x) { return s[find(x)]; }
    int find(int x) { return -1 == fa[x] ? x : fa[x] = find(fa[x]); }
    bool connect(int x, int y) {
        // cout << "C" _ x _ y << endl;
        x = find(x), y = find(y);  
        if (x == y) return false;
        if (s[x] > s[y]) swap(x, y);
        cnt--; s[y] += s[x]; 
        fa[x] = y;
        return true;
    }
};

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    vector<pii> B;
    vector<int> ck(m+1);
    vector<int> vis(m+1);
    for (int i = 1;i <= n; ++i) {
        int x,y; cin >> x >> y;
        vis[x] = vis[y] = 1;
        if (x == y) ck[x] = 1;
        else {
            if (x > y) swap(x,y);
            B.push_back({x,y});
        }
    }
    int tot = 0;
    for (int i = 1;i <= m; ++i) {
        tot += vis[i];
    }
    sort(B.begin(),B.end());
    B.erase(unique(B.begin(),B.end()),B.end());
    // build simple graph
    for (auto [a,b] : B) {
        deg[a]++; deg[b]++;
        G[a].insert(b);
        G[b].insert(a);
    }
    // delete leaves
    vector<int> todel;
    for (int i = 1;i <= m; ++i) if (deg[i] == 1 && !ck[i]) {
        todel.push_back(i);
    }
    for (auto i : todel) {
        tot--;
        vis[i] = 0;
        deg[i]--;
        for (auto v : G[i]) G[v].erase(i), deg[v]--;
    }
    for (int i = 1;i <= m; ++i) {
        if (deg[i] > 2) {
            cout << "impossible\n";
            return 0;
        } 
    }
    vector<pii> C;
    for (auto [a,b] : B) if (vis[a] && vis[b]) {
        C.push_back({a,b});
    }
    DSU dsu(m);
    set<int> cy;
    for (auto [a,b] : C) {
        if (!dsu.connect(a, b)) {      
            cy.insert(dsu.find(a));
        }
    }

    if (sz(cy)) {
        // for(auto x : cy) cout << x<< endl;
        if (sz(cy) == 1) {
            int k = *cy.begin();
            if (dsu.size(k) == tot) {
                cout << "possible\n";
                return 0;
            }
        }
        // ?s

        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: 0ms
memory: 27928kb

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

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: 6ms
memory: 27752kb

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: 6ms
memory: 27440kb

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

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

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: 5ms
memory: 27148kb

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

input:

4 5
1 2
3 4
4 5
5 3

output:

possible

result:

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