QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#599098#9424. Stop the Castle 2ucup-team008#WA 66ms5036kbC++178.7kb2024-09-29 01:18:272024-09-29 01:18:28

Judging History

This is the latest submission verdict.

  • [2024-09-29 01:18:28]
  • Judged
  • Verdict: WA
  • Time: 66ms
  • Memory: 5036kb
  • [2024-09-29 01:18:27]
  • Submitted

answer

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <complex>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>

using namespace std;

// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
typedef vector<int> vi;
#define f first
#define s second
#define derr if(0) cerr
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<" [" << #x << "] = ["; _print(x); cerr << "\e[39m" << flush;
// END NO SAD

template<class Fun>
class y_combinator_result {
  Fun fun_;
public:
  template<class T>
  explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}

  template<class ...Args>
  decltype(auto) operator()(Args &&...args) {
    return fun_(std::ref(*this), std::forward<Args>(args)...);
  }
};

template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
  return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}

template<class T>
bool updmin(T& a, T b) {
  if(b < a) {
    a = b;
    return true;
  }
  return false;
}
template<class T>
bool updmax(T& a, T b) {
  if(b > a) {
    a = b;
    return true;
  }
  return false;
}
typedef int64_t ll;
typedef long double ld;

bool dfs(int a, int L, vector<vi>& g, vi& btoa, vi& A, vi& B) {
	if (A[a] != L) return 0;
	A[a] = -1;
	for (int b : g[a]) if (B[b] == L + 1) {
		B[b] = 0;
		if (btoa[b] == -1 || dfs(btoa[b], L + 1, g, btoa, A, B))
			return btoa[b] = a, 1;
	}
	return 0;
}

int hopcroftKarp(vector<vi>& g, vi& btoa) {
	int res = 0;
	vi A(g.size()), B(btoa.size()), cur, next;
	for (;;) {
		fill(all(A), 0);
		fill(all(B), 0);
		/// Find the starting nodes for BFS (i.e. layer 0).
		cur.clear();
		for (int a : btoa) if(a != -1) A[a] = -1;
		rep(a,0,sz(g)) if(A[a] == 0) cur.push_back(a);
		/// Find all layers using bfs.
		for (int lay = 1;; lay++) {
			bool islast = 0;
			next.clear();
			for (int a : cur) for (int b : g[a]) {
				if (btoa[b] == -1) {
					B[b] = lay;
					islast = 1;
				}
				else if (btoa[b] != a && !B[b]) {
					B[b] = lay;
					next.push_back(btoa[b]);
				}
			}
			if (islast) break;
			if (next.empty()) return res;
			for (int a : next) A[a] = lay;
			cur.swap(next);
		}
		/// Use DFS to scan for augmenting paths.
		rep(a,0,sz(g))
			res += dfs(a, 0, g, btoa, A, B);
	}
}

