4651: [Noi2016]网格

来源:互联网 发布:淘宝 户外头巾 编辑:程序博客网 时间:2024/04/29 00:02

4651: [Noi2016]网格

Time Limit: 50 Sec  Memory Limit: 1024 MB
Submit: 40  Solved: 27
[Submit][Status][Discuss]

Description

跳蚤国王和蛐蛐国王在玩一个游戏。
他们在一个 n 行 m 列的网格上排兵布阵。其中的 c 个格子中 (0≤c≤nm),每个格子有一只蛐蛐,其余的格子中,每个格子有一只跳蚤。
我们称占据的格子有公共边的两只跳蚤是相邻的。
我们称两只跳蚤是连通的,当且仅当这两只跳蚤相邻,或存在另一只跳蚤与这两只跳蚤都连通。
现在,蛐蛐国王希望,将某些(0 个,1 个或多个)跳蚤替换成蛐蛐,使得在此之后存在至少两只跳蚤不连通。
例如:我们用图表示一只跳蚤,用图表示一只蛐蛐,那么图 1 描述了一个 n=4,m=4,c=2的情况。
这种情况下蛐蛐国王可以通过将第 2 行第 2 列,和第 3 行第 3 列的两只跳蚤替换为蛐蛐,从而达成他的希望,如图 2 所示。并且,不存在更优的方案,但是可能存在其他替换 2 只跳蚤的方案。
你需要首先判断蛐蛐国王的希望能否被达成。如果能够达成,你还需要最小化被替换的跳蚤的个数。
 

Input

每个输入文件包含多组数据。
输入文件的第一行只有一个整数 TT,表示数据的组数。保证 1≤T≤20。
接下来依次输入 TT 组数据,每组数据的第一行包含三个整数 n, m, c。
保证1≤n,m≤10^9,0≤c≤min(nm,105)
接下来 c行,每行包含两个整数 x, y表示第 x 行,第 y 列的格子被一个蛐蛐占据(1≤x≤n,1≤y≤m)每一组数据当中,同一个蛐蛐不会被多次描述。
同一行相邻的整数之间由一个空格隔开。
1≤n,m≤10^9, 0≤c≤nm, 1≤x≤n, 1≤y≤m
1≤T≤20。我们记 ∑c为某个测试点中,其 T 组输入数据的所有 c 的总和,∑c≤10^5

Output

对于每一组数据依次输出一行答案。
如果这组数据中,蛐蛐国王的希望不能被达成,输出-1。否则,输出被替换的跳蚤的个数的最小值

Sample Input

4
4 4 2
1 1
4 4
2 3 1
1 2
2 2 2
1 1
2 2
1 1 0

Sample Output

2
1
0
-1
explanation
第一组数据就是问题描述中的例子。
对于第二组数据,可以将第 2 行第 2 列的一只跳蚤替换为蛐蛐,从而使得存在两只跳蚤不连通
并且不存在更优的方案。
对于第三组数据,最初已经存在两只跳蚤不连通,故不需要再进行替换。
对于第四组数据,由于最多只有一只跳蚤,所以无论如何替换都不能存在两只跳蚤不连通

HINT

Source

[Submit][Status][Discuss]



离散化 + tarjan求割点。。。。细节挺多的

写网同那天有注意到,答案只可能是-1 0 1 2,,后面就彻底不会了。。。。。

如果询问中蛐蛐把整个网格占满或者只剩一个位置,那答案就是-1

如果网格还剩两只跳蚤,连通的情况下是-1否则是0

网格中已经有跳蚤不连通,答案是0

网格中存在割点答案是1

除去以上情况答案就是2了


剩下的问题依然很大。。。怎么去寻找割点?

首先,割点周围的八连通块一定存在蛐蛐,反过来,只有蛐蛐周围的八连通块存在割点

每个蛐蛐向外扩展两圈,将这24只跳蚤存进一张图,跳蚤同周围的四连通块连边

判断原图是否连通的话,只要判断每个蛐蛐八连通块周围的跳蚤四连通块是否都属于同一连通块

如果原图已经不连通,那么至少有一个蛐蛐八连通块周围至少有两个跳蚤四连通块

最后,对于所有跳蚤连通块做tarjan求割点

但是,这个割点可能是周围没有蛐蛐的跳蚤,但显然这样并不是割点

因此,,,求出割点后还要特判是不是合法割点。。。

#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<queue>#include<algorithm>#include<cmath>#include<map>using namespace std;const int maxn = 1E5 + 10;const int T = 24;const int dx[8] = {0,1,0,-1,1,1,-1,-1};const int dy[8] = {1,0,-1,0,1,-1,1,-1};struct Point{int x,y; Point(){}Point(int x,int y): x(x),y(y){}bool operator < (const Point &b) const{if (x < b.x) return 1;if (x > b.x) return 0;return y < b.y;}}p[maxn],q[maxn*T];int TT,n,m,c,tot,cnt,Cnt,dfs_clock,dfn[maxn*T],low[maxn*T],scc_num[maxn*T],bel[maxn];bool flag,is_cut[maxn*T],vis[maxn*T],Vis[maxn];map <Point,int> m1,m2;vector <int> v0[maxn],v[maxn*T],Node[maxn];void Dfs0(int x){bel[x] = Cnt;for (int i = 0; i < v0[x].size(); i++){int to = v0[x][i];if (Vis[to]) continue;Vis[to] = 1; Dfs0(to);}}void Dfs(int x,int from){int son = 0;low[x] = dfn[x] = ++dfs_clock; scc_num[x] = cnt;for (int i = 0; i < v[x].size(); i++){int to = v[x][i];if (to == from) continue;if (!vis[to]) {vis[to] = 1; ++son; Dfs(to,x);low[x] = min(low[x],low[to]);if (low[to] >= dfn[x]) is_cut[x] = 1;}else low[x] = min(low[x],dfn[to]);}if (from == -1 && son == 1) is_cut[x] = 0;}bool Check(int x){for (int i = 0; i < 8; i++){int xx = q[x].x + dx[i];int yy = q[x].y + dy[i];if (m1.count(Point(xx,yy))) return 1;}return 0;}bool Not_Connect(){for (int i = 1; i <= c; i++)for (int j = 0; j < 8; j++){Point g = Point(p[i].x + dx[j],p[i].y + dy[j]);if (m1.count(g)) v0[i].push_back(m1[g]);}for (int i = 1; i <= c; i++)if (!Vis[i]) Vis[i] = 1,++Cnt,Dfs0(i);for (int i = 1; i <= c; i++)for (int x = -2; x <= 2; x++)for (int y = -2; y <= 2; y++){Point g = Point(p[i].x + x,p[i].y + y);if (g.x <= 0 || g.x > n || g.y <= 0 || g.y > m) continue;if (!m1.count(g) && !m2.count(g)){q[++tot] = g; m2[g] = tot;Node[bel[i]].push_back(tot);}}for (int i = 1; i <= tot; i++)for (int j = 0; j < 4; j++){Point g = Point(q[i].x + dx[j],q[i].y + dy[j]);if (m2.count(g)) v[i].push_back(m2[g]);}for (int i = 1; i <= tot; i++)if (!vis[i]) vis[i] = 1,++cnt,Dfs(i,-1);for (int i = 1; i <= tot; i++)if (is_cut[i] && Check(i)) flag = 1;for (int i = 1; i <= Cnt; i++){int Num = -1;for (int j = 0; j < Node[i].size(); j++){int now = Node[i][j];if (Num == -1) Num = scc_num[now];else if (scc_num[now] != Num) return 1;}}return 0;}int getint(){char ch = getchar(); int ret = 0;while (ch < '0' || '9' < ch) ch = getchar();while ('0' <= ch && ch <= '9')ret = ret*10 + ch - '0',ch = getchar();return ret;}void Clear(){for (int i = 1; i <= tot; i++)v[i].clear(),vis[i] = is_cut[i] = 0;for (int i = 1; i <= c; i++)Node[i].clear(),v0[i].clear(),Vis[i] = 0;flag = cnt = tot = dfs_clock = Cnt = 0;m1.clear(); m2.clear();}void Work(){n = getint(); m = getint(); c = getint();for (int i = 1; i <= c; i++){int x = getint(),y = getint();p[i] = Point(x,y); m1[p[i]] = i;}if (1LL*n*m - c <= 1) {puts("-1"); Clear(); return;}if (Not_Connect()) {puts("0"); Clear(); return;}if (1LL*n*m - c == 2){if (cnt == 1 || !c) puts("-1");else puts("0"); Clear(); return;}if (n == 1 || m == 1) flag = 1;if (flag) puts("1"); else puts("2"); Clear();}int main(){#ifdef DMCfreopen("DMC.txt","r",stdin);#endifTT = getint(); while (TT--) Work();return 0;}

0 0
原创粉丝点击