QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#612257#9424. Stop the Castle 2yyyyxhWA 66ms5780kbC++146.9kb2024-10-05 09:53:492024-10-05 09:53:50

Judging History

This is the latest submission verdict.

  • [2024-10-05 09:53:50]
  • Judged
  • Verdict: WA
  • Time: 66ms
  • Memory: 5780kb
  • [2024-10-05 09:53:49]
  • Submitted

answer

#include <map>
#include <cstdio>

#include <algorithm>
#include <cassert>
#include <limits>
#include <queue>
#define fi first
#define se second


#include <vector>

namespace atcoder {

namespace internal {

template <class T> struct simple_queue {
    std::vector<T> payload;
    int pos = 0;
    void reserve(int n) { payload.reserve(n); }
    int size() const { return int(payload.size()) - pos; }
    bool empty() const { return pos == int(payload.size()); }
    void push(const T& t) { payload.push_back(t); }
    T& front() { return payload[pos]; }
    void clear() {
        payload.clear();
        pos = 0;
    }
    void pop() { pos++; }
};

}  // namespace internal

}  // namespace atcoder


namespace atcoder {

template <class Cap> struct mf_graph {
  public:
    mf_graph() : _n(0) {}
    explicit mf_graph(int n) : _n(n), g(n) {}

    int add_edge(int from, int to, Cap cap) {
        assert(0 <= from && from < _n);
        assert(0 <= to && to < _n);
        assert(0 <= cap);
        int m = int(pos.size());
        pos.push_back({from, int(g[from].size())});
        int from_id = int(g[from].size());
        int to_id = int(g[to].size());
        if (from == to) to_id++;
        g[from].push_back(_edge{to, to_id, cap});
        g[to].push_back(_edge{from, from_id, 0});
        return m;
    }

    struct edge {
        int from, to;
        Cap cap, flow;
    };

    edge get_edge(int i) {
        int m = int(pos.size());
        assert(0 <= i && i < m);
        auto _e = g[pos[i].first][pos[i].second];
        auto _re = g[_e.to][_e.rev];
        return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};
    }
    std::vector<edge> edges() {
        int m = int(pos.size());
        std::vector<edge> result;
        for (int i = 0; i < m; i++) {
            result.push_back(get_edge(i));
        }
        return result;
    }
    void change_edge(int i, Cap new_cap, Cap new_flow) {
        int m = int(pos.size());
        assert(0 <= i && i < m);
        assert(0 <= new_flow && new_flow <= new_cap);
        auto& _e = g[pos[i].first][pos[i].second];
        auto& _re = g[_e.to][_e.rev];
        _e.cap = new_cap - new_flow;
        _re.cap = new_flow;
    }

    Cap flow(int s, int t) {
        return flow(s, t, std::numeric_limits<Cap>::max());
    }
    Cap flow(int s, int t, Cap flow_limit) {
        assert(0 <= s && s < _n);
        assert(0 <= t && t < _n);
        assert(s != t);

        std::vector<int> level(_n), iter(_n);
        internal::simple_queue<int> que;

        auto bfs = [&]() {
            std::fill(level.begin(), level.end(), -1);
            level[s] = 0;
            que.clear();
            que.push(s);
            while (!que.empty()) {
                int v = que.front();
                que.pop();
                for (auto e : g[v]) {
                    if (e.cap == 0 || level[e.to] >= 0) continue;
                    level[e.to] = level[v] + 1;
                    if (e.to == t) return;
                    que.push(e.to);
                }
            }
        };
        auto dfs = [&](auto self, int v, Cap up) {
            if (v == s) return up;
            Cap res = 0;
            int level_v = level[v];
            for (int& i = iter[v]; i < int(g[v].size()); i++) {
                _edge& e = g[v][i];
                if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue;
                Cap d =
                    self(self, e.to, std::min(up - res, g[e.to][e.rev].cap));
                if (d <= 0) continue;
                g[v][i].cap += d;
                g[e.to][e.rev].cap -= d;
                res += d;
                if (res == up) return res;
            }
            level[v] = _n;
            return res;
        };

        Cap flow = 0;
        while (flow < flow_limit) {
            bfs();
            if (level[t] == -1) break;
            std::fill(iter.begin(), iter.end(), 0);
            Cap f = dfs(dfs, t, flow_limit - flow);
            if (!f) break;
            flow += f;
        }
        return flow;
    }

    std::vector<bool> min_cut(int s) {
        std::vector<bool> visited(_n);
        internal::simple_queue<int> que;
        que.push(s);
        while (!que.empty()) {
            int p = que.front();
            que.pop();
            visited[p] = true;
            for (auto e : g[p]) {
                if (e.cap && !visited[e.to]) {
                    visited[e.to] = true;
                    que.push(e.to);
                }
            }
        }
        return visited;
    }

  private:
    int _n;
    struct _edge {
        int to, rev;
        Cap cap;
    };
    std::vector<std::pair<int, int>> pos;
    std::vector<std::vector<_edge>> g;
};

}  // namespace atcoder

