結果

問題 No.348 カゴメカゴメ
ユーザー koprickykopricky
提出日時 2017-09-12 12:53:29
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 4,775 bytes
コンパイル時間 1,456 ms
コンパイル使用メモリ 175,872 KB
実行使用メモリ 68,992 KB
最終ジャッジ日時 2024-04-25 07:02:06
合計ジャッジ時間 17,141 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 AC 38 ms
50,816 KB
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 AC 38 ms
50,816 KB
testcase_07 WA -
testcase_08 AC 36 ms
50,688 KB
testcase_09 AC 35 ms
50,816 KB
testcase_10 WA -
testcase_11 AC 37 ms
50,816 KB
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 35 ms
50,816 KB
testcase_21 AC 36 ms
50,816 KB
testcase_22 AC 36 ms
50,688 KB
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 RE -
testcase_44 RE -
testcase_45 RE -
testcase_46 RE -
testcase_47 RE -
testcase_48 RE -
testcase_49 RE -
testcase_50 RE -
testcase_51 RE -
testcase_52 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define each(a,b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int>P;

const int MAX_N = 1005;

const int dx[] = {1,0,0,-1};
const int dy[] = {0,1,-1,0};
const int dx2[] = {1,1,1,0,0,-1,-1,-1};
const int dy2[] = {1,0,-1,1,-1,1,0,-1};

class UF {
private:
    int sz;
    vector<int> par;
    vector<int> nrank;

public:
    UF(int node_size){
        sz = node_size;
        par.resize(sz);
        nrank.resize(sz);
    	rep(i,sz){
    		par[i] = i;
    		nrank[i] = 0;
    	}
    }

    int find(int x){
    	if(par[x] == x){
    		return x;
    	}else{
    		return par[x] = find(par[x]);
    	}
    }

    void unite(int x,int y){
    	x = find(x);
    	y = find(y);
    	if(x == y) return;
    	if(nrank[x] < nrank[y]){
    		par[x] = y;
    	}else{
    		par[y] = x;
    		if(nrank[x] == nrank[y])
    			nrank[x]++;
    	}
    }

    bool same(int x,int y){
    	return find(x) == find(y);
    }
};

bool visited[MAX_N];
int n,m;
vector<int> graph[MAX_N*MAX_N];
set<int> st;
string s[MAX_N];
int kind[MAX_N*MAX_N];
int kind_cnt[MAX_N*MAX_N];

void dfs(int u,int v)
{
    visited[u*m+v] = true;
    rep(i,4){
        int nx = u + dx[i],ny = v + dy[i];
        if(0 <= nx && nx < n && 0 <= ny && ny < m && !visited[m*nx+ny]){
            if(st.count(kind[m*nx+ny]) == 0){
                st.insert(kind[m*nx+ny]);
                graph[kind[m*nx+ny]].pb(kind[m*u+v]);
                graph[kind[m*u+v]].pb(kind[m*nx+ny]);
            }
            dfs(nx,ny);
        }
    }
}

int dp[MAX_N*MAX_N][2];
vector<int> G[MAX_N*MAX_N];

void make_graph(int u,int p,int pp,int depth)
{
    if(depth % 2 == 0){
        rep(i,graph[u].size()){
            if(graph[u][i] != p){
                make_graph(graph[u][i],u,p,depth+1);
            }
        }
    }else{
        rep(i,graph[u].size()){
            if(graph[u][i] != p){
                G[pp].pb(u),G[u].pb(pp);
                make_graph(graph[u][i],u,p,depth+1);
            }
        }
    }
}

void dfs_dp(int u,int p)
{
    if(u == 0){
        rep(i,G[u].size()){
            if(G[u][i] != p){
                dfs_dp(G[u][i],u);
                dp[u][0] += max(dp[G[u][i]][0],dp[G[u][i]][1]);
            }
        }
    }else{
        rep(i,G[u].size()){
            if(G[u][i] != p){
                dfs_dp(G[u][i],u);
                dp[u][0] += max(dp[G[u][i]][0],dp[G[u][i]][1]);
                dp[u][1] += dp[G[u][i]][0];
            }
        }
        dp[u][1] += kind_cnt[u];
    }
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> n >> m;
    rep(i,n+2){
        s[i].resize(m+2);
    }
    rep(i,m+2){
        s[0][i] = s[n+1][i] = '.';
    }
    rep(i,n){
        string t;
        cin >> t;
        s[i+1][0] = s[i+1][m+1] = '.';
        rep(j,m){
            s[i+1][j+1] = t[j];
        }
    }
    n += 2,m += 2;
    UF uf(n*m);
    rep(i,n){
        rep(j,m){
            if(s[i][j] == 'x'){
                rep(k,8){
                    int u = i+dx2[k],v = j+dy2[k];
                    if(0 <= u && u < n && 0 <= v && v < m && s[u][v] == 'x'){
                        uf.unite(i*m+j,u*m+v);
                    }
                }
            }else{
                rep(k,4){
                    int u = i+dx[k],v = j+dy[k];
                    if(0 <= u && u < n && 0 <= v && v < m && s[u][v] == '.'){
                        uf.unite(i*m+j,u*m+v);
                    }
                }
            }
        }
    }
    map<int,int> mp;
    int syurui = 0;
    rep(i,n){
        rep(j,m){
            int par = uf.find(i*m+j);
            if(mp.count(par) == 0){
                mp[par] = syurui++;
            }
            kind[i*m+j] = mp[par];
            kind_cnt[kind[i*m+j]]++;
        }
    }
    st.insert(kind[0]);
    dfs(0,0);
    // rep(i,syurui){
    //     rep(j,graph[i].size()){
    //         cout << i << " " << graph[i][j] << endl;
    //     }
    // }
    make_graph(0,0,0,0);
    // rep(i,syurui){
    //     rep(j,G[i].size()){
    //         cout << i << " " << G[i][j] << endl;a
    //     }
    // }
    dfs_dp(0,-1);
    cout << dp[0][0] << endl;
    return 0;
}
0