QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#227050#5160. Kebab PizzaFyindWA 4ms17128kbC++232.6kb2023-10-26 20:21:172023-10-26 20:21:17

Judging History

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

  • [2023-10-26 20:21:17]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:17128kb
  • [2023-10-26 20:21:17]
  • 提交

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;

vector<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> island, 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) island.push_back(x), 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());
    for (auto [a,b] : B) {
        deg[a]++; deg[b]++;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    for (int i = 1;i <= m; ++i) if (deg[i] == 1 &&!ck[i]) {
        tot--;
    }
    
    DSU dsu(m);
    for (int i = 1;i <= m; ++i) if (deg[i]) {
        if (deg[i] > 1 || (deg[i]==1 && ck[i])) {
            int degp = 0;
            for (auto v : G[i]) if (deg[v] > 1 || (deg[v]==1 && ck[v])) {
                degp++;
            }
            if (degp > 2) {
                cout << "impossible\n";
                return 0;
            }
        }
    }
    set<int> cy;
    for (int i = 1;i <= m; ++i) if (deg[i]) {
        if (deg[i] > 1) {
            for (auto v : G[i]) if (deg[v] > 1 && i < v) {
                if (!dsu.connect(i, v)) {
                    
                    cy.insert(dsu.find(i));
                }
            }
        }
    }
    if (sz(cy)) {
        // for(auto x : cy) cout << x<< endl;
        int k = *cy.begin();
        if (dsu.size(k) == tot) {
            cout << "possible\n";
            return 0;
        }
        cout << "impossible\n";
        return 0;
    }
    cout << "possible\n";
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 4ms
memory: 17128kb

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

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

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

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

input:

4 4
1 2
2 1
3 4
4 3

output:

possible

result:

ok single line: 'possible'

Test #6:

score: 0
Accepted
time: 2ms
memory: 17064kb

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

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

input:

4 5
1 2
3 4
4 5
5 3

output:

possible

result:

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