QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#326357 | #2563. Curly Racetrack | bear0131 | WA | 0ms | 3584kb | C++14 | 5.0kb | 2024-02-12 22:44:24 | 2024-02-12 22:44:25 |
Judging History
answer
#include<bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef array<int, 3> ai3;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int Mod = 1e9 + 7;
inline int sign(int a){ return (a&1) ? (Mod-1) : 1; }
inline void uadd(int &a, int b){ a += b-Mod; a += (a>>31) & Mod; }
inline void usub(int &a, int b){ a -= b, a += (a>>31) & Mod; }
inline void umul(int &a, int b){ a = (int)(1ll * a * b % Mod); }
inline int add(int a, int b){ a += b-Mod; a += (a>>31) & Mod; return a; }
inline int sub(int a, int b){ a -= b, a += (a>>31) & Mod; return a; }
inline int mul(int a, int b){ a = (int)(1ll * a * b % Mod); return a; }
int qpow(int b, ll p){ int ret = 1; while(p){ if(p&1) umul(ret, b); umul(b, b), p >>= 1; } return ret; }
const int fN = 100;
int fact[fN], invfact[fN], pw2[fN], invpw2[fN], inv[fN];
void initfact(int n){
pw2[0] = 1; for(int i = 1; i <= n; ++i) pw2[i] = mul(pw2[i-1], 2);
invpw2[0] = 1; for(int i = 1; i <= n; ++i) invpw2[i] = mul(invpw2[i-1], (Mod+1) / 2);
fact[0] = 1; for(int i = 1; i <= n; ++i) fact[i] = mul(fact[i-1], i);
invfact[n] = qpow(fact[n], Mod-2); for(int i = n; i > 0; --i) invfact[i-1] = mul(invfact[i], i);
for(int i = 1; i <= n; ++i) inv[i] = mul(invfact[i], fact[i-1]);
}
int binom(int n, int k){ return (k < 0 || k > n) ? 0 : mul(fact[n], mul(invfact[k], invfact[n-k])); }
const double pi = acos(-1);
inline void chmax(int &a, int b){ (b>a) ? (a=b) : 0; }
inline void chmin(int &a, int b){ (b<a) ? (a=b) : 0; }
namespace mf{
int cn, cur[100100], hd[100100]; int dis[100100];
int ce, to[100100], nxt[100100]; int cap[100100];
void clr(int n){
//cout << "------clr-------" << endl;
cn = n, ce = 0;
fill(hd, hd+cn, -1);
}
void ae(int f, int t, int c){
//cout << "ae " << f << " " << t << " " << c << " " << endl;
nxt[ce] = hd[f], cap[ce] = c, to[ce] = t, hd[f] = ce++;
nxt[ce] = hd[t], cap[ce] = 0, to[ce] = f, hd[t] = ce++;
}
bool bfs(int S, int T){
fill(dis, dis+cn, inf);
dis[S] = 0;
queue<int> q;
q.emplace(S);
while(!q.empty()){
int u = q.front(); q.pop();
//cout << "bfs " << u << endl;
for(int eid = hd[u]; eid >= 0; eid = nxt[eid]){
int t = to[eid];
if(dis[t] > dis[u] + 1 && cap[eid]){
dis[t] = dis[u] + 1;
q.emplace(t);
}
}
}
//cout << cn << " " << S << " " << T << ": " << dis[T] << endl;
return dis[T] < inf;
}
int dfs(int u, int dest, int flow){
//cout << "dfs " << u << endl;
if(u == dest) return flow;
int nf = flow;
for(int &eid = cur[u]; eid >= 0; eid = nxt[eid]){
int t = to[eid];
if(cap[eid] && dis[t] == dis[u] + 1){
int ret = dfs(t, dest, min(nf, cap[eid]));
nf -= ret, cap[eid] -= ret, cap[eid^1] += ret;
if(!nf) break;
}
}
return flow - nf;
}
int mf(int S, int T){
int ans = 0;
while(bfs(S, T)){
rep(i, cn) cur[i] = hd[i];
ans += dfs(S, T, inf);
//cout << ans << endl;
}
return ans;
}
}
int n, m;
string s[111];
int cx, cy, bx[111][111], by[111][111];
int xid[10010], yid[10010], xhas[10010], yhas[10010];
int main(){
//freopen("pipe.in", "r", stdin);
//freopen("pipe.out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
rep(i, n) cin >> s[i];
rep(i, n) rep(j, m){
bx[i][j] = cx;
if(j == m-1 || s[i][j] == '1' || s[i][j] == '2' || s[i][j+1] == '3' || s[i][j+1] == '4')
++cx;
}
rep(j, m) rep(i, n){
by[i][j] = cy;
if(i == n-1 || s[i][j] == '1' || s[i][j] == '3' || s[i+1][j] == '2' || s[i+1][j] == '4')
++cy;
}
//rep(i, n) rep(j, m) cout << bx[i][j] << (j == m-1 ? '\n' : ' ');
//cout << "\n";
//rep(i, n) rep(j, m) cout << by[i][j] << (j == m-1 ? '\n' : ' ');
//cout << "\n";
rep(i, n) rep(j, m) if(isdigit(s[i][j])) s[i][j] = 'o';
cx = cy = 0;
rep(i, n){
for(int j = 0; j < m; ){
int r = j, has = 0;
while(r < m && bx[i][j] == bx[i][r]) has |= (s[i][r++] == 'x');
xid[bx[i][j]] = (((r - j) % 2 == 1 && !has) ? cx++ : -1);
j = r;
}
}
rep(j, m){
for(int i = 0; i < n; ){
int r = i, has = 0;
while(r < n && by[r][j] == by[i][j]) has |= (s[r++][j] == 'x');
yid[by[i][j]] = (((r - i) % 2 == 1 && !has) ? cy++ : -1);
i = r;
}
}
return 0;
//rep(i, n) rep(j, m) cout << xid[bx[i][j]] << (j == m-1 ? '\n' : ' ');
//cout << "\n";
//rep(i, n) rep(j, m) cout << yid[by[i][j]] << (j == m-1 ? '\n' : ' ');
//cout << "\n";
mf::clr(cx + cy + 2);
rep(i, cx) mf::ae(0, 2 + i, 1);
rep(i, cy) mf::ae(2 + cx + i, 1, 1);
rep(i, n) rep(j, m) if(s[i][j] != 'o'){
int nx = xid[bx[i][j]], ny = yid[by[i][j]];
if(nx >= 0 && ny >= 0) mf::ae(2 + nx, 2 + cx + ny, 1);
xhas[nx] = yhas[ny] = 1;
}
rep(i, cx) if(!xhas[i]) return cout << "-1\n", 0;
rep(i, cy) if(!yhas[i]) return cout << "-1\n", 0;
int ans = 0;
rep(i, n) rep(j, m) ans += (s[i][j] != 'x');
ans -= (cx + cy - mf::mf(0, 1));
cout << ans << "\n";
return 0;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3584kb
input:
4 5 4...x .2..2 .o1x. 3..3o
output:
result:
wrong answer Answer contains longer sequence [length = 1], but output contains 0 elements