結果

問題 No.348 カゴメカゴメ
ユーザー keikei
提出日時 2018-11-02 01:46:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,664 bytes
コンパイル時間 2,475 ms
コンパイル使用メモリ 197,376 KB
実行使用メモリ 89,588 KB
最終ジャッジ日時 2024-04-30 15:32:32
合計ジャッジ時間 8,828 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 215 ms
38,912 KB
testcase_01 WA -
testcase_02 AC 421 ms
72,220 KB
testcase_03 AC 425 ms
89,588 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 5 ms
6,940 KB
testcase_13 AC 3 ms
6,940 KB
testcase_14 AC 3 ms
6,940 KB
testcase_15 AC 69 ms
8,448 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
inline ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
inline ll lcm(ll a, ll b) { return a / gcd(a, b)*b; }
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }

/*
 <url:https://yukicoder.me/problems/no/348>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

int dx[8] = {1,1,0,-1,-1,-1, 0, 1};
int dy[8] = {0,1,1,1 ,0 ,-1,-1,-1};
map<ll,ll> mp;
void rec(int n,ll dep,ll& even,ll& odd,vector<vector<pii>>& G,vector<int>& visited){
    if(visited[n]) return;
    visited[n] = 1;
    if(dep%2 == 0){
        even += mp[n];
    }else{
        odd += mp[n];
    }
    for(auto p:G[n]){
        rec(p.first,dep+p.second,even,odd,G,visited);
    }
}
ll solve(){
    ll res = 0;
    int N,M; cin >> N >> M;
    vector<vector<char>> grid(N+2,vector<char>(M+2,'.'));
    for(int i = 1; i <= N; i++) for(int j = 1; j <= M;j++) cin >> grid[i][j];
    
    vector<vector<int>> checked(N+2,vector<int>(M+2,0));
    int dotcnt = 0;
    int ringcnt = 0;
    
    for(int i = 0; i < N+2; i++){
        for(int j = 0; j < M+2; j++){
            if(checked[i][j]!=0) continue;
            char c = grid[i][j];
            int add = 0;
            if(c == '.'){ dotcnt--; add = 2;}
            else{ ringcnt++; add = 1;}
            queue<pii> q; q.push({i,j});
            while(q.size()){
                int x,y;
                tie(y,x) = q.front(); q.pop();
                for(int k = 0; k < 8; k+=add){
                    int nx = x + dx[k];
                    int ny = y + dy[k];
                    if(ny < 0 || nx < 0 || ny >= N+2 || nx >= M+2) continue;
                    if(checked[ny][nx]!=0) continue;
                    if(grid[ny][nx] != c) continue;
                    if(c == '.'){
                        checked[ny][nx] = dotcnt;
                        q.push({ny,nx});
                    }else{
                        checked[ny][nx] = ringcnt;
                        q.push({ny,nx});
                    }
                }
            }
        }
    }
    //cout << checked << endl;
    int SZ = abs(dotcnt)+1+abs(ringcnt);
    vector<vector<pii>> G(SZ);
    for(int i = 0; i < N+2; i++){
        for(int j = 0; j < M+2; j++){
            char c = grid[i][j];
            int add = 0;
            if(c == '.'){ add = 2;}
            else{ add = 1;}
            
            pii S;
            if(checked[i][j] < 0) S = {abs(checked[i][j]),0};
            else S = {abs(dotcnt) + checked[i][j],1};
            
            for(int k = 0; k < 8; k+=add){
                int nx = j + dx[k];
                int ny = i + dy[k];
                if(ny < 0 || nx < 0 || ny >= N+2 || nx >= M+2) continue;
                if(checked[i][j] == checked[ny][nx]) continue;
                pii T;
                if(checked[ny][nx] < 0) T = {abs(checked[ny][nx]),0};
                else T = {abs(dotcnt) + checked[ny][nx],1};
                G[S.first].push_back(T);
                G[T.first].push_back(S);
            }
        }
    }
    for(int i = 0; i < SZ;i++){
        sort(G[i].begin(),G[i].end());
        G[i].erase(unique(G[i].begin(),G[i].end()),G[i].end());
    }
    //cout << G << endl;
    for(int i = 0; i < N+2; i++){
        for(int j = 0; j < M+2; j++){
            if(checked[i][j] > 0){
                mp[abs(dotcnt)+checked[i][j]]++;
            }
        }
    }
    //cout << mp << endl;
    
    vector<int> visited(SZ);
    ll Even = 0,Odd = 0;
    rec(1,0,Even,Odd,G,visited);
    res = max(Even,Odd);
    return res;
}
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    cout << solve() << endl;
    return 0;
}
0