void rsolve() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<array<int, 2>> rooks(n), obstacles(m);
  vector<int> rows, cols;
  for(auto& x: rooks) {
    cin >> x[0] >> x[1];
    rows.pb(x[0]);
    cols.pb(x[1]);
  }
  for(auto& x: obstacles) {
    cin >> x[0] >> x[1];
    rows.pb(x[0]);
    cols.pb(x[1]);
  }
  sort(all(rows)); rows.erase(unique(all(rows)), rows.end());
  sort(all(cols)); cols.erase(unique(all(cols)), cols.end());
  for(auto& x: rooks) {
    x[0] = lower_bound(all(rows), x[0]) - rows.begin();
    x[1] = lower_bound(all(cols), x[1]) - cols.begin();
  }
  for(auto& x: obstacles) {
    x[0] = lower_bound(all(rows), x[0]) - rows.begin();
    x[1] = lower_bound(all(cols), x[1]) - cols.begin();
  }
  int r = sz(rows);
  int c = sz(cols);
  vector<vector<int>> rowtocols(r), coltorows(c);
  for(auto [x, y]: rooks) {
    rowtocols[x].pb(y);
    coltorows[y].pb(x);
  }
  for(int i = 0; i < r; i++) sort(all(rowtocols[i]));
  for(int i = 0; i < c; i++) sort(all(coltorows[i]));
  vector<int> rowdp(r), coldp(c);
  for(int i = 1; i < r; i++) rowdp[i] = rowdp[i-1] + max(0, sz(rowtocols[i-1])-1);
  for(int i = 1; i < c; i++) coldp[i] = coldp[i-1] + max(0, sz(coltorows[i-1])-1);
  int lhsnodes = rowdp[r-1] + max(0, sz(rowtocols[r-1]) - 1);
  int rhsnodes = coldp[c-1] + max(0, sz(coltorows[c-1]) - 1);
  int ans = lhsnodes + rhsnodes;
  vector<vector<int>> lhsgraph(lhsnodes);
  vector<int> rhsreturn(rhsnodes, -1);
  vector<array<int, 2>> matching(sz(obstacles), array<int, 2>({-1, -1}));
  for(int oidx = 0; oidx < sz(obstacles); oidx++) {
    auto [x, y] = obstacles[oidx];
    if(sz(rowtocols[x]) <= 1 || sz(coltorows[y]) <= 1) continue;
    int posinrow = lower_bound(all(rowtocols[x]), y) - rowtocols[x].begin();
    int posincol = lower_bound(all(coltorows[y]), x) - coltorows[y].begin();
    if(posinrow == 0 || posincol == 0) continue;
    if(posinrow < sz(rowtocols[x]) && posincol < sz(coltorows[y])) {
      // intersection found!
      int lhsnode = rowdp[x] + posinrow - 1;
      int rhsnode = coldp[y] + posincol - 1;
      lhsgraph[lhsnode].pb(rhsnode);
      matching[oidx][0] = lhsnode;
      matching[oidx][1] = rhsnode;
    }
  }
  vector<bool> keepv(m, false);
  hopcroftKarp(lhsgraph, rhsreturn);
  vector<array<int, 2>> matched;
  for(int j = 0; j < sz(rhsreturn); j++) {
    int i = rhsreturn[j];
    if(i >= 0) matched.pb({i, j});
  }
  sort(all(matched));
  int keep = m - k;
  vector<bool> rowhandled(lhsnodes), colhandled(rhsnodes);
  for(int oidx = 0; keep > 0 && oidx < sz(obstacles); oidx++) {
    auto [x, y] = obstacles[oidx];
    if(sz(rowtocols[x]) <= 1 || sz(coltorows[y]) <= 1) continue;
    int posinrow = lower_bound(all(rowtocols[x]), y) - rowtocols[x].begin();
    int posincol = lower_bound(all(coltorows[y]), x) - coltorows[y].begin();
    if(posinrow == 0 || posincol == 0) continue;
    if(posinrow < sz(rowtocols[x]) && posincol < sz(coltorows[y])) {
      // intersection found!
      int lhsnode = rowdp[x] + posinrow - 1;
      int rhsnode = coldp[y] + posincol - 1;
      if(binary_search(all(matched), array<int, 2>({lhsnode, rhsnode}))) {
        keep--; ans -= 2; keepv[oidx] = true;
        assert(!rowhandled[lhsnode]); assert(!colhandled[rhsnode]);
        rowhandled[lhsnode] = true; colhandled[rhsnode] = true;
      }
    }
  }
  for(int oidx = 0; keep > 0 && oidx < sz(obstacles); oidx++) {
    if(keepv[oidx]) continue;
    auto [x, y] = obstacles[oidx];
    int posinrow = lower_bound(all(rowtocols[x]), y) - rowtocols[x].begin();
    int posincol = lower_bound(all(coltorows[y]), x) - coltorows[y].begin();
    if(posinrow > 0 && posinrow < sz(rowtocols[x])) {
      int lhsnode = rowdp[x] + posinrow - 1;
      if(!rowhandled[lhsnode]) {
        keep--; ans -= 1;
        assert(!rowhandled[lhsnode]);
        rowhandled[lhsnode] = true;
      }
    }
  }
  for(int oidx = 0; keep > 0 && oidx < sz(obstacles); oidx++) {
    if(keepv[oidx]) continue;
    auto [x, y] = obstacles[oidx];
    int posincol = lower_bound(all(coltorows[y]), x) - coltorows[y].begin();
    if(posincol > 0 && posincol < sz(coltorows[y])) {
      int rhsnode = coldp[y] + posincol - 1;
      if(!colhandled[rhsnode]) {
        keep--; ans -= 1;
        assert(!colhandled[rhsnode]);
        colhandled[rhsnode] = true;
      }
    }
  }
  for(int oidx = 0; keep > 0 && oidx < sz(obstacles); oidx++) {
    if(keepv[oidx]) continue;
    keepv[oidx] = true; keep--;
  }
  cout << ans << "\n";
  for(int i = 0; i < m; i++) {
    if(!keepv[i]) cout << (i+1) << " ";
  }
  cout << "\n";
}
void solve() {
  int t;
  cin >> t;
  while(t--) rsolve();
}

// what would chika do
// are there edge cases (N=1?)
// are array sizes proper (scaled by proper constant, for example 2* for koosaga tree)
// integer overflow?
// DS reset properly between test cases
// are you doing geometry in floating points
// are you not using modint when you should

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
}

詳細信息

Test #1:

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

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

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 14 15 16 17 
15
2 3 
0
3 4 5 6 
0
2 3 4 5 6 7 8 9 
11
1 2 3 
8
1 2 3 
0
1 2 3 4 5 6 7 8 9 10 11 12 
1
5 6 7 8 9 10 11 12 
8
16 17 18 19 
1
1 2 3 4 5 6 7 8 
7
6 7 8 
10
13 14 15 
1
9 10 11 12 13 14 15 16 17 18 19 20 
0
1 
1
2 3 
0
5 6 7 
7
7 8 9 10 11 12 13 14 15 
2
8 9 10...

result:

wrong answer Participant states the answer to be 7 but is actually 8 (test case 1)