首页 > 其他分享 >题解 ABC334G【Christmas Color Grid 2】

题解 ABC334G【Christmas Color Grid 2】

时间:2023-12-24 09:22:57浏览次数:30  
标签:return Color 题解 rep int Grid operator Modint friend

先求出初始时绿连通块数量。

将一个绿色格子染成红色,会改变绿连通块数量,当且仅当这个绿色格子是孤点或割点。如果是孤点,会使得绿连通块数量减少一;如果是割点,会使得绿连通块数量增加它所在的点双数量减一。根据上述规则可以求出每个绿色格子染红后的绿连通块数量,求平均值即可。

时间复杂度 \(O(nm)\)。

// Problem: G - Christmas Color Grid 2
// Contest: AtCoder - UNIQUE VISION Programming Contest 2023 Christmas (AtCoder Beginner Contest 334)
// URL: https://atcoder.jp/contests/abc334/tasks/abc334_g
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//By: OIer rui_er
#include <bits/stdc++.h>
#define rep(x, y, z) for(int x = (y); x <= (z); ++x)
#define per(x, y, z) for(int x = (y); x >= (z); --x)
#define debug(format...) fprintf(stderr, format)
#define fileIO(s) do {freopen(s".in", "r", stdin); freopen(s".out", "w", stdout);} while(false)
#define endl '\n'
using namespace std;
typedef long long ll;

