QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#254115#1344. Rooks GameMohamed308RE 1ms5360kbC++203.5kb2023-11-18 00:54:022023-11-18 00:54:03

Judging History

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

  • [2023-11-18 00:54:03]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:5360kb
  • [2023-11-18 00:54:02]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int N = 1e3+5;
deque<pair<int, int>> x[N], y[N];
set<int> adj[N], adj2[N];
set<pair<int, int>> X[N], Y[N];
int indeg[N], indeg2[N], R[N], C[N];

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int n, m; cin >> n >> m;
    for(int i=0; i<m; i++){
        int a, b; cin >> a >> b;
        R[i] = a;
        C[i] = b;
        x[a].push_back({b, i});
        y[b].push_back({a, i});
        X[a].insert({b, i});
        Y[b].insert({a, i});
    }
    for(auto &h : x){
        sort(h.begin(), h.end());
    }
    for(auto &h : y){
        sort(h.begin(), h.end());
    }
    for(auto &h : x){
        for(int i=0; i<(int)h.size()-1; i++){
            indeg[h[i].second]++;
            indeg[h[i+1].second]++;
            adj[h[i].second].insert(h[i+1].second);
            adj[h[i+1].second].insert(h[i].second);
        }
    }
    for(auto &h : y){
        for(int i=0; i<(int)h.size()-1; i++){
            indeg[h[i].second]++;
            indeg[h[i+1].second]++;
            adj[h[i].second].insert(h[i+1].second);
            adj[h[i+1].second].insert(h[i].second);
        }
    }

    // for(int i=0; i<N; i++){
    //     if(adj[i].empty())continue;
    //     auto x = adj[i];
    //     for(auto y : x){
    //         cout << y << " " << i << '\n';
    //     }
    // }

    int ans1 = 0, ans2 = 0;
    set<pair<int, int>> st;
    for(int i=0; i<n; i++){
        if(indeg[i] == 0)continue;
        st.insert({indeg[i], i});
    }
    for(int i=0; i<N; i++)indeg2[i] = indeg[i], adj2[i] = adj[i];
    vector<int> vis(n);
    while(st.size()){
        int u = st.rbegin() -> second;
        st.erase({indeg[u], u});
        vis[u] = 1;
        ans1++;
        if(X[R[u]].begin()->second == u){
            auto nxt = X[R[u]].begin();
            nxt++;
            int v = nxt->second;
            st.erase({indeg[v], v});
            indeg[v]--;
            if(indeg[v] > 0)
                st.insert({indeg[v], v});
        } else if(X[R[u]].rbegin()->second == u){
            auto prev = X[R[u]].rbegin();
            prev++;
            int v = prev->second;
            st.erase({indeg[v], v});
            indeg[v]--;
            if(indeg[v] > 0)
                st.insert({indeg[v], v});
        } 
        if(Y[C[u]].begin()->second == u){
            auto nxt = Y[C[u]].begin();
            nxt++;
            int v = nxt->second;
            st.erase({indeg[v], v});
            indeg[v]--;
            if(indeg[v] > 0)
                st.insert({indeg[v], v});
        } else if(Y[C[u]].rbegin()->second == u){
            auto prev = Y[C[u]].rbegin();
            prev++;
            int v = prev->second;
            st.erase({indeg[v], v});
            indeg[v]--;
            if(indeg[v] > 0)
                st.insert({indeg[v], v});
        } 
        X[R[u]].erase({C[u], u});
        Y[C[u]].erase({R[u], u});
    }
    while(st.size())st.erase(st.begin());
    for(int i=0; i<N; i++)indeg[i] = indeg2[i], adj[i] = adj2[i];
    for(int i=0; i<n; i++){
        if(indeg[i] == 0)continue;
        st.insert({indeg[i], i});
    }
    
    while(st.size()){
        int u = st.begin() -> second;
        st.erase({indeg[u], u});
        ans2++;
        for(auto v : adj[u]){
            st.erase({indeg[v], v});
            indeg[v]--;
            if(indeg[v])
                st.insert({indeg[v], v});
            adj[v].erase(u);
        }
    }
    cout << ans1 << " " << ans2 << endl;

}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

8 3
1 1
1 8
8 8

output:

1 2

result:

ok 2 number(s): "1 2"

Test #2:

score: 0
Accepted
time: 1ms
memory: 5104kb

input:

8 4
1 1
1 8
8 8
8 1

output:

2 3

result:

ok 2 number(s): "2 3"

Test #3:

score: 0
Accepted
time: 1ms
memory: 5192kb

input:

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

output:

0 0

result:

ok 2 number(s): "0 0"

Test #4:

score: 0
Accepted
time: 1ms
memory: 5192kb

input:

100 1
100 100

output:

0 0

result:

ok 2 number(s): "0 0"

Test #5:

score: 0
Accepted
time: 1ms
memory: 5360kb

input:

10 12
1 2
1 4
1 6
3 6
5 6
10 7
8 7
6 7
4 7
2 7
7 3
9 5

output:

7 8

result:

ok 2 number(s): "7 8"

Test #6:

score: -100
Runtime Error

input:

9 12
2 3
5 8
9 7
9 9
1 2
7 8
9 1
3 3
2 9
4 4
4 6
1 6

output:

8 9

result: