# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
785319 |
2023-07-17T08:20:59 Z |
박상훈(#10022) |
Raspad (COI17_raspad) |
C++17 |
|
141 ms |
69024 KB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct DSU1{
int path[101];
void init(int n){for (int i=1;i<=n;i++) path[i] = i;}
int find(int s){
if (s==path[s]) return s;
return path[s] = find(path[s]);
}
bool merge(int s, int v){
s = find(s), v = find(v);
if (s==v) return false;
path[v] = s;
return true;
}
}dsu, dsu3;
struct DSU2{
int path[101], on[101], cur;
void init(int n){
cur = 0;
for (int i=1;i<=n;i++){
path[i] = i;
cur += on[i];
// printf(" %d", on[i]);
}
// printf("\n");
}
int find(int s){
if (s==path[s]) return s;
return path[s] = find(path[s]);
}
void merge(int s, int v){
s = find(s), v = find(v);
assert(s!=v);
if (on[s] || on[v]) --cur;
on[s] &= on[v];
path[v] = path[s];
}
int count(){return cur;}
}dsu2;
struct Data{
int p, q, t;
Data(){}
Data(int _p, int _q, int _t): p(_p), q(_q), t(_t) {}
};
vector<Data> Dat[100100];
char a[100100][55];
int b[100100][55], cnt[100100], to[55];
vector<int> adj[55];
ll simulate(int sx, int cntV, const vector<Data> &Dat){
int prvT = sx;
dsu2.init(cntV);
ll ret = 0;
for (const auto &[p, q, t]:Dat){
ret += (ll)(prvT-t) * dsu2.count();
dsu2.merge(p, q);
prvT = t;
}
ret += (ll)prvT * dsu2.count();
// printf(" fuck %d\n", dsu2.count());
return ret;
}
ll solve(int n, int m, char a[100100][55]){
// init here
for (int i=1;i<=n;i++){
Dat[i].clear();
fill(b[i], b[i]+m+1, 0);
cnt[i] = 0;
}
ll ans = 0;
for (int i=1;i<=n;i++){
// printf("calc %d\n", i);
//init here
for (int j=1;j<=cnt[i-1];j++){
adj[j].clear();
to[j] = 0;
}
// get component
cnt[i] = 0;
for (int j=1;j<=m;j++) if (a[i][j]=='1'){
if (j>1 && a[i][j-1]=='1') b[i][j] = b[i][j-1];
else b[i][j] = ++cnt[i];
if (b[i-1][j]!=0){
// printf(" push %d %d\n", b[i-1][j], b[i][j]);
adj[b[i-1][j]].push_back(b[i][j]);
to[b[i-1][j]] = b[i][j];
}
}
// for (int j=1;j<=cnt[i-1];j++) printf(" %d", to[j]);
// printf("\n");
dsu.init(cnt[i]);
// merge using (i-1)th row
for (int j=1;j<=cnt[i-1];j++){
for (int k=0;k<(int)adj[j].size()-1;k++){
if (dsu.merge(adj[j][k], adj[j][k+1])){
// printf("ok %d %d\n", j, k);
Dat[i].emplace_back(adj[j][k], adj[j][k+1], i-1);
}
}
}
// reindex
dsu3.init(cnt[i-1]);
for (auto &[p, q, t]:Dat[i-1]){
p = dsu3.find(p), q = dsu3.find(q);
if (!to[p]) swap(p, q);
dsu3.merge(p, q);
}
// merge using x(< i-1)th row
for (auto &[p, q, t]:Dat[i-1]) if (to[p] && to[q]){
if (dsu.merge(to[p], to[q])){
Dat[i].emplace_back(to[p], to[q], t);
}
}
// update ans
fill(dsu2.on+1, dsu2.on+cnt[i]+1, 1);
ll val1 = simulate(i, cnt[i], Dat[i]);
ans += val1;
// printf(" ok add %lld\n", val1);
// calc with invalid (i-1)th row component
fill(dsu2.on+1, dsu2.on+cnt[i-1]+1, 0);
for (int j=1;j<=cnt[i-1];j++) if (!to[j]) dsu2.on[j] = 1;
ll val2 = simulate(i-1, cnt[i-1], Dat[i-1]);
ans += val2 * (n-i+1);
// printf(" ok add %lld * %d = %lld\n", val2, n-i+1, val2 * (n-i+1));
}
return ans;
}
int visited[100100][55], dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
void dfs(int x, int y, char a[100100][55], int s, int e, int m){
visited[x][y] = 1;
for (int k=0;k<4;k++){
int nx = x + dx[k], ny = y + dy[k];
if (nx < s || nx > e) continue;
if (ny < 1 || ny > m) continue;
if (a[nx][ny]!='1') continue;
if (visited[nx][ny]) continue;
dfs(nx, ny, a, s, e, m);
}
}
ll count_naive(int s, int e, int m, char a[100100][55]){
ll ans = 0;
for (int i=s;i<=e;i++){
fill(visited[i]+1, visited[i]+m+1, 0);
}
for (int i=s;i<=e;i++){
for (int j=1;j<=m;j++) if (!visited[i][j] && a[i][j]=='1'){
dfs(i, j, a, s, e, m);
ans++;
}
}
return ans;
}
ll naive(int n, int m, char a[100100][55]){
ll ans = 0;
for (int i=1;i<=n;i++){
for (int j=i;j<=n;j++) ans += count_naive(i, j, m, a);
}
return ans;
}
mt19937 seed(1557);
uniform_int_distribution<int> rng(0, 2147483647);
int getrand(int l, int r){return rng(seed)%(r-l+1) + l;}
int n, m;
void gen(){
n = getrand(1, 20);
m = getrand(1, 20);
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++) a[i][j] = '0' + !!(getrand(0, 1));
a[i][m+1] = 0;
}
}
void stress(int tc){
printf("-----------------------------------------\n");
printf("Stress #%d\n", tc);
gen();
printf("Input:\n");
printf("%d %d\n", n, m);
for (int i=1;i<=n;i++){
printf("%s\n", a[i]+1);
}
ll ans = naive(n, m, a);
ll out = solve(n, m, a);
printf("Answer: %lld\n", ans);
printf("Output: %lld\n", out);
assert(ans == out);
}
int main(){
// for (int i=1;;i++) stress(i);
int n, m;
scanf("%d %d", &n, &m);
for (int i=1;i<=n;i++) scanf("%s", a[i]+1);
printf("%lld\n", solve(n, m, a));
// printf("%lld\n", naive(n, m, a));
}
Compilation message
raspad.cpp: In function 'int main()':
raspad.cpp:242:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
242 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
raspad.cpp:244:30: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
244 | for (int i=1;i<=n;i++) scanf("%s", a[i]+1);
| ~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2900 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
3 ms |
3028 KB |
Output is correct |
10 |
Correct |
3 ms |
3028 KB |
Output is correct |
11 |
Correct |
3 ms |
2900 KB |
Output is correct |
12 |
Correct |
2 ms |
3028 KB |
Output is correct |
13 |
Correct |
2 ms |
2900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
16212 KB |
Output is correct |
2 |
Correct |
51 ms |
30036 KB |
Output is correct |
3 |
Correct |
53 ms |
32984 KB |
Output is correct |
4 |
Correct |
24 ms |
27196 KB |
Output is correct |
5 |
Correct |
12 ms |
10876 KB |
Output is correct |
6 |
Correct |
53 ms |
31264 KB |
Output is correct |
7 |
Correct |
39 ms |
35668 KB |
Output is correct |
8 |
Correct |
29 ms |
25800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2900 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
3 ms |
3028 KB |
Output is correct |
10 |
Correct |
3 ms |
3028 KB |
Output is correct |
11 |
Correct |
3 ms |
2900 KB |
Output is correct |
12 |
Correct |
2 ms |
3028 KB |
Output is correct |
13 |
Correct |
2 ms |
2900 KB |
Output is correct |
14 |
Correct |
17 ms |
16212 KB |
Output is correct |
15 |
Correct |
51 ms |
30036 KB |
Output is correct |
16 |
Correct |
53 ms |
32984 KB |
Output is correct |
17 |
Correct |
24 ms |
27196 KB |
Output is correct |
18 |
Correct |
12 ms |
10876 KB |
Output is correct |
19 |
Correct |
53 ms |
31264 KB |
Output is correct |
20 |
Correct |
39 ms |
35668 KB |
Output is correct |
21 |
Correct |
29 ms |
25800 KB |
Output is correct |
22 |
Correct |
66 ms |
25828 KB |
Output is correct |
23 |
Correct |
136 ms |
42304 KB |
Output is correct |
24 |
Correct |
141 ms |
44048 KB |
Output is correct |
25 |
Correct |
75 ms |
31148 KB |
Output is correct |
26 |
Correct |
81 ms |
50276 KB |
Output is correct |
27 |
Correct |
70 ms |
33148 KB |
Output is correct |
28 |
Correct |
106 ms |
40128 KB |
Output is correct |
29 |
Correct |
100 ms |
33012 KB |
Output is correct |
30 |
Correct |
51 ms |
29908 KB |
Output is correct |
31 |
Correct |
105 ms |
69024 KB |
Output is correct |
32 |
Correct |
79 ms |
49804 KB |
Output is correct |
33 |
Correct |
73 ms |
33196 KB |
Output is correct |
34 |
Correct |
94 ms |
34796 KB |
Output is correct |