mt19937 rnd(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
int randint(int L, int R) {
    uniform_int_distribution<int> dist(L, R);
    return dist(rnd);
}

template<typename T> void chkmin(T& x, T y) {if(x > y) x = y;}
template<typename T> void chkmax(T& x, T y) {if(x < y) x = y;}

template<int mod>
inline unsigned int down(unsigned int x) {
	return x >= mod ? x - mod : x;
}

template<int mod>
struct Modint {
	unsigned int x;
	Modint() = default;
	Modint(unsigned int x) : x(x) {}
	friend istream& operator>>(istream& in, Modint& a) {return in >> a.x;}
	friend ostream& operator<<(ostream& out, Modint a) {return out << a.x;}
	friend Modint operator+(Modint a, Modint b) {return down<mod>(a.x + b.x);}
	friend Modint operator-(Modint a, Modint b) {return down<mod>(a.x - b.x + mod);}
	friend Modint operator*(Modint a, Modint b) {return 1ULL * a.x * b.x % mod;}
	friend Modint operator/(Modint a, Modint b) {return a * ~b;}
	friend Modint operator^(Modint a, int b) {Modint ans = 1; for(; b; b >>= 1, a *= a) if(b & 1) ans *= a; return ans;}
	friend Modint operator~(Modint a) {return a ^ (mod - 2);}
	friend Modint operator-(Modint a) {return down<mod>(mod - a.x);}
	friend Modint& operator+=(Modint& a, Modint b) {return a = a + b;}
	friend Modint& operator-=(Modint& a, Modint b) {return a = a - b;}
	friend Modint& operator*=(Modint& a, Modint b) {return a = a * b;}
	friend Modint& operator/=(Modint& a, Modint b) {return a = a / b;}
	friend Modint& operator^=(Modint& a, int b) {return a = a ^ b;}
	friend Modint& operator++(Modint& a) {return a += 1;}
	friend Modint operator++(Modint& a, int) {Modint x = a; a += 1; return x;}
	friend Modint& operator--(Modint& a) {return a -= 1;}
	friend Modint operator--(Modint& a, int) {Modint x = a; a -= 1; return x;}
	friend bool operator==(Modint a, Modint b) {return a.x == b.x;}
	friend bool operator!=(Modint a, Modint b) {return !(a == b);}
};

typedef Modint<998244353> mint;

const int K = 1e3 + 5, N = 1e6 + 5;
const int nxt[4][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};

int n, m, k, vis[K][K];
string s[K];
int dfn[N], low[N], cut[N], cnt[N], tms;
vector<vector<int>> bcc;
stack<int> stk;
mint ans, tot;

inline int getId(int x, int y) {
    return (x - 1) * m + y;
}

void dfs(int x, int y, int u) {
    vis[x][y] = u;
    rep(d, 0, 3) {
        int nx = x + nxt[d][0], ny = y + nxt[d][1];
        if(s[nx][ny] == '#' && !vis[nx][ny]) dfs(nx, ny, u);
    }
}

struct Edge {
	int v, nxt;
	Edge(int a = 0, int b = 0) : v(a), nxt(b) {}
}e[N * 8];
int h[N], ne = 1;
inline void add(int u, int v) {
	e[++ne] = Edge(v, h[u]); h[u] = ne;
	e[++ne] = Edge(u, h[v]); h[v] = ne;
}

void tarjan(int u, int fa) {
	dfn[u] = low[u] = ++tms;
	stk.push(u);
	int deg = 0;
	for(int i = h[u]; i; i = e[i].nxt) {
		int v = e[i].v;
		if(v == fa) continue;
		if(!dfn[v]) {
			++deg;
			tarjan(v, u);
			chkmin(low[u], low[v]);
			if(low[v] >= dfn[u]) {
			    cut[u] = 1;
				bcc.push_back({}); 
				while(true) {
					int w = stk.top(); stk.pop();
					bcc.back().push_back(w);
					++cnt[w];
					if(w == v) break;
				}
				bcc.back().push_back(u);
				++cnt[u];
			}
		}
		else chkmin(low[u], dfn[v]);
	}
	if(!fa && deg == 1) cut[u] = 0;
	if(!fa && !deg) {
	    bcc.push_back({u});
	    cut[u] = -1;
	}
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> m;
    s[0] = s[n + 1] = string(m + 2, ' ');
    rep(i, 1, n) {
        cin >> s[i];
        s[i] = ' ' + s[i] + ' ';
    }
    rep(i, 1, n) rep(j, 1, m) if(s[i][j] == '#' && !vis[i][j]) dfs(i, j, ++k);
    rep(i, 1, n) {
        rep(j, 1, m) {
            if(s[i][j] == '#') {
                rep(d, 0, 3) {
                    int nx = i + nxt[d][0], ny = j + nxt[d][1];
                    if(s[nx][ny] == '#') add(getId(i, j), getId(nx, ny));
                }
            }
        }
    }
    rep(i, 1, n) {
        rep(j, 1, m) {
            if(s[i][j] == '#' && !dfn[getId(i, j)]) {
                tarjan(getId(i, j), 0);
            }
        }
    }
    rep(i, 1, n) {
        rep(j, 1, m) {
            if(s[i][j] == '#') {
                ++tot;
                ans += k;
                if(cut[getId(i, j)] == 1) ans += cnt[getId(i, j)] - 1;
                else if(cut[getId(i, j)] == -1) --ans;
            }
        }
    }
    cout << ans / tot << endl;
    return 0;
}

标签:return,Color,题解,rep,int,Grid,operator,Modint,friend
From: https://www.cnblogs.com/ruierqwq/p/abc334g.html

相关文章

  • 题解 ABC334F【Christmas Present 2】
    设\(f_i\)表示假设只有编号为\(1\simi\)的点,此时的答案。\(f_n\)即为所求。显然有:\[f_i=\min\limits_{i-k\lej<i}\{f_j+dis(s\toj+1\toj+2\to\cdots\toi)\}+dis(i\tos)\]当\(i\toi+1\)时,大括号内部全局增加\(dis(i\toi+1)\),可以全局打标记后单调队列维护。......
  • 题解 ABC334E【Christmas Color Grid 1】
    先求出初始时绿连通块数量。枚举每个红色格子,将其染成绿色本应增加一个绿连通块,但是它每与一个绿连通块相邻,就又会减少一个绿连通块。根据上述规则可以求出每个红色格子染绿后的绿连通块数量,求平均值即可。时间复杂度\(O(nm)\)。//Problem:E-ChristmasColorGrid1//Co......
  • 检查Windows更新问题解决
    在任务栏搜索框输入cmd,点击右侧的“以管理员身份运行”,打开后输入:(建议复制粘贴,防止输入有误出现错误提示等请忽略*)SCconfigwuauservstart=auto回车(Enter按键)SCconfigbitsstart=auto回车(Enter按键)SCconfigcryptsvcstart=auto回车(Enter按键)SCconfigtrustedin......
  • 【题解】洛谷P1068 [NOIP2009 普及组] 分数线划定 (map)
    ##题目描述世博会志愿者的选拔工作正在A市如火如荼的进行。为了选拔最合适的人才,A市对所有报名的选手进行了笔试,笔试分数达到面试分数线的选手方可进入面试。面试分数线根据计划录取人数的$150\%$划定,即如果计划录取$m$名志愿者,则面试分数线为排名第$m\times150\%$(向......
  • P3893 [GDOI2014] Beyond 题解
    P3893[GDOI2014]Beyond题解思路称第一个字符串为\(A\),第二个字符串\(B\)。考虑枚举环长\(L\),那么如果\(L\)是可行的,当且仅当存在一个位置\(i\),使得\(A_{1\simi}=B_{L-i+1,L},A_{i+1\simL}=B_{1,L-i}\),也就是\(A_{1\simL}\)的一个前缀和\(B_{1\s......
  • 【洛谷 P1781】宇宙总统 题解(高精度+结构体排序)
    宇宙总统题目描述地球历公元6036年,全宇宙准备竞选一个最贤能的人当总统,共有个非凡拔尖的人竞选总统,现在票数已经统计完毕,请你算出谁能够当上总统。输入格式第一行为一个整数,代表竞选总统的人数。接下来有行,分别为第一个候选人到第个候选人的票数。输出格式共两行,第一行是......
  • 浅谈WPF之DataGrid过滤,分组,排序
    使用过Excel的用户都知道,Excel可以方便的对数据进行分组,过滤,排序等操作,而在WPF中,默认提供的DataGrid只有很简单的功能,那么如何才能让我们开发的DataGrid,也像Excel一样具备丰富的客户端操作呢?今天就以一个简单的小例子,简述如何在WPF中实现DataGrid的过滤,筛选,排序等功能。仅供学习分......
  • 题解:【XR-3】核心城市
    题解:【XR-3】核心城市思路一:考虑由特例推广到一般1、很容易想到先考虑一个关键点的情况,然后再推广到一般情况。2、一个点肯定选距离上最平衡的那个点,即树的中心。接着在中心周围贪心的选剩下的(k-1)个关键点即可。3、这里有一个误区:各点到某点的距离最小,是找树的中心而不是重......
  • 闭合区域面积统计 题解
    题目描述计算一个\(10\times10\)矩阵中由\(1\)围成的图形的面积。如下所示,在\(10\times10\)的二维数组中,\(1\)围住了\(15\)个点,因此面积为\(15\)。000000000000001110000000100100000001001000100010100101......
  • CF1881F Minimum Maximum Distance 题解
    因为白点对\(f_i\)没有贡献,所以可以重构出一棵原树的子树,使得所有的叶子都为标记点且标记点数量不变(没有删去标记点)。因为没有标记被删去且结构不变,所以这棵树的答案与原树答案相同。现在,对于所有节点,到它距离最大的标记点一定在叶子上。那么问题就变为:求出树上任意一点到所有......