QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#362093 | #8507. Clever Cell Choices | ucup-team3099# | WA | 1ms | 3608kb | C++14 | 7.7kb | 2024-03-23 14:08:29 | 2024-03-23 14:08:29 |
Judging History
answer
#include <iostream>
#include <vector>
#include <chrono>
#include <random>
#include <cassert>
#include <queue>
std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
template <class T = int>
class Dinic {
public:
struct Edge {
Edge(int a, T b){to = a;cap = b;}
int to;
T cap;
};
Dinic(int _n) : n(_n) {
edges.resize(n);
}
T maxFlow(int src, int sink) {
T ans = 0;
while(bfs(src, sink)) {
// maybe random shuffle edges against bad cases?
T flow;
pt = std::vector<int>(n, 0);
while((flow = dfs(src, sink))) {
ans += flow;
}
}
return ans;
}
void addEdge(int from, int to, T cap, T other = 0) {
edges[from].push_back((int) list.size());
list.push_back(Edge(to, cap));
edges[to].push_back((int) list.size());
list.push_back(Edge(from, other));
}
bool inCut(int u) const { return h[u] < n; }
int size() const { return n; }
//private:
int n;
std::vector<std::vector<int> > edges;
std::vector<Edge> list;
std::vector<int> h, pt;
T dfs(int on, int sink, T flow = 1e9) {
if(flow == 0) {
return 0;
} if(on == sink) {
return flow;
}
for(; pt[on] < (int) edges[on].size(); pt[on]++) {
int cur = edges[on][pt[on]];
if(h[on] + 1 != h[list[cur].to]) {
continue;
}
T got = dfs(list[cur].to, sink, std::min(flow, list[cur].cap));
if(got) {
list[cur].cap -= got;
list[cur ^ 1].cap += got;
return got;
}
}
return 0;
}
bool bfs(int src, int sink) {
h = std::vector<int>(n, n);
h[src] = 0;
std::queue<int> q;
q.push(src);
while(!q.empty()) {
int on = q.front();
q.pop();
for(auto a : edges[on]) {
if(list[a].cap == 0) {
continue;
}
int to = list[a].to;
if(h[to] > h[on] + 1) {
h[to] = h[on] + 1;
q.push(to);
}
}
}
return h[sink] < n;
}
};
int main() {
std::ios_base::sync_with_stdio(false); std::cin.tie(NULL);
int n, m;
std::cin >> n >> m;
std::vector<std::string> mat(n);
for(int i = 0; i < n; i++) {
std::cin >> mat[i];
}
Dinic<int> graph(n * m + 2);
int src = n*m;
int sink = src+1;
for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) if(mat[i][j] == '.') {
if((i + j) % 2 == 0) {
graph.addEdge(src, i*m + j, 1);
int dx = 0, dy = 1;
for(int dir = 0; dir < 4; dir++) {
std::swap(dx, dy);
dx = -dx;
int x = i + dx, y = j + dy;
if(0 <= x && x < n && 0 <= y && y < m && mat[x][y] == '.') {
graph.addEdge(i*m+j, x*m+y, 1);
}
}
} else {
graph.addEdge(i*m + j, sink, 1);
}
}
auto ori = graph;
int flow = graph.maxFlow(src, sink);
std::vector<bool> got(n*m, false);
for(auto id : graph.edges[src]) {
int to = graph.list[id].to;
if(graph.list[id].cap == 0) {
got[to] = true;
}
}
for(auto id : graph.edges[sink]) {
int to = graph.list[id].to;
if(graph.list[id].cap == 1) {
got[to] = true;
}
}
auto otherGot = got;
int ans = 0;
for(int i = 0; i < n*m; i++) {
if(got[i]) {
auto tmp = ori;
for(int j = 0; j < (int) tmp.list.size(); j++) if(tmp.list[j].to == i) {
tmp.list[j].cap = tmp.list[j^1].cap = 0;
}
if(tmp.maxFlow(src, sink) != flow) {
otherGot[i] = false;
} else {
ans++;
}
}
}
std::cout << ans << '\n';
return 0;
ans = 0;
for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) {
if(mat[i][j] == '.') {
std::vector<std::pair<int, int>> comp;
comp.emplace_back(i, j);
mat[i][j] = '#';
int totFreq[2] = {0, 0};
int gotFreq[2] = {0, 0};
int otherFreq[2] = {0, 0};
for(int k = 0; k < (int) comp.size(); k++) {
auto [x, y] = comp[k];
//std::cout << "visit (" << x << ", " << y << ") starting at (" << i << ", " << j << ")\n";
totFreq[(x+y)%2]++;
if(got[x*m+y]) {
gotFreq[(x+y)%2]++;
}
if(otherGot[x*m+y]) {
otherFreq[(x+y)%2]++;
}
int dx = 0, dy = 1;
for(int dir = 0; dir < 4; dir++) {
std::swap(dx, dy);
dx = -dx;
int toX = x + dx, toY = y + dy;
if(0 <= toX && toX < n && 0 <= toY && toY < m && mat[toX][toY] == '.') {
mat[toX][toY] = '#';
comp.emplace_back(toX, toY);
}
}
}
if(gotFreq[0] != totFreq[0]) {
ans += otherFreq[0];
}
if(gotFreq[1] != totFreq[1]) {
ans += otherFreq[1];
}
}
}
std::cout << ans << '\n';
}
/*
NEVER FORGET TO:
Look at the problem's constraints before coding.
How to cheese cf:
Find a lower bound or upper bound for the problem. Have faith that it is the answer of the problem.
If it isn't the answer, have more faith or change to another bound god by looking for a better bound.
Trust guesses. Who has time to think? If people in div2 AC the problem it requires no proof since people don't prove things.
You must draw cases. Thinking gets you nowhere, so draw cases and reach illogical conclusions from them.
Sometimes drawing cases is bad because it takes too much time. Faster is to not think at all and just code a bruteforce solution.
This is called "law of small numbers". If something works for small numbers, surely it works for big numbers.
https://en.wikipedia.org/wiki/Faulty_generalization#Hasty_generalization don't mind the "faulty" part of it, in competitive programming mistakes are lightly punished
Don't think about them being right or not, cf is a battle of intuition only.
Be as stupid as possible in implementation. Trying to be smart is an easy way to get WA.
Think about 2x2 cases for matrix problems and hope that everything works for the general case.
Find a necessary condition and trust it to be sufficient. They're basically the same thing.
Heuristics might speed up your code. Forget about complexity, it's only about ACing and not proving that your solution is good.
For paths in a grid starting at (1, i) or something like that, assume that they never cross and do D&C
Consider doing problems in reverse order of queries/updates
For combinatorics problems, consider symmetry
General strategy (MUST DO):
Try to solve the problem with more restricted constraints.
About testing:
Test n=1, a[i]=1, a[i]=n, etc. Basically, test low values. No need to test if pretests are strong, but if you get WA it's good.
This isn't a joke. Do it if you get stuck. It's shit practice in my opinion, but do it if you want AC.
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3608kb
input:
3 3 #.# ... #.#
output:
1
result:
wrong answer 1st numbers differ - expected: '4', found: '1'