using namespace std;
int read(){
	char c=getchar();int x=0;
	while(c<48||c>57) c=getchar();
	do x=x*10+(c^48),c=getchar();
	while(c>=48&&c<=57);
	return x;
}
typedef vector<int> vi;
typedef pair<int,int> pii;
const int N=200003;
int n,m,k,cx,cy;
bool ans[N],del[N];
int eu[N],ev[N],eid[N];
map<pii,int> idx,idy;
int getidx(pii cur){
	auto it=idx.find(cur);
	if(it==idx.end()) return idx[cur]=++cx;
	return it->se;
}
int getidy(pii cur){
	auto it=idy.find(cur);
	if(it==idy.end()) return idy[cur]=++cy;
	return it->se;
}
void solve(){
	map<int,vi> mpx,mpy;
	idx.clear();idy.clear();
	n=read();m=read();k=read();cx=cy=0;
	for(int i=1;i<=n;++i){
		int x=read(),y=read();
		mpx[x].emplace_back(y);
		mpy[y].emplace_back(x);
	}
	int res=0;
	for(auto &[t,vc]:mpx) sort(vc.begin(),vc.end()),res+=vc.size()-1;
	for(auto &[t,vc]:mpy) sort(vc.begin(),vc.end()),res+=vc.size()-1;
	for(int i=1;i<=m;++i){
		int x=read(),y=read();
		auto itx=mpx.find(x);
		auto ity=mpy.find(y);
		eu[i]=ev[i]=0;ans[i]=0;
		if(itx!=mpx.end()){
			auto it=lower_bound(itx->se.begin(),itx->se.end(),y);
			if(it!=(itx->se).begin()&&it!=(itx->se).end()) eu[i]=getidx(pii(x,*it));
		}
		if(ity!=mpy.end()){
			auto it=lower_bound(ity->se.begin(),ity->se.end(),x);
			if(it!=(ity->se).begin()&&it!=(ity->se).end()) ev[i]=getidy(pii(y,*it));
		}
	}
	atcoder::mf_graph<int> mf(cx+cy+2);
	for(int i=1;i<=m;++i) if(eu[i]&&ev[i]) eid[i]=mf.add_edge(eu[i],cx+ev[i],1);
	for(int i=1;i<=cx;++i) mf.add_edge(0,i,1);
	for(int i=cx+1;i<=cx+cy;++i) mf.add_edge(i,cx+cy+1,1);
	for(int i=1;i<=cx+cy;++i) del[i]=0;
	int num=mf.flow(0,cx+cy+1);
	k=m-k;
	for(int i=1;i<=m;++i)
		if(eu[i]&&ev[i]&&mf.get_edge(eid[i]).flow&&k) res-=2,ans[i]=1,del[eu[i]]=del[ev[i]]=1,--k;
	for(int i=1;i<=m;++i){
		if(!ans[i]&&eu[i]&&k&&!del[eu[i]]) del[eu[i]]=1,--res,ans[i]=1,--k;
		if(!ans[i]&&ev[i]&&k&&!del[ev[i]+cx]) del[ev[i]+cx]=1,--res,ans[i]=1,--k;
	}
	for(int i=1;i<=m;++i) if(!ans[i]&&k) ans[i]=1,--k;
	printf("%d\n",res);
	for(int i=1;i<=m;++i) if(!ans[i]) printf("%d ",i);
	putchar('\n');
}
int main(){
	int tc=read();
	while(tc--) solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

4
2 3 5 6 
2
2 
0
2 3 

result:

ok ok 3 cases (3 test cases)

Test #2:

score: -100
Wrong Answer
time: 66ms
memory: 5780kb

input:

1224
11 17 14
7 3
4 2
8 13
3 15
3 4
5 11
10 2
3 3
8 6
7 11
2 3
10 4
1 3
12 1
2 5
11 9
11 6
11 10
8 15
1 5
9 14
4 11
1 6
10 7
7 6
11 4
8 4
1 11
18 3 2
14 8
2 14
13 13
9 12
14 12
5 6
8 1
10 5
8 6
8 9
6 6
7 5
12 11
6 11
13 5
1 10
7 6
14 5
6 15
2 4
11 1
1 6 4
14 14
13 9
9 3
10 12
7 5
8 13
9 14
1 9 8
4 9...

output:

7
3 4 5 6 7 8 9 10 11 12 13 15 16 17 
15
2 3 
0
3 4 5 6 
0
2 3 4 5 6 7 8 9 
11
1 3 
8
1 2 3 
0
1 2 3 4 5 6 7 8 9 10 11 12 
1
5 6 7 9 10 11 12 
8
17 18 19 
1
1 2 3 4 5 6 7 8 
7
6 8 
10
13 14 15 
1
10 11 12 13 14 15 16 17 18 19 20 
0
1 
1
2 3 
0
5 6 7 
7
8 12 13 14 15 
2
10 11 12 13 14 
4
3 4 5 6 7 8 ...

result:

wrong answer Jury has better answer. Participant's answer is 9 while jury's answer is 8 (test case 62)