QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#289771 | #7857. (-1,1)-Sumplete | ucup-team139# | TL | 1665ms | 796192kb | C++17 | 6.8kb | 2023-12-23 23:38:22 | 2023-12-23 23:38:22 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
template <class Cap> struct mf_graph {
struct simple_queue_int {
std::vector<int> 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 int &t) { payload.push_back(t); }
int &front() { return payload[pos]; }
void clear() {
payload.clear();
pos = 0;
}
void pop() { pos++; }
};
mf_graph() : _n(0) {}
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;
}
std::vector<int> level, iter;
simple_queue_int que;
void _bfs(int s, int t) {
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);
}
}
}
Cap _dfs(int v, int s, 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 = _dfs(e.to, s, 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(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);
level.assign(_n, 0), iter.assign(_n, 0);
que.clear();
Cap flow = 0;
while (flow < flow_limit) {
_bfs(s, t);
if (level[t] == -1) break;
std::fill(iter.begin(), iter.end(), 0);
Cap f = _dfs(t, s, flow_limit - flow);
if (!f) break;
flow += f;
}
return flow;
}
std::vector<bool> min_cut(int s) {
std::vector<bool> visited(_n);
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;
}
void dump_graphviz(std::string filename = "maxflow") const {
std::ofstream ss(filename + ".DOT");
ss << "digraph{\n";
for (int i = 0; i < _n; i++) {
for (const auto &e : g[i]) {
if (e.cap > 0) ss << i << "->" << e.to << "[label=" << e.cap << "];\n";
}
}
ss << "}\n";
ss.close();
return;
}
int _n;
struct _edge {
int to, rev;
Cap cap;
};
std::vector<std::pair<int, int>> pos;
std::vector<std::vector<_edge>> g;
};
void solve(int t){
int n;
cin>>n;
vector mat(n,vector(n,'.'));
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>mat[i][j];
}
}
vector r(n,0),c(n,0);
for(int i=0;i<n;i++)cin>>r[i];
for(int i=0;i<n;i++)cin>>c[i];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(mat[i][j]=='-'){
r[i]++;
c[j]++;
}
}
}
long long totr=0,totc=0;
for(int i=0;i<n;i++){
if(r[i]<0 || c[i]<0){
cout<<"No\n";
return;
}
totr+=r[i];
totc+=c[i];
}
if(totr!=totc){
cout<<"No\n";
}else{
mf_graph<int> ds(2*n+2);
int source = 2*n;
int sink = 2*n+1;
//ds.init(2*n+2);
for(int i=0;i<n;i++){
ds.add_edge(source,i,r[i]);
ds.add_edge(n+i,sink,c[i]);
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
ds.add_edge(i,n+j,1);
}
}
if(ds.flow(source,sink)!=totr){
cout<<"No\n";
}else{
vector ok(n,vector(n,false));
for(auto i : ds.edges()){
if(i.from<n && i.to<2*n && i.flow==1){
ok[i.from][i.to-n]=true;
}
}
cout<<"Yes\n";
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if((ok[i][j]^(mat[i][j]=='-'))==false){
cout<<"0";
}else{
cout<<"1";
}
}
cout<<"\n";
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t=1;
//cin>>t;
for(int i=1;i<=t;i++)solve(i);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3540kb
input:
3 +-+ -++ +-+ 1 1 1 1 -1 3
output:
Yes 001 001 111
result:
ok n=3
Test #2:
score: 0
Accepted
time: 0ms
memory: 3832kb
input:
3 --- -++ +++ -2 -1 0 -2 -1 0
output:
Yes 110 100 000
result:
ok n=3
Test #3:
score: 0
Accepted
time: 0ms
memory: 3608kb
input:
3 +-+ -++ ++- 1 0 2 2 2 -1
output:
No
result:
ok n=3
Test #4:
score: 0
Accepted
time: 0ms
memory: 3652kb
input:
1 - -1 1
output:
No
result:
ok n=1
Test #5:
score: 0
Accepted
time: 0ms
memory: 3572kb
input:
1 - 0 0
output:
Yes 0
result:
ok n=1
Test #6:
score: 0
Accepted
time: 0ms
memory: 3672kb
input:
20 +-------+-----+++-++ -+-++++----++-++-++- -+++--+---+--+-++--- -+++-+--+----++---+- +++-+-++++++-+-+---+ -++-----+----++++++- +-++--+++++-++-+---- +-+----+---+-+++--+- +++++-+++++----+--+- ------++++---+--++-- ++++--------++++--+- -+-+-++++-+-++-++--+ ---+-++---+-++-++--- +-++++-++----+-+++-- +-+...
output:
Yes 01111100100100101111 10101110000100100011 10010010000111101101 10010100011111010111 00101011111100100100 11000001100110001011 10110010000100101111 00100000000100001101 00011000111001101101 11110000100001000011 00000011010000001101 10100000110011011010 00010001110011011000 10100010011001011100 01...
result:
ok n=20
Test #7:
score: 0
Accepted
time: 1ms
memory: 4144kb
input:
100 ++++-+-+--++++++-+--+--++-+-+--+++++-+++---+-+-+-++-+-+++-------+-++--+-++--+--+++++-++-+---+--+--++ -++--++-+-++++-+---++-+-+-+-+-+-+-+-+--+-+--+--+++---+--+-----+-----+-++-++-+-++++++--+-+++-+++-++++ --+---++-++--++-+++-------+--+-++------+-----+--+----++++++++-+--+++++--++--+-+-+++---+--+++-+...
output:
Yes 0000011000010110011000001110000011010011010111000110001110000000101100101100111111110110100010010011 0110011110010100001100111000001110001101000000001100110010000010000010110110101111110010111011101111 1100100001001111110010010000110010100011010011000000111111111010011111001100101011100010011101...
result:
ok n=100
Test #8:
score: 0
Accepted
time: 16ms
memory: 17444kb
input:
500 --+-+-+-++-----+++--+-+++-+---+-+-------+++--++++++-+--++--+-+-++++-++++--++--+---++--++----++--+---++-++--+-----+-+---++-++++-+++++++---++-++--+-++++-+----++-+++-+++---+--+++-+--++-++--+++++++-+++--+---+---+-+---++-+-+--+-+++-++-----+++-++-+++-+-++--++++++-+-++-+++---++-+++-++----+--+++----++++...
output:
Yes 11010101001111100011010001011101011111110001100000010001100101011110111100110010001100110000110010010010011011111010111001000010000000111001001101000010111100100010001110110001011001001100000001000110111011101011100101011010001001111100010010001010011000000101001000111001000100111101100011110000...
result:
ok n=500
Test #9:
score: 0
Accepted
time: 1385ms
memory: 793316kb
input:
4000 -++-+-+-+--+-++++---+-++------++---+-+++--+++--+++++++---+-++-+++++++----+---+++-++--++---+-++--+----+---+--++-+-+-+-----+-+---++-++--+---+++-++++-+-----++--++-++---++-+--+++-+--+--+-+-++-+++--++---+++-+-+---+++-++-+-++-+-+++---+++---+-+--++---+-+---+--+++--+----+-+--++---+-----+-+--+----+-+++-...
output:
Yes 10010101011010000111010011111100111010001100011000000011101001000000011110111000100110011101001101111011101100101010111110101110010011011100010000101111100110010011100101100010110110101001000110011100010101110001001010010100011100011101011001110101110110001101111010110011101111101011011110100010...
result:
ok n=4000
Test #10:
score: 0
Accepted
time: 1288ms
memory: 795120kb
input:
4000 +---+--++-+--++-+-++--+++--++--+++-+-+-+--++++++-++-+-+++-+---++++-+-+++----++-+---++--++--+++-----++---++-+--++++----++--++--+-+-++--+--+++++-+---+++-+++--++++-++-+++++++----+++-----+--++-+++-++-++-+++-+--++++++-+--++--+-+---++--------+-+--++----+-++-+-++---+--++--+-+--+-+++-+--+--++++-++----+...
output:
Yes 01110110010110010100110001100110001010101100000010010100010111000010100011110010111001100110001111100111001011000011110011001101010011011000001011100010001100001001000000011110001111101100100010010010001011000000101100110101110011111111010110011110100101001110110011010110100010110110000100111101...
result:
ok n=4000
Test #11:
score: 0
Accepted
time: 1541ms
memory: 793884kb
input:
4000 -+--+------+--+++-++-----++--+-++-++-++-+-+-++++++--++--+-++-+-+++---++-+-+++++-++++++-+-++-++-++-+-++---+-+-------++-+++-++-+-++++-++-+-+-----+----+--+----++++-------++-+--+++----+++++++-+--+-+---+-+++++-+-----++-------++++--+-+-++---++++-++++-++-+++-+-++---+--+---+-++++++--++---+-++++++-+-+--...
output:
Yes 10110111111011000100111110011010010010010101000000110011010010100011100101000001000000101001001001010011101011111110010001001010000100101011111011110110111100001111111001011000111100000001011010111010000010111110011111110000110101001110000100001001000101001110110111010000001100111010000001010111...
result:
ok n=4000
Test #12:
score: 0
Accepted
time: 1343ms
memory: 796192kb
input:
4000 +-+-----++++-----++++-+-++-+----+++---++--+---+++-+-++------+-+-++++----+-++++--+-++----+-+---+--+-----+-++--++-+++---+---+++---++-+++---++++---++----+++--+---+---+++-++-+-+-+--+++--++----++-+------+++-++-++--+--+++---+-------++++-+-++--++-+--+------+++++---+---++-++++-+++-++++-+---++-++++----+...
output:
Yes 01011111000011111000010100101111000111001101110001010011111101010000111101000011010011110101110110111110100110010001110111000111001000111000011100111100011011101110001001010101100011001111001011111100010010011011000111011111110000101001100101101111110000011101110010000100010000101110010000111100...
result:
ok n=4000
Test #13:
score: 0
Accepted
time: 1454ms
memory: 794300kb
input:
4000 -+++---+-------+-++++-+++-++-+--++----++++++---+---++-++++-+++-++++-+---+--+++-----+-+--++-+--+-++++--+--++-+---+++++++++-+++++++-+++--+-+---++-+-++----+-++--++-++++++++++++++-+++-++-+++-++-++---+---+++-+-+++++++--+-+++-++-+-----+++-++--+++------+++--+++---+--+----++-+--+-+---+--+---+-+-+--++++...
output:
Yes 10001110111111101000010001001011001111000000111011100100001000100001011101100011111010110010110100001101100101110000000001000000010001101011100101001111010011001000000000000001000100100010010011101110001010000000110100010010111110001001100011111100011000111011011110010110101110110111010101100001...
result:
ok n=4000
Test #14:
score: 0
Accepted
time: 1212ms
memory: 795688kb
input:
4000 +--+++++--++--+-+++-++-+-+-+-+--++------++++---+++-+-+--+------++-+--++-+-+++-----+-+++++-+------++++-++++-+--+------++--+++++-+-+--+-+++++++++++++++-+--+-+-+---+-+-+++++-++--+-+---++-++--+--+-+++-+-+++++-+--++-+----+++-++-++++-+---+--+-++-++-+-+-+---++-++-+-+----++-++++-----------+++--++++-++-...
output:
Yes 01100000110011010001001010101011001111110000111000101011011111100101100101000111110100000101111110000100001011011111100110000010101101000000000000000101101010111010100000100110101110010011011010001010000011001101000011101101111010001001011011010101000110110101000011011110000000000011100111101101...
result:
ok n=4000
Test #15:
score: 0
Accepted
time: 1665ms
memory: 793824kb
input:
4000 ---++-++-+-+----++-++++-----------+++--++++-++-++--+-+--+--+-+-++++--++-++--+++++--++--+-+----+-+---++-+++-+--+-++++++++-++---++++-+--+-+++---+-+--+--+-+--+--++++++-+---++++++-++++----+-+-+-++--+---+--+-+++--++++++-+++-+---+--+-----------++-+++--+++++++--++-+++++--+++-+-+++++++++--+---+++--+-+-...
output:
Yes 11100100101011110011011101001000101010001100111110000101100001010110001011101110100111010110001000001111110110101101111111100010110101101110101010000010110100110111010101111100111101001010111100101010010011001110110111110001000000000010001100110011101110010011111011110101011111100010001010010101...
result:
ok n=4000
Test #16:
score: -100
Time Limit Exceeded
input:
4000 ++-+-------+--++-++-++--++-+++++-++++--+-----++-+--+---++++--+-----++++++---+--+---+------+++-+-----+-+++--+++-+-+-+++-----++---+-+---+-+++++-+--+-++--++-+-----++-+-+---+++-+----++++---++--+-+-++-++-+--++---+++------++-+-++++--+--++-++-+++-++-+--++----++---+-+++-+-+++-++-+--++-++++--++-+-+-+-+-...