90pts 求调
查看原帖
90pts 求调
627636
封禁用户楼主2023/2/6 18:55

https://www.luogu.com.cn/record/101563293

#include <iostream>
#include <vector>
using namespace std;
vector<int> a[1000005], seele[1000005], sing[1000005]; int x[1000005], y[1000005], qwasd[1000005], minn, TLEWA; bool vis[1000005];
int read() {int x; cin >> x; return x;}
void dfs(int x, int fa) {
bool flag=false; minn = min(minn, qwasd[x]); vis[x] = true;
for (int i=0; i<sing[x].size(); i++) {
if (vis[sing[x][i]]) {TLEWA++; continue;} else dfs(sing[x][i], x);
} TLEWA--;
}
int main() {
int n, m, k, summ=0; cin >> n >> m; for (int i=1; i<=n; i++) {a[i].push_back(714821520); seele[i].push_back(714821520); for (int j=1; j<=m; j++) {a[i].push_back(read()); seele[i].push_back(0);}} cin >> k; for (int i=1; i<=k; i++) {cin >> x[i] >> y[i]; x[i]++; y[i]++; seele[x[i]][y[i]] = i;}
for (int i=1; i<=k; i++) {
if (x[i] == 1 || y[i] == 1 || x[i] == n || y[i] == m || seele[x[i]-1][y[i]] > 0 || seele[x[i]+1][y[i]] > 0 || seele[x[i]][y[i]-1] > 0 || seele[x[i]][y[i]+1] > 0) sing[i].push_back(i), sing[i].push_back(i); summ += a[x[i]][y[i]]; qwasd[i] = 714821520;
if (x[i] > 1 && seele[x[i]-1][y[i]] <= 0) {if (seele[x[i]-1][y[i]] < 0) {sing[i].push_back(-seele[x[i]-1][y[i]]); sing[-seele[x[i]-1][y[i]]].push_back(i);} else {summ += a[x[i]-1][y[i]]; qwasd[i] = min(qwasd[i], a[x[i]-1][y[i]]);} seele[x[i]-1][y[i]] = -i;}
if (x[i] < n && seele[x[i]+1][y[i]] <= 0) {if (seele[x[i]+1][y[i]] < 0) {sing[i].push_back(-seele[x[i]+1][y[i]]); sing[-seele[x[i]+1][y[i]]].push_back(i);} else {summ += a[x[i]+1][y[i]]; qwasd[i] = min(qwasd[i], a[x[i]+1][y[i]]);} seele[x[i]+1][y[i]] = -i;}
if (y[i] > 1 && seele[x[i]][y[i]-1] <= 0) {if (seele[x[i]][y[i]-1] < 0) {sing[i].push_back(-seele[x[i]][y[i]-1]); sing[-seele[x[i]][y[i]-1]].push_back(i);} else {summ += a[x[i]][y[i]-1]; qwasd[i] = min(qwasd[i], a[x[i]][y[i]-1]);} seele[x[i]][y[i]-1] = -i;}
if (y[i] < m && seele[x[i]][y[i]+1] <= 0) {if (seele[x[i]][y[i]+1] < 0) {sing[i].push_back(-seele[x[i]][y[i]+1]); sing[-seele[x[i]][y[i]+1]].push_back(i);} else {summ += a[x[i]][y[i]+1]; qwasd[i] = min(qwasd[i], a[x[i]][y[i]+1]);} seele[x[i]][y[i]+1] = -i;}
}
for (int i=1; i<=k; i++) if (!vis[i]) {TLEWA = 0; minn = 714821520; dfs(i, 0); TLEWA++; TLEWA /= 2; if (TLEWA > 1) {cout << "No"; return 0;} if (!TLEWA) summ -= minn;} cout << summ;
}
2023/2/6 18:55
加